I am trying to implement RecyclerView and CardView and I am working with json.
All my code is working fine and RecyclerView adapter is also not showing any error.
But in my main activity when i try to call adapter it is showing a error.
I took all the reference from here
Foodlist.java
public class FoodList extends RecyclerView.Adapter<FoodList.FoodViewHolder>{
private final Activity context;
LayoutInflater inflater;
public static class FoodViewHolder extends RecyclerView.ViewHolder {
TextView txt1;
TextView txt2;
TextView txt3;
TextView txt4;
CardView cv;
FoodViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
txt1 = (TextView) itemView.findViewById(R.id.txt1);
txt2 = (TextView) itemView.findViewById(R.id.txt2);
txt3 = (TextView) itemView.findViewById(R.id.txt3);
txt4 = (TextView) itemView.findViewById(R.id.txt4);
}
}
List<Food> food;
public FoodList(Activity context, List<Food> food) {
this.food = food;
inflater = context.getLayoutInflater();
this.context = context;
}
#Override
public FoodViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.examplelistview, viewGroup, false);
FoodViewHolder fvh = new FoodViewHolder(v);
return fvh;
}
#Override
public void onBindViewHolder(FoodViewHolder holder, int position) {
View view;
view = inflater.inflate(R.layout.examplelistview, null, true);
if (view == null) {
holder.txt1 = (TextView)view.findViewById(R.id.txt1);
holder.txt2 = (TextView) view.findViewById(R.id.txt2);
holder.txt3 = (TextView) view.findViewById(R.id.txt3);
holder.txt4 = (TextView) view.findViewById(R.id.txt4);
view.setTag(holder);
}
else {
holder = (FoodViewHolder) view.getTag();
}
final Food item = food.get(position);
holder.txt1.setText(item.txt1);
holder.txt2.setText(item.txt2);
holder.txt3.setText(item.txt3);
holder.txt4.setText(item.txt4);
}
#Override
public int getItemCount() {
return food.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
Food.java:
public class Food {
public String txt1, txt2, txt3, txt4;
}
FoodViewHolder.java
public class FoodViewHolder {
TextView txt1, txt2, txt3, txt4;
}
MainActivity.java
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
rv.setLayoutManager(llm);
final FoodList adapter = new FoodList(MainActivity.this,food);
rv.setAdapter(adapter);
And I want to call my adapter in json below in another function:
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setAdapter(adapter<here it is showing the error>, new DialogInterface.OnClickListener() {
}
What should i do?
Thanks in Advance
Related
I can't get the layout.xml to show all of the images, it only shows the image that I added into the android:src="blabla.jpg" and all of the list use "blabla.jpg".
I can show the text from the arraylist perfectly fine, but the problem is only the image so i tried this code
public RecyclerViewAdapter(Context mContext,ArrayList<Integer> mImageHolder,ArrayList<String> mItemHolder, ArrayList<String> mDescHolder) {
this.mImageHolder = mImageHolder;
this.mItemHolder = mItemHolder;
this.mDescHolder = mDescHolder;
this.mContext = mContext;
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mItemHolder = new ArrayList<>();
private ArrayList<String> mDescHolder = new ArrayList<>();
private ArrayList<Integer> mImageHolder = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(Context mContext,ArrayList<Integer> mImageHolder,ArrayList<String> mItemHolder, ArrayList<String> mDescHolder) {
this.mImageHolder = mImageHolder;
this.mItemHolder = mItemHolder;
this.mDescHolder = mDescHolder;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
holder.itemHolder.setText(mItemHolder.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked on: " + mItemHolder.get(position));
//ini ke intent
Intent intent = new Intent(mContext, ListDetail.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_ITEM", mItemHolder.get(position));
extras.putString("EXTRA_DESC", mDescHolder.get(position));
extras.putInt("EXTRA_IMAGE", mImageHolder.get(position));
intent.putExtras(extras);
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mItemHolder.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView itemHolder;
ImageView imageHolder;
RelativeLayout parentLayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageHolder = itemView.findViewById(R.id.ImageItem);
itemHolder = itemView.findViewById(R.id.NameItem);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
Any help would be appreciated,
In your onBindViewHolder add:
holder.imageHolder.setImageResource(image);
use the setImageresource property of imageView and get the image from the list of that position inside onBindViewHolder.
holder.imageHolder.setImageResource(mImageHolder.get(position));
I have requirement to create nested recyclerView like
RecyclerView Contains-> CardView contains-> RecyclerView.
If I click on card I'm getting child position only but I also need parent card position. How can I handle click events of both Recyclerview and CardView at same time?
MainActivity.java:
public class MainActivity extends Activity {
LinearLayout linView;
private RecyclerView main_view;
RootAdapter adapter2;
private RelativeLayout mRelativeLayout;
private Context mContext;
private Activity mActivity;
int pos = 0;
int main_pos;
private static RecyclerView.Adapter adapter;
private static RecyclerView.Adapter adapterCo;
private RecyclerView.LayoutManager layoutManager;
private static ArrayList<DataModelMain> dataMain;
private static ArrayList<DataModel_CO> dataCo;
static View.OnClickListener myOnClickListener;
static View.OnClickListener onClickListener;
private static ArrayList<Integer> removedItems;
TestMain mainData = new TestMain();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prof = (ImageView) findViewById(R.id.profile);
main_view = (RecyclerView) findViewById(R.id.recycler_main);
mRelativeLayout = (RelativeLayout) findViewById(R.id.partners);
cont = (TextView) findViewById(R.id.mobile);
iconType = (ImageView) findViewById(R.id.icon_type);
linView = (LinearLayout) findViewById(R.id.layout_view);
// Get the application context
mContext = getApplicationContext();
// Get the activity
mActivity = MainActivity.this;
myOnClickListener = new MyOnClickListener(this);
onClickListener = new OnClickListener(this);
main_view.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(
this,
LinearLayoutManager.VERTICAL,
false
) {
};
main_view.setLayoutManager(layoutManager);
main_view.setItemAnimator(new DefaultItemAnimator());
dataMain = new ArrayList<DataModelMain>();
getMainData();
setMainView();
}
}
private void getMainData() {
for (int i = 0; i < mainData.passengers.length; i++) {
dataMain.add(new DataModelMain(
mainData.type[i],
mainData.passengers[i],
mainData.p_name[i],
mainData.p_contact[i],
mainData.p_flight[i],
mainData.p_pnr[i],
mainData.air[i],
mainData.p_time[i]
));
}
adapter2 = new RootAdapter(this, dataMain);
main_view.setAdapter(adapter2);
}
private class MyOnClickListener implements View.OnClickListener {
private final Context context;
private MyOnClickListener(Context context) {
this.context = context;
}
#Override
public void onClick(View v) {
showIcons(v);
}
private void showIcons(View v) {
pos = main_view.getChildPosition(v);
setMainView();
}
}
private class OnClickListener implements View.OnClickListener {
private final Context context1;
private OnClickListener(Context context0) {
this.context1 = context0;
}
#Override
public void onClick(View v) {
showIcons(v);
}
private void showIcons(View v) {
pos = main_view.getChildPosition(v);
Log.e("CLICKED","pos: "+pos);
}
}
private class RootAdapter extends RecyclerView.Adapter<RootAdapter.RootViewHolder> {
private final LayoutInflater inflater;
String[] _items = new String[]{"GROUP 1", "GROUP 2", "GROUP 3", "GROUP 4"};
private ArrayList<DataModelMain> dataSet;
public RootAdapter(Context context,ArrayList<DataModelMain> data) {
inflater = LayoutInflater.from(context);
this.dataSet = data;
}
#Override
public RootViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.card_main, viewGroup, false);
RootViewHolder rvi = new RootViewHolder(view);
view.setOnClickListener(MainActivity.onClickListener);
return rvi;
}
#Override
public void onBindViewHolder(RootViewHolder rootViewHolder, int i) {
rootViewHolder.recyclerViewChild.setLayoutManager(new LinearLayoutManager(inflater.getContext()));
rootViewHolder.recyclerViewChild.setAdapter(new ChildAdapter(inflater,dataSet));
}
#Override
public int getItemCount() {
return _items.length;
}
class RootViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerViewChild;
public RootViewHolder(View itemView) {
super(itemView);
recyclerViewChild = (RecyclerView) itemView.findViewById(R.id.recycler_partners);
}
}
}
private class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ChildViewHolder> {
private LayoutInflater _inflater;
private ArrayList<DataModelMain> dataSet;
public ChildAdapter(LayoutInflater inflater,ArrayList<DataModelMain> data) {
_inflater = inflater;
this.dataSet = data;
}
#Override
public ChildViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = _inflater.inflate(R.layout.card_partner, viewGroup, false);
ChildViewHolder rvi = new ChildViewHolder(view);
view.setOnClickListener(MainActivity.myOnClickListener);
return rvi;
}
#Override
public void onBindViewHolder(ChildViewHolder holder, int listPosition) {
TextView name = holder.tvName;
TextView cont = holder.tvCont;
TextView flight = holder.tvFlight;
TextView pnr = holder.tvPNR;
TextView time = holder.tvTime;
ImageView type = holder.im_type;
ImageView pic = holder.im_pic;
ImageView logo = holder.im_logo;
CardView cardView = holder.card;
name.setText(dataSet.get(listPosition).getName());
cont.setText(dataSet.get(listPosition).getContact());
flight.setText(dataSet.get(listPosition).getFlight());
pnr.setText(dataSet.get(listPosition).getPnr());
time.setText(dataSet.get(listPosition).getTime());
type.setImageResource(dataSet.get(listPosition).getType());
pic.setImageResource(dataSet.get(listPosition).getImage());
logo.setImageResource(dataSet.get(listPosition).getLogo());
switch (dataSet.get(listPosition).getType()){
case R.mipmap.gov:
cardView.setCardBackgroundColor(Color.parseColor("#FAFAD2"));
break;
case R.mipmap.vip:
cardView.setCardBackgroundColor(Color.parseColor("#CDFFCD"));
break;
case R.mipmap.jail:
cardView.setCardBackgroundColor(Color.parseColor("#FFE4E1"));
break;
case R.mipmap.ban:
cardView.setCardBackgroundColor(Color.parseColor("#DCDCDC"));
break;
}
}
#Override
public int getItemCount() {
return dataSet.size();
}
public class ChildViewHolder extends RecyclerView.ViewHolder {
ImageView im_type,im_pic,im_logo;
TextView tvName,tvCont,tvFlight,tvPNR,tvTime;
CardView card;
public ChildViewHolder(View itemView) {
super(itemView);
this.card = (CardView) itemView.findViewById(R.id.card_view);
this.im_type = (ImageView) itemView.findViewById(R.id.p_type);
this.im_pic = (ImageView) itemView.findViewById(R.id.p_profile);
this.im_logo = (ImageView) itemView.findViewById(R.id.p_air_lines);
this.tvName = (TextView) itemView.findViewById(R.id.p_name);
this.tvCont = (TextView) itemView.findViewById(R.id.p_mob);
this.tvFlight = (TextView) itemView.findViewById(R.id.p_flight);
this.tvPNR = (TextView) itemView.findViewById(R.id.p_pnr);
this.tvTime = (TextView) itemView.findViewById(R.id.p_timer);
}
}
}
}
How about making the root/parent cardView clickable??
Give an id to the cardView item of the parent recyclerView. Place it in your ViewHolderClass and then assign a onCLickListener to the cardView in BindViewHolder like this:
rootViewHolder.rootCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// keep an track of the selected position of the cardView and store it in a global/local variable
}
});
you can track the adapter position of the selected cardView using
rootViewHolder.getAdapterPosition()
Note: rootViewHolder is written as per your ViewHolder class
I hope this helps !!
so I was trying to replace my listview with a recyclerview in order to improve my app,
now my activity doesnt show the list.
I was checking the adapter and I couldn't find any mistake
This was my ListView Adapter:
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<Salary> salaryList;
public CustomAdapter(Context context, List<Salary> salaryList) {
this.salaryList = salaryList;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_card, parent, false);
holder = new ViewHolder();
holder.fieldName = (TextView) view.findViewById(R.id.fieldName);
holder.txtSalary = (TextView) view.findViewById(R.id.txtSalary);
holder.cardview = (CardView) view.findViewById(R.id.cardview);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Salary salary = salaryList.get(position);
holder.salary = salary;
holder.cardview.setTag(salary);
holder.fieldName.setText(salary.getField());
holder.txtSalary.setText(salary.getSalary());
holder.cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Salary salary = (Salary) v.getTag();
Intent i = new Intent(context, ViewSalary.class);
i.putExtra(ViewSalary.FIELD_NAME, salary.getField());
i.putExtra(ViewSalary.SALARY_NUM, salary.getSalary())
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
});
public static class ViewHolder {
Salary salary;
TextView fieldName;
TextView txtSalary;
CardView cardview;
}
}
and now here is my new RecyclerView Adapter:
public class CustomAdapter extends RecyclerView.Adapter {
private List<Salary> salaryList;
public CustomAdapter(List<Salary> salaryList) {
this.salaryList = new ArrayList<Salary>();
this.salaryList.addAll(salaryList);
}
#Override
public void onBindViewHolder(SalaryViewHolder salaryViewHolder, int i) {
Salary salary = salaryList.get(i);
salaryViewHolder.fieldName.setText(salary.getField());
salaryViewHolder.txtSalary.setText(salary.getSalary());
salaryViewHolder.salary = salary;
salaryViewHolder.cardview.setTag(salary);
}
#Override
public SalaryViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.item_card, viewGroup, false);
return new SalaryViewHolder(itemView);
}
public class SalaryViewHolder extends RecyclerView.ViewHolder{
protected Salary salary;
protected TextView fieldName;
protected TextView txtSalary;
protected CardView cardview;
public SalaryViewHolder(View view) {
super(view);
fieldName = (TextView) view.findViewById(R.id.fieldName);
txtSalary = (TextView) view.findViewById(R.id.txtSalary);
cardview = (CardView) view;
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Salary salary = (Salary) v.getTag();
Intent i = new Intent(context, ViewSalary.class);
i.putExtra(ViewSalary.FIELD_NAME, salary.getField());
i.putExtra(ViewSalary.SALARY_NUM, salary.getSalary());;
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
});
}
}
}
any ideas??
I need to create a RecyclerView inside a custom view. Following my code:
public class CitySinglePageGridView extends LinearLayout {
Context context ;
View mView ;
RecyclerView recyclerView ;
RecyclerView.LayoutManager layoutManager ;
RecyclerView.Adapter adapter;
public CitySinglePageGridView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context ;
LayoutInflater inflater;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.module_city_single_page_gridview,this,true);
recyclerView = (RecyclerView) mView.findViewById(R.id.cardList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
adapter = new CityCardviewAdapter();
recyclerView.setAdapter(adapter);
}
}
The problem is when I run the application nothing shows in the custom view, but when I'm using this RecyclerView in a Fragment or Activity everything works fine.
Following my custom adapter:
public class CityCardviewAdapter extends RecyclerView.Adapter<CityCardviewAdapter.ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_card_item_city, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return 5;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
protected TextView title;
protected TextView desc ;
protected TextView distance ;
protected TextView likeCount ;
protected ImageView thumbnail ;
protected ImageView like ;
protected ImageView Distance ;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
desc = (TextView) itemView.findViewById(R.id.desc);
distance = (TextView) itemView.findViewById(R.id.distance);
likeCount = (TextView) itemView.findViewById(R.id.likeCount);
thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
like = (ImageView) itemView.findViewById(R.id.like);
Distance = (ImageView) itemView.findViewById(R.id.nav);
}
}
}
I set getItemCount to 5 to test the application.
i have a custom listview with some textviews and a button. On button click i want to save the listview values to server, to do this task i'm using asyntask but my code crashed on pregress dialog. i'm having problem how to pass activity's instance for using progress dialog
public class ListViewAdapter extends BaseAdapter{
Context context;
public arrayList<Planning> arraylist;
private static LayoutInflater inflater = null;
public PlanningList_activity planActivity;
public ListView mListview;
Db_sqlite databaseHelper;
public ListViewAdapter(Context context,ArrayList<Plan> arraylist) {
this.context = context;
this.planArraylist = plan0Arraylist;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
databaseHelper = new Db_sqlite(context);
}
#Override
public int getCount() {
return planArraylist.size();
}
#Override
public Planning getItem(int arg0) {
return planArraylist.get(arg0);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_lv_item, parent, false);
holder = new ViewHolder();
holder.tvdrCode = (TextView convertView.findViewById(R.id.Plan_no);
holder.tvDrName = (TextView) convertView.findViewById(R.id.doc_name);
holder.tvterrcode= (TextView) convertView.findViewById(R.id.mon);
holder.tvAdr = (TextView) convertView.findViewById(R.id.adr);
holder.tvClassDesc = (TextView) convertView.findViewById(R.id.class_desc);
holder.tvspeciality = (TextView) convertView.findViewById(R.id.ff_code);
holder.tvAreaName = (TextView) convertView.findViewById(R.id.area_name);
holder.tvShift = (TextView) convertView.findViewById(R.id.shift);
holder.btn_visited =(Button)convertView.findViewById(R.id.button1);
holder.btn_visited.setTag(position);
holder.btn_visited.setOnClickListener(ButtonClickListener);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Planning p = planArraylist.get(position);
holder.tvdrCode.setText(p.getDocCode());
holder.tvDrName.setText(p.getDocName());
holder.tvterrcode.setText(p.getTerrCode());
holder.tvAdr.setText(p.getAdr());
holder.tvClassDesc.setText(p.getClassDesc());
holder.tvspeciality.setText(p.getSP());
holder.tvAreaName.setText(p.getAreaName());
holder.tvShift.setText(p.getMornEven());
return convertView;
}
static class ViewHolder {
TextView tvdrCode;
TextView tvDrName;
TextView tvterrcode;
TextView tvAdr;
TextView tvClassDesc;
TextView tvspeciality;
TextView tvAreaName;
TextView tvShift;
Button btn_visited;
}
Probably context is null . use mContext which you are passing in SaveDCR class constructor for creating progress bar:
#Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Saving...");
progressDialog.setCancelable(false);
progressDialog.show();
Log.e( "sending dvr to sqlite","yes");
}
And from Button click pass context as:
new SaveDCR(v.getContext()).execute(dvr);