I have a listView (vertical) and every list item has a horizontal list view (horizontal).
But the problem is when i scroll the horizontal scrollview in the row the vertical list is also calling getView()...
So, there is a huge performance hit..
So , can any one tell me a better solution to it ..
public class GridViewAdapter extends BaseAdapter {
List<List<Hotel>> gridDatasource;
Context mContext;
public GridViewAdapter(List<List<Hotel>> gridDatasource, Context context) {
this.gridDatasource = gridDatasource;
this.mContext = context;
}
public void setGridDatasource(List<List<Hotel>> gridDatasource) {
this.gridDatasource = gridDatasource;
}
#Override
public int getCount() {
if (gridDatasource == null) {
return 0;
}
return gridDatasource.size();
}
#Override
public Object getItem(int position) {
return gridDatasource.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_table_cell,
null);
holder = new GridViewHolder();
holder.headerView = (TextView) convertView
.findViewById(R.id.gridViewRowHeader);
holder.listView = (HorizontalListView) convertView
.findViewById(R.id.gridViewHorizontalListView);
convertView.setTag(holder);
} else {
holder = (GridViewHolder) convertView.getTag();
Log.d("TAG", "Reaching Here");
}
holder.headerView.setText("Hello From Sandeep");
HorizontalListViewAdapter adapter = new HorizontalListViewAdapter(
mContext, gridDatasource.get(position));
holder.listView.setAdapter(adapter);
return convertView;
}
}
static class GridViewHolder {
TextView headerView;
HorizontalListView listView;
}
public class HorizontalListViewAdapter extends BaseAdapter {
Context mContext;
List<Hotel> mHotels;
public HorizontalListViewAdapter(Context context, List<Hotel> hotels) {
this.mContext = context;
this.mHotels = hotels;
}
#Override
public int getCount() {
if (mHotels == null) {
return 0;
}
return mHotels.size();
}
#Override
public Object getItem(int position) {
return mHotels.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HotelCell cell = (HotelCell) convertView;
if (cell == null) {
cell = new HotelCell(mContext);
} else {
Log.d("TAG", "Reached here 2");
}
cell.setHotel(mHotels.get(position));
cell.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
HotelDetailActivity.class);
intent.putExtra("DATA", ((HotelCell) v).getHotel());
startActivity(intent);
}
});
cell.setPadding(0, 0, 10, 0);
return cell;
}
}
Dear i suggest to try My this Code
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder;
if (v == null)
{
v = inflater.inflate(R.layout.custom_image_layout, null);
holder = new ViewHolder();
holder.txtFileName = (TextView) v.findViewById(R.id.txtFileName);
holder.imageView = (ImageView) v.findViewById(R.id.imgView);
v.setTag(holder);
} else
{
holder = (ViewHolder) v.getTag();
}
holder.imageView.setImageBitmap(bm);
holder.txtFileName.setText(""+nameoffile);
return v;
}
static class ViewHolder
{
public ImageView imageView;
public TextView txtFileName;
}
Use The Holder Class
Related
when i was trying tutorial checkbox in listview theres some error on my adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewTag viewTag;
if (convertView == null) {
//取得listItem容器 view
convertView = myInflater.inflate(R.layout.list_item_pegawai, null);
//建構listItem內容view
viewTag = new ViewTag(
(TextView) convertView.findViewById(R.id.txt_namapegawai),
(CheckBox) convertView.findViewById(R.id.checkBox_pegawai));
//設置容器內容
convertView.setTag(viewTag);
} else {
viewTag = (ViewTag) convertView.getTag();
}
complete code
public class CustomAdapter extends BaseAdapter {
private Context context;
public static ArrayList<Model> modelArrayList;
public CustomAdapter(Context context, ArrayList<Model> modelArrayList) {
this.context = context;
CustomAdapter.modelArrayList = modelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return modelArrayList.size();
}
#Override
public Object getItem(int position) {
return modelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_select, null, true);
holder.checkBox = convertView.findViewById(R.id.checkBox);
holder.tvName = convertView.findViewById(R.id.textView7);
holder.Tvnumber = convertView.findViewById(R.id.textView8);
convertView.setTag(holder);
} else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(modelArrayList.get(position).getName());
holder.Tvnumber.setText(modelArrayList.get(position).getPhone());
holder.checkBox.setChecked(modelArrayList.get(position).getSelected());
holder.checkBox.setTag(R.integer.btnplusview, convertView);
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View tempview = (View) holder.checkBox.getTag(R.integer.btnplusview);
TextView tv = tempview.findViewById(R.id.textView7);
Integer pos = (Integer) holder.checkBox.getTag();
if (modelArrayList.get(pos).getSelected()) {
modelArrayList.get(pos).setSelected(false);
} else {
modelArrayList.get(pos).setSelected(true);
}
}
});
return convertView;
}
private class ViewHolder {
protected CheckBox checkBox;
private TextView tvName;
private TextView Tvnumber;
}
}
I have GridView which contain the number of items, If user click on save button then selected value show in ListView, I tried but not got succeied, Can any one help to solve this problem?
MainActivity.java
fillDataMyProperty();
if (myPropertyArrayList != null && myPropertyArrayList.size() > 0) {
myPropertListAdapter = new MyPropertListAdapter(MainActivity.this, myPropertyArrayList, txtSubmit);
gridMyProperty.setAdapter(myPropertListAdapter);
}
My GridAdapter is
public class MyPropertListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ItemModel> arrayList;
private boolean check = false;
private View view;
private TextView txtSubmit;
public MyPropertListAdapter(Context context, ArrayList<ItemModel> arrayList, TextView txtSubmit) {
this.context = context;
this.arrayList = arrayList;
this.txtSubmit = txtSubmit;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public int getViewTypeCount() {
return super.getViewTypeCount();
}
#SuppressLint("InflateParams")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item_row, null);
viewHolder = new ViewHolder();
viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
viewHolder.rlMain = (RelativeLayout) convertView.findViewById(R.id.rlMain);
viewHolder.imgCheck = (ImageView) convertView.findViewById(R.id.imgCheck);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txtItem.setText(arrayList.get(position).getItem_name());
if (arrayList.get(position).isCheck() == true) {
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_green_facility);
Glide.with(context).load(R.drawable.ic_facility_check).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(viewHolder.imgCheck);
} else if (arrayList.get(position).isCheck() == false) {
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_white_facility);
viewHolder.imgCheck.setVisibility(View.GONE);
}
viewHolder.rlMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view = v;
if (arrayList.get(position).isCheck() == false) {
Log.d("if---------------------", String.valueOf(arrayList.get(position).isCheck()));
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_green_facility);
viewHolder.imgCheck.setVisibility(View.VISIBLE);
Glide.with(context).load(R.drawable.ic_facility_check).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(viewHolder.imgCheck);
arrayList.get(position).setCheck(true);
check = true;
} else if (arrayList.get(position).isCheck() == true) {
Log.d("else-------------------", String.valueOf(arrayList.get(position).isCheck()));
viewHolder.txtItem.setBackgroundResource(R.drawable.ic_white_facility);
viewHolder.imgCheck.setVisibility(View.GONE);
arrayList.get(position).setCheck(false);
check = false;
}
}
});
txtSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showItemsDilaog();
}
});
return convertView;
}
public class ViewHolder {
TextView txtItem;
RelativeLayout rlMain;
ImageView imgCheck;
}
private void showItemsDilaog() {
final Dialog mDialog = new Dialog(context);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mDialog.setContentView(R.layout.dialog_list_item);
ImageView imgCancel = (ImageView) mDialog.findViewById(R.id.imgCancel);
ListView listItem = (ListView) mDialog.findViewById(R.id.listItem);
if (arrayList != null && arrayList.size() > 0) {
ItemsAdapter mAdapter = new ItemsAdapter(context, arrayList);
listItem.setAdapter(mAdapter);
}
imgCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
}}
ItemsAdapter.Java
class ItemsAdapter extends BaseAdapter {
// private String[] mStrings = new String[]{"Elevator", "Air Conditionor", "Electricity", "Houe Keeping", "Garden", "All"};
private ViewHolder viewHolder;
private Context context;
private ArrayList<ItemModel> mArrayList;
public ItemsAdapter(Context context, ArrayList<ItemModel> maintananceModelArrayList) {
this.context = context;
this.mArrayList = maintananceModelArrayList;
}
#Override
public int getCount() {
return mArrayList.size();
}
#Override
public Object getItem(int position) {
return mArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_row, null);
viewHolder = new ViewHolder();
viewHolder.txtItem = (TextView) convertView.findViewById(R.id.txtItem);
viewHolder.lineSeperator = (View) convertView.findViewById(R.id.lineSeperator);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (arrayList.get(position).isCheck() == true) {
viewHolder.txtItem.setText(arrayList.get(position).getItem_name());
Log.d("Items:::::::::::::::",arrayList.get(position).getItem_name());
}
if (position == arrayList.size() - 1) {
viewHolder.lineSeperator.setVisibility(View.GONE);
} else {
viewHolder.lineSeperator.setVisibility(View.VISIBLE);
}
return convertView;
}
public class ViewHolder {
TextView txtItem;
View lineSeperator;
}
}
I am working on tinder swipe cards library.So I referred this Sample.
My issue is, while swiping image I need to get the swiped image position.So far I tried like int idGet = flingContainer.getSelectedItemPosition(); But it is not working for me.
Below I have posted relevant code :
Logcat:
E:/ idGet : -1
CardsFragment.java:
public class CardsFragment extends Fragment {
private SwipeFlingAdapterView flingContainer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
flingContainer = (SwipeFlingAdapterView) rootView.findViewById(R.id.frame);
likeImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
likeNope();
flipMethodRight(80.00f);
}
});
}
}
void flipMethodRight(float scrollProgressPercent) {
flingContainer.getTopCardListener().selectRight();
View view = flingContainer.getSelectedView();
view.findViewById(R.id.background).setAlpha(0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
public class MyAppAdapter extends BaseAdapter {
public List<GetImageData> parkingList;
public Context context;
private MyAppAdapter(List<GetImageData> apps, Context context) {
this.parkingList = apps;
this.context = context;
}
#Override
public int getCount() {
return parkingList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater =getActivity(). getLayoutInflater();
rowView = inflater.inflate(R.layout.adapt_card_frag, parent, false);
Log.e("AdapterCalling", "AdapterCalling");
// configure view holder
viewHolder = new ViewHolder();
viewHolder.DataText = (TextView) rowView.findViewById(R.id.bookText);
viewHolder.background = (FrameLayout) rowView.findViewById(R.id.background);
viewHolder.cardImage = (ImageView) rowView.findViewById(R.id.cardImage);
Log.e("ParkingList", "" +parkingList.get(position).getDescription() + "");
int idGet = flingContainer.getSelectedItemPosition();
Log.e("idGet", ""+idGet);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.DataText.setText(parkingList.get(position).getDescription() + "");
Glide.with(CardsFragment.this).load(parkingList.get(position).getImagePath()).into(viewHolder.cardImage);
return rowView;
}
}
I solved this issue with the help of below code:
String getImageFlingStr, GET_PRESENT_IMAGE;
getImageFlingStr = nameImgArrList.get(0).getImagePath();
presentImageShowing = getImageFlingStr;
GET_PRESENT_IMAGE = presentImageShowing;
Log.e("presentImageShowing", ""+presentImageShowing);
Is it possible to change row view when I'm clicking on it? For example I have listView with rows where some short information shown, and when after clicking on row detailed view will show instead of old row.
I tried to use addView in my listView, but "listview addView(View, int) is not supported in AdapterView" error appeared.
try something like that:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<UserPojo> mList;
public MyAdapter(Context context, List<UserPojo> users) {
this.mContext = context;
this.mList = users;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public UserPojo getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final UserPojo user = mList.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_ad, null);
holder = new ViewHolder();
holder.pic = (CircleImageView) convertView.findViewById(R.id.profile_imageView);
holder.name = (TextView) convertView.findViewById(R.id.name_textView);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
if (user.isClicked) {
holder.pic.setVisibility(View.VISIBLE);
holder.name.setVisibility(View.GONE);
} else {
holder.pic.setVisibility(View.GONE);
holder.name.setVisibility(View.VISIBLE);
}
holder.name.setText("text");
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user.setClicked(true);
notifyDataSetChanged();
}
});
return convertView;
}
private static class ViewHolder {
CircleImageView pic;
TextView name;
}
}
Main Adapter
public class NewTreatmentAdapter extends BaseAdapter {
Context mContext;
LayoutInflater inflater;
ArrayList<MyTreatmentDetailNew> list;
public NewTreatmentAdapter(Context context, ArrayList<MyTreatmentDetailNew> list) {
this.mContext = context;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyTreatmentDetailNew allergiesDetails = (MyTreatmentDetailNew) list.get(position);
Holders holders;
View view = convertView;
if (convertView == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.new_my_treat_list, parent, false);
holders = new Holders();
holders.txtFrom = (TextView) view.findViewById(R.id.txtFrom);
holders.txtTreatmnt = (TextView) view.findViewById(R.id.txtTreatmnt);
holders.txtStatus = (TextView) view.findViewById(R.id.txtStatus);
holders.lView = (ListView) view.findViewById(R.id.lView);
holders.txtFrom.setTag(list.get(position));
holders.txtTreatmnt.setTag(list.get(position));
holders.txtStatus.setTag(list.get(position));
holders.lView.setTag(list.get(position));
view.setTag(holders);
} else {
holders = (Holders) view.getTag();
((Holders) view.getTag()).txtFrom.setTag(list.get(position));
((Holders) view.getTag()).txtTreatmnt.setTag(list.get(position));
((Holders) view.getTag()).txtStatus.setTag(list.get(position));
((Holders) view.getTag()).lView.setTag(list.get(position));
}
holders.txtFrom.setText(allergiesDetails.fromName);
holders.txtTreatmnt.setText(allergiesDetails.treatmntName);
holders.txtStatus.setText(allergiesDetails.status);
ArrayList<MyTreatmentSubDetailNew> subDetailList = allergiesDetails.subDetailList;
if(subDetailList!=null && subDetailList.size()!=0) {
NewTreatmentSubAdapter subAdapter = new NewTreatmentSubAdapter(mContext, subDetailList);
holders.lView.setAdapter(subAdapter);
//((ViewGroup)convertView).addView(holders.lView);
//holders.lView.setTag(allergiesDetails);
}
return view;
}
public class Holders {
TextView txtFrom, txtTreatmnt, txtStatus, txtDay, txtMg, txtOften, txtName;
ListView lView;
} }
SubAdapter
public class NewTreatmentSubAdapter extends BaseAdapter {
Context mContext;
LayoutInflater inflater;
ArrayList<MyTreatmentSubDetailNew> list;
public NewTreatmentSubAdapter(Context context, ArrayList<MyTreatmentSubDetailNew> list) {
this.mContext = context;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyTreatmentSubDetailNew allergiesDetails = (MyTreatmentSubDetailNew) list.get(position);
System.out.println("sub position >> "+position);
Holders holders;
View view = convertView;
if (convertView == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// view = inflater.inflate(R.layout.new_my_treat_sub_list, parent, false);
view = inflater.inflate(R.layout.new_my_treat_sub_list, null);
holders = new Holders();
holders.txtDay = (TextView) view.findViewById(R.id.txtDay);
holders.txtMg = (TextView) view.findViewById(R.id.txtMg);
holders.txtOften = (TextView) view.findViewById(R.id.txtOften);
holders.txtName = (TextView) view.findViewById(R.id.txtName);
holders.txtDay.setTag(list.get(position));
holders.txtMg.setTag(list.get(position));
holders.txtOften.setTag(list.get(position));
holders.txtName.setTag(list.get(position));
view.setTag(holders);
} else {
holders = (Holders) view.getTag();
((Holders) view.getTag()).txtDay.setTag(list.get(position));
((Holders) view.getTag()).txtMg.setTag(list.get(position));
((Holders) view.getTag()).txtName.setTag(list.get(position));
((Holders) view.getTag()).txtOften.setTag(list.get(position));
}
holders.txtDay = (TextView) view.findViewById(R.id.txtDay);
holders.txtMg = (TextView) view.findViewById(R.id.txtMg);
holders.txtOften = (TextView) view.findViewById(R.id.txtOften);
holders.txtName = (TextView) view.findViewById(R.id.txtName);
holders.txtDay.setText("" + allergiesDetails.Days);
holders.txtMg.setText(allergiesDetails.Dosage);
holders.txtName.setText(allergiesDetails.MedicineName);
holders.txtOften.setText(allergiesDetails.AsNeed + "," + allergiesDetails.Morning + "," + allergiesDetails.AfterNoon + "," + allergiesDetails.Night);
// ((ViewHolder) view.getTag()).edittextComments.setTag(arrayListIndividualEvaluationSectionsService.get(position));
return view;
}
public class Holders {
TextView txtFrom, txtTreatmnt, txtStatus, txtDay, txtMg, txtOften, txtName;
ListView lView;
}
/*
public Object getItem(int position) {
for (Organization org : orgnizations) {
if (position == 0) {
return org;
}
position--;
List<Contact> contacts = org.getContacts();
if (contacts != null) {
int size = contacts.size();
if (position >= size) {
position -= size;
} else {
return contacts.get(position);
}
}
}*/
}