list adapter using fragments - java

I am using a listAdapter to try to populate a listView. but i am having an issue and it is causing my application to crash. i initially wanted to populate the list into xml objects but i believe a listView would be better for my application..THIS IS MY ADAPTER
package com.example.victor.kidsrewards;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.view.LayoutInflater;
import android.widget.TextView;
import java.util.List;
/**
* Created by Victor on 3/20/2016.
*/
public class ListAdapter extends ArrayAdapter<Task> {
private final Context context;
private final List<Task> taskList;
public ListAdapter(Context context, List<Task> taskList){
super(context, R.layout.fragment_tasks, taskList);
this.context = context;
this.taskList= taskList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.fragment_tasks,parent,false);
/*TextView taskText = (TextView)rowView.findViewById(R.id.newTaskText);
taskText.setText(taskList.get(position).getText());
TextView pointsText = (TextView)rowView.findViewById(R.id.newPointsText);
pointsText.setText(taskList.get(position).getText());*/
return rowView;
}
}
**
**
this is my xml code
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="TODO TEXT GOES_HERE">
</ListView>
</FrameLayout>
**
**
This is my fragment where i call my listadapter
package com.example.victor.kidsrewards;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class TaskList extends ListFragment {
private TaskDataBase tdb;
public TaskList() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_task_list, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
tdb = new TaskDataBase(getContext());
setListAdapter(new ListAdapter(getContext(), tdb.getTasks()));
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
}

What is the name of the layout file that you have included?
If it is fragment_tasks.xml then fragment_tasks.xml should not have a ListView. It should at least have the textView with id newTaskText.
Define the content layout which contains a listView. Then there is a layout for each row. This should only contain the TextView (and other views relevant to a single row) but not ListView.

Related

My getBindingAdapterrPosition only returns -1

I'm trying to implement a feature on my application, where - if you click on a RecyclerView item - it opens up a dialog box for that item with a picture - kind of like InstaGram.
However, I am trying to make an onClickListener, where I get the position of the adapter. The goal is, that it reads what item I click on, so it can open up a dialog box for that specific item. But no matter what item in the recyclerview I click on, it returns the position as being -1.
Here is my code for it:
UserAdapter.java
import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.myndapplication.R;
import com.example.android.myndapplication.model.User;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> userList;
Dialog myDialog;
public UserAdapter(Context mContext, List<User> userList) {
this.mContext = mContext;
this.userList = userList;
}
#NonNull
#Override
public UserAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_list_item, parent, false);
final ViewHolder ViewHolder = new ViewHolder(view);
ViewHolder.item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "Test Click"+String.valueOf(ViewHolder.getBindingAdapterPosition()), Toast.LENGTH_SHORT).show();
}
});
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull UserAdapter.ViewHolder holder, int position) {
User user = userList.get(position);
holder.imageView.setImageResource(userList.get(position).getImage());
holder.textView.setText(userList.get(position).getTitle());
}
#Override
public int getItemCount() {
return userList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CardView item;
private CircleImageView iv_profile;
private TextView tv_username;
private TextView tv_date;
private ImageView iv_content;
private ImageView imageView;
private TextView textView;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.img_bookmarks);
textView = itemView.findViewById(R.id.title_bookmarks);
item = (CardView) itemView.findViewById(R.id.profile_item);
iv_profile = (CircleImageView) itemView.findViewById(R.id.dialog_profile_image);
tv_username = (TextView) itemView.findViewById(R.id.dialog_profile_username);
tv_date = (TextView) itemView.findViewById(R.id.dialog_profile_date);
iv_content = (ImageView) itemView.findViewById(R.id.dialog_content_image);
}
}
}
UserRecycler1.java
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.myndapplication.R;
import com.example.android.myndapplication.adapter.UserAdapter;
import com.example.android.myndapplication.model.User;
import java.util.ArrayList;
import java.util.List;
public class UserRecycler1 extends Fragment {
private RecyclerView recyclerView;
private UserAdapter adapter;
private List<User> userList;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
userList = new ArrayList<>();
User user1 = new User(R.drawable.annapelzer, "Salad");
User user2 = new User(R.drawable.brookelark_1, "Pasta");
User user3 = new User(R.drawable.brookelark_2, "Fruit Salad");
User user4 = new User(R.drawable.brookelark_3, "Smoothies with Fruit");
User user5 = new User(R.drawable.cala, "Soup");
User user6 = new User(R.drawable.davide_cantelli, "Lobster Salad");
User user7 = new User(R.drawable.joseph_gonzales, "Breakfast toast with Berries");
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
userList.add(user5);
userList.add(user6);
userList.add(user7);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_recycler, container, false);
recyclerView = view.findViewById(R.id.recycler1);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
adapter = new UserAdapter(getActivity(), userList);
recyclerView.setAdapter(adapter);
return view;
}
}
MainFragmentTab.java
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.myndapplication.R;
import com.example.android.myndapplication.adapter.TabAdapter;
public class MainFragmentTab extends Fragment {
private TabAdapter adapter;
private TabLayout tableLayout;
private ViewPager viewPager;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tabs, container, false);
viewPager = view.findViewById(R.id.request_orders_view_pager);
tableLayout = view.findViewById(R.id.request_orders_tabs);
adapter = new TabAdapter(getFragmentManager());
adapter.addFragment(new UserRecycler1(), "Public Bookmarks");
adapter.addFragment(new UserRecycler1(), "Latest Posts");
viewPager.setAdapter(adapter);
tableLayout.setupWithViewPager(viewPager);
return view;
}
}
recycler_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true"
app:cardCornerRadius="8dp"
android:id="#+id/profile_item">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/img_bookmarks"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/title_bookmarks"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/title2"
android:textColor="#color/dark_green"
android:padding="5dp"
android:textSize="16sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
I hope you all can help, as I have tried for a while, but continue to receive the same output no matter what I do.
You have to move your setOnClickListener from onCreateViewHolder into onBindViewHolder.
You might want to check this tutorial
Edit
Something like here:
#NonNull
#Override
public UserAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_list_item, parent, false);
final ViewHolder ViewHolder = new ViewHolder(view);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull UserAdapter.ViewHolder holder, int position) {
User user = userList.get(position);
holder.imageView.setImageResource(userList.get(position).getImage());
holder.textView.setText(userList.get(position).getTitle());
holder.item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "Test Click"+userList.get(position), Toast.LENGTH_SHORT).show();
}
});
}

error: cannot find symbol variable itemsAdapter

I'm following a tutorial and I can't get my project to successfully run. I keep getting the error: cannot find symbol variable itemsAdapter, even though my class is in the same folder. Any ideas? Thank you
MainActivity.java
itemsAdapter.java
MainActivity.java:
package com.example.simpletodo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<String> items;
Button btnAdd;
EditText etItem;
RecyclerView rvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
etItem = findViewById(R.id.etItem);
rvItems = findViewById(R.id.rvItems);
items = new ArrayList<>();
items.add("Buy milk");
items.add("Go to the gym");
items.add("Email Autumn");
itemsAdapter ItemsAdapter;
ItemsAdapter = new itemsAdapter(items);
rvItems.setAdapter(itemsAdapter);
rvItems.setLayoutManager(new LinearLayoutManager(this));
}
}
ItemsAdapter.java:
package com.example.simpletodo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class itemsAdapter extends
RecyclerView.Adapter<itemsAdapter.ViewHolder>{
List<String> items;
public itemsAdapter(List<String> items) {
this.items = items;
}
// #NonNull
#Override
// creates each view
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
// Use layout inflator to inflate a view
View todoView = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
// wrap it inside a View Holder and return it
return new ViewHolder(todoView);
}
// responsible for binding data to a particular view holder
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// Grab the item at the position
String item = items.get(position);
// Bind the item into specified view holder
holder.bind(item);
}
// the # of items available in the data
#Override
public int getItemCount() {
return 0;
}
// Container to provide easy access to views that represent each row of the list
class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(#NonNull View itemView) {
super(itemView);
}
// Update the view inside of the view holder with this data
public void bind(String item) {
}
}
In line 41 of MainActivity.java itemsAdapter needs to be ItemsAdapter
PS: Your naming standard for itemsAdapter is reversed from the convention. The class should be named ItemsAdapter and the variable/instance should be itemsAdapter

RecyclerView that's supposed to diplay cardViews doesn't show anything

I have a recyclerView that must show a list of cards, but it doesn't!
There is an arrayList of recipes get passed to the adapter to display them as cardviews, everything in the debugging seems to be alright, but it doesnt display anything on the screen.
ViewHolder:
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.nd.ameer.bake.R;
public class RecipeViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public ImageView coverImageView;
public RecipeViewHolder(View v) {
super(v);
titleTextView = (TextView) v.findViewById(R.id.titleTextView);
coverImageView = (ImageView) v.findViewById(R.id.recipeImageView);
}
}
Adapter:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import com.example.nd.ameer.bake.R;
import com.example.nd.ameer.bake.models.Recipe;
import com.example.nd.ameer.bake.views.holders.RecipeViewHolder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class RecipeAdapter extends
RecyclerView.Adapter<RecipeViewHolder> {
ArrayList<Recipe> recipes = new ArrayList<>();
Context context;
public RecipeAdapter(ArrayList<Recipe> recipes, Context context) {
this.recipes = recipes;
this.context = context;
}
#Override
public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recipe_item, parent, false);
return new RecipeViewHolder(view);
}
#Override
public void onBindViewHolder(RecipeViewHolder holder, int position) {
holder.coverImageView.setImageResource(R.color.colorPrimaryLight);
holder.titleTextView.setText(recipes.get(position).getName());
}
#Override
public int getItemCount() {
return recipes.size();
}
}
Fragment onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_recipe, container, false);
createRecipes();
recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recipe);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
if (recipes.size() > 0 & recyclerView != null) {
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new RecipeAdapter(recipes, getContext()));
}
return rootView;
}
The method createRecipes(); creates a list of recipes and then it get passed to the adapter.
as you can see, the recipes size is 4, so it's not the problem
I didn't know the cause of the problem till now, but I moved both the adapter and the view holder to the Recipes Fragment as inner classes, and this has fixed the problem.

my image slider doesn't work on all devices

i have created an app which contain number of buttons and each button is connected with number of photos (image slider (i think i am using image slider using viewerpage)) ....... it works perfectly on some devices such as 6.0.1 but when i am tring to use it on another devices such as 4.2.2 it stops immediately once i click on the butuon and here and i have done in xml (it's name tip_3)
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
and i have created another xml ( as the walk-through in the youtube ) (xml name algwhra )
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and here is the java for (tips_3) :
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Tips_3 extends AppCompatActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tips_3);
viewPager = (ViewPager) findViewById(R.id.viewPager);
AlGwhra viewPagerAdapter = new AlGwhra(this);
viewPager.setAdapter(viewPagerAdapter);
}
}
and here is the (algwhra) java :
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import javax.xml.transform.Result;
/**
* Created by Hassan on 12-Jul-17.
*/
public class AlGwhra extends PagerAdapter{
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images ={R.drawable.g1,R.drawable.g2,R.drawable.g3,R.drawable.g4,R.drawable.g5,R.drawable.g6,R.drawable.g7,R.drawable.g8,R.drawable.g9,R.drawable.g10,R.drawable.g11,R.drawable.g12,R.drawable.g13};
public AlGwhra(Context context) {
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.algwhra, null);
ImageView imageView =(ImageView) view.findViewById(R.id.imageView2);
imageView.setImageResource(images[position]);
ViewPager vp =(ViewPager) container;
vp.addView(view ,0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp =(ViewPager) container;
View view =(View) object;
vp.removeView(view);
}
}

How to declare Adapter in fragment?

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockFragment;
public class FragmentTab2 extends SherlockFragment {
private String[]names = {"Video", "Audio"};
private String[]urls = {"http://c172200.r0.cf3.rackcdn.com/104824.mp4", "http://c172200.r0.cf3.rackcdn.com/106973.mp4"};
#Override
public View onCreateView(LayoutInflater inflater1, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab2.xml
View view = inflater1.inflate(R.layout.fragmenttab2, container, false);
return view;
ListView lv = (ListView)findViewById(R.id.List);
VideoAdapter adapter = new VideoAdapter(this, names, urls);
lv.setAdapter(adapter);
}
}
I'm to make a listview in a fragmenttab. I have the created the interface using a tutorial. http://www.youtube.com/watch?v=54wC1lgmbKQ In the video you can see application. I'm not sure where to declare and to declare the adapter. Right now i'm declaring in in the fragmenttab1 activity it self, but this causes errors to appear, saying that "The Constructor in undefined" and that "The method findViewById(int) in undefined within the type FragmentTab2". Can anyone help me? This would really boost my school project. Thanks in advance!
The code of the adapter:
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class VideoAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] names;
private final String[] urls;
public VideoAdapter(Context context, String[] names, String[] urls) {
super(context, R.layout.videorow, names);
this.context = context;
this.names = names;
this.urls = urls;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.videorow, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView2);
Button button = (Button) rowView.findViewById(R.id.grootheden);
textView.setText(names[position]);
button.setTag(urls[position]);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse((String) v.getTag()), "video/mp4");
context.startActivity(intent);
}
});
return rowView;
}
}
Try this:
ListView lv = (ListView)findViewById(R.id.List);
Should be:
ListView lv = (ListView) view.findViewById(R.id.List);
EDIT:
Also, you need to return the view at the END of your onCreate();
return view;
Your code, fixed:
public class FragmentTab2 extends SherlockFragment {
private String[]names = {"Video", "Audio"};
private String[]urls = {"http://c172200.r0.cf3.rackcdn.com/104824.mp4", "http://c172200.r0.cf3.rackcdn.com/106973.mp4"};
#Override
public View onCreateView(LayoutInflater inflater1, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab2.xml
View view = inflater1.inflate(R.layout.fragmenttab2, container, false);
ListView lv = (ListView) view.findViewById(R.id.List);
VideoAdapter adapter = new VideoAdapter(this, names, urls);
lv.setAdapter(adapter);
return view;
}
}

Categories