I have the following ArrayAdapter:
public class CriteriosAdapter extends ArrayAdapter<Criterio> {
private Context context;
public CriteriosAdapter(Context context, List<Criterio> objects) {
super(context, 0, objects);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Criterio criterio = getItem(position);
final CriterioViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_row, parent, false);
viewHolder = new CriterioViewHolder();
viewHolder.txtCriterio = (TextView)convertView.findViewById(R.id.txtCriterio);
viewHolder.tgIrregular = (ToggleButton)convertView.findViewById(R.id.tgIrregular);
viewHolder.btnCam = (ImageView) convertView.findViewById(R.id.btnCam);
convertView.setTag(viewHolder);
}
else {
viewHolder = (CriterioViewHolder)convertView.getTag();
}
viewHolder.txtCriterio.setText(criterio.nome);
viewHolder.txtCriterio.setTextColor(context.getColor(R.color.white));
viewHolder.tgIrregular.setChecked(false);
viewHolder.tgIrregular.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.tgIrregular.isChecked()) {
viewHolder.btnCam.setVisibility(View.VISIBLE);
} else {
viewHolder.btnCam.setVisibility(View.INVISIBLE);
}
}
});
viewHolder.btnCam.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
FragmentActivity activity = (FragmentActivity)(context);
android.support.v4.app.FragmentManager fm = activity.getSupportFragmentManager();
FotosFragment alertDialog = new FotosFragment();
alertDialog.show(fm, "fragment_alert");
}
});
return convertView;
}
class CriterioViewHolder {
TextView txtCriterio;
ToggleButton tgIrregular;
ImageView btnCam;
}
}
Therefore, I want to set the itemID of each item as the ID stored in the database. Because, when the toggle button is setted to ON, I wish to get that value.
Could someone help on it?
public class CriteriosAdapter extends ArrayAdapter<Criterio> {
private Context context;
public CriteriosAdapter(Context context, List<Criterio> objects) {
super(context, 0, objects);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Criterio criterio = getItem(position);
final CriterioViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_row, parent, false);
viewHolder = new CriterioViewHolder();
viewHolder.txtCriterio = (TextView)convertView.findViewById(R.id.txtCriterio);
viewHolder.tgIrregular = (ToggleButton)convertView.findViewById(R.id.tgIrregular);
viewHolder.btnCam = (ImageView) convertView.findViewById(R.id.btnCam);
convertView.setTag(viewHolder);
}
else {
viewHolder = (CriterioViewHolder)convertView.getTag();
}
viewHolder.txtCriterio.setText(criterio.nome);
viewHolder.txtCriterio.setTextColor(context.getColor(R.color.white));
viewHolder.tgIrregular.setChecked(false);
viewHolder.tgIrregular.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.tgIrregular.isChecked()) {
viewHolder.btnCam.setVisibility(View.VISIBLE);
} else {
viewHolder.btnCam.setVisibility(View.INVISIBLE);
}
}
});
viewHolder.btnCam.setOnClickListener(new Listener(position,criterio));
return convertView;
}
class CriterioViewHolder {
TextView txtCriterio;
ToggleButton tgIrregular;
ImageView btnCam;
}
class Listener implements OnClickListener {
private int position;
Criterio data;
Listener(int position,Criterio data) {
this.position = position;
this.data = data;
}
#SuppressLint("UseValueOf")
#Override
public void onClick(View v) {
// you get the unique id of every item and perform your desired operation
id = data.getId();
}
}
}
Related
I dont know why my output of listview always in wrong order after I update the data in my activity.
This is how I initialize the adapter in my activity
inboxTradeAdapter=new InboxTradeAdapter(InboxTradeActivity.this, R.layout.activity_inbox_trade, tradeArrayList,loginUserId);
tradeList.setAdapter(inboxTradeAdapter);
I call
inboxTradeAdapter.notifyDataSetChanged();
after tradeArrayList is updated
This is my Adapter
public class InboxTradeAdapter extends ArrayAdapter<Trade> {
private List<Trade> tradeList;
private Context context;
private String loginUserId;
public InboxTradeAdapter(Context context, int resource, List<Trade> tradeList, String loginUserId) {
super(context, resource, tradeList);
this.context = context;
this.tradeList = tradeList;
this.loginUserId=loginUserId;
}
#Override
public boolean isEnabled(int position) {
return true;
}
#Override
public int getCount() {
return ((null != tradeList) ?
tradeList.size() : 0);
}
#Override
public Trade getItem(int position) {
return ((null != tradeList) ?
tradeList.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (null == view) {
view = layoutInflater.inflate(R.layout.grid_category, null);
}
final Trade trade = tradeList.get(position);
if (trade != null) {
final CardView tradeLayout = (CardView) view.findViewById(R.id.category_gridlayout);
final TextView tradeName = (TextView) view.findViewById(R.id.category_name);
final ImageView tradeImage = (ImageView) view.findViewById(R.id.category_icon);
tradeName.setText(trade.getNameOwn());
tradeName.setSelected(true);
tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return view;
}
}
I can confirm that the tradeArrayList order is correct in my activity but the listview display the trade in random order. I cannot find the bug, please help
I noticed your not using a holder pattern. Always utilize it for optimization.
To the issue at hand, try refactoring your code using the holder pattern. You can learn more about it online.
There is no need overriding the getItem() method too.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Trade trade = getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_category, parent, false);
holder = new ViewHolder();
holder.tradeName = (TextView) convertView.findViewById(R.id.category_name);
holder.tradeLayout = (CardView) convertView.findViewById(R.id.category_gridlayout);
holder.tradeImage = (ImageView) convertView.findViewById(R.id.category_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tradeName.setText(trade.getNameOwn());
holder.tradeName.setSelected(true);
holder.tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return convertView;
}
}
Your viewholder class code:
static class ViewHolder {
private TextView tradeName;
private TextView tradeLayout;
private ImageView tradeImage;
}
I have found the solution, create a view holder in adapter and create a setter to set the tradeList before call the notifyDataSetChanged()
This is my answer
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final Trade trade = tradeList.get(position);
Log.d("worked",position+": "+trade.getId());
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_trade, parent, false);
holder = new ViewHolder();
holder.tradeName = (TextView) convertView.findViewById(R.id.trade_name);
holder.tradeLayout = (CardView) convertView.findViewById(R.id.trade_listlayout);
holder.tradeImage = (ImageView) convertView.findViewById(R.id.trade_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (trade != null) {
holder.tradeName.setText(trade.getNameOwn());
holder.tradeName.setSelected(true);
try {
Picasso.with(context)
.load(trade.getImageOwnOne())
.error(R.drawable.ic_menu_camera)
.resize(200, 200)
.into(holder.tradeImage);
}catch(Exception e){
holder.tradeImage.setImageResource(R.drawable.ic_menu_camera);
}
holder.tradeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, DisplayTradeActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("trade", trade);
intent.putExtras(bundle);
context.startActivity(intent);
}
});
holder.tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return convertView;
}
public void setTradeList(ArrayList<Trade> tl) {
this.tradeList = tl;
}
static class ViewHolder {
private TextView tradeName;
private CardView tradeLayout;
private ImageView tradeImage;
}
Good day,
I am trying to refresh my knowledge in android programming
by creating a simple memo app with custom adapter list.
My problem is, even I call "NotifyDataSetChanged();",
the value deleted in my list is always the last one.
Though when I refresh it, it will display the correct one.
Here is my code :
public class CustomAdapter extends BaseAdapter{
List<Memo> memos;
Context context;
int [] imageId;
private static LayoutInflater inflater = null;
public CustomAdapter(MainActivity mainActivity, List<Memo> _memos) {
memos = _memos;
context = mainActivity;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return memos.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
TextView tv2;
Button deleteMemoBtn;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder mViewHolder = null;
if (convertView == null) {
mViewHolder = new Holder();
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_memo, parent, false);
mViewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
mViewHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
mViewHolder.deleteMemoBtn = (Button) convertView.findViewById(R.id.deleteMemoBtn);
mViewHolder.tv.setText(memos.get(position).getMemoTitle());
mViewHolder.tv2.setText(Integer.toString(memos.get(position).getMemoID()));
mViewHolder.tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + memos.get(position).getMemoTitle(), Toast.LENGTH_LONG).show();
}
});
mViewHolder.deleteMemoBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "deleted " + Integer.toString(position), Toast.LENGTH_LONG).show();
memos.remove(position);
notifyDataSetChanged();
}
});
}
return convertView;
}
}
Hoping someone can lighten me up why this code is not working.
Thank you!
Try this code.
public class CustomAdapter extends BaseAdapter {
List<Memo> memos;
Context context;
int[] imageId;
private LayoutInflater inflater = null;
public CustomAdapter(Context mainActivity, List<Memo> _memos) {
memos = _memos;
context = mainActivity;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return memos.size();
}
#Override
public Object getItem(int position) {
return memos.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv;
TextView tv2;
Button deleteMemoBtn;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder mViewHolder = null;
if (convertView == null) {
mViewHolder = new Holder();
convertView = inflater.inflate(R.layout.list_memo, parent, false);
mViewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
mViewHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
mViewHolder.deleteMemoBtn = (Button) convertView.findViewById(R.id.deleteMemoBtn);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (Holder) convertView.getTag();
}
mViewHolder.tv.setText(memos.get(position).getMemoTitle());
mViewHolder.tv2.setText(Integer.toString(memos.get(position).getMemoID()));
mViewHolder.tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + memos.get(position).getMemoTitle(), Toast.LENGTH_LONG).show();
}
});
mViewHolder.deleteMemoBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "deleted " + Integer.toString(position), Toast.LENGTH_LONG).show();
memos.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}
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;
}
}
UPDATE WITH VIEW HOLDER
public class DatabaseListAdapter extends BaseAdapter {
Context context;
ArrayList<DatabaseListItems> databaseList;
ArrayList<String> DeleteList = new ArrayList<>();
static class ViewHolder {
TextView tvFirstName;
TextView tvSecondName;
View row;
}
public DatabaseListAdapter(Context context, ArrayList<DatabaseListItems> list) {
this.context = context;
databaseList = list;
}
#Override
public int getCount() {
return databaseList.size();
}
#Override
public Object getItem(int position) {
return databaseList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final DatabaseListItems databaseListItems = databaseList.get(position);
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.custom_listview_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvFirstName = (TextView) rowView.findViewById(R.id.txtViewFirstName);
viewHolder.tvSecondName = (TextView) rowView.findViewById(R.id.txtViewSecondName);
viewHolder.row = rowView.findViewById(R.id.row);
rowView.setTag(viewHolder);
}
final ViewHolder holder = (ViewHolder) rowView.getTag();
holder.tvFirstName.setText(databaseListItems.getFirstName());
holder.tvSecondName.setText(databaseListItems.getSecondName());
if (databaseListItems.getFirstRow() == true) {
holder.tvSecondName.setAlpha(0.0f);
holder.tvFirstName.setTextColor(Color.parseColor("#161616"));
holder.row.setBackgroundColor(Color.parseColor("#f7f7f7"));
holder.row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,OtherActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
} else {
holder.tvSecondName.setAlpha(1.0f);
}
}
return rowView;
}
}
in my android app, i have got an custom table list, where the data will be dynamic filled.
only the first row should a an static row, which i set programmatically.
i have this function to fill my data into the list
private void showList() {
boolean FirstRow = true;
ArrayList<DatabaseListItems> databaseList = new ArrayList<>();
databaseList.clear();
String query = "SELECT * FROM " + DatabaseHelper.DATABASE_TABLE;
Cursor c1 = dbHandler.selectQuery(query);
if (c1.moveToFirst()) {
do {
DatabaseListItems databaseListItems = new DatabaseListItems();
if (c1.getPosition() == 0 && FirstRow == true) {
databaseListItems.setFirstRow(true);
FirstRow = false;
databaseListItems.setFirstName("FIRST ROW");
databaseListItems.setSecondName(null);
c1.moveToPosition(-1);
} else {
databaseListItems.setFirstRow(false);
FirstRow = false;
databaseListItems.setFirstName(c1.getString(c1.getColumnIndex(DatabaseHelper.COLUMN_FIRSTNAME)));
databaseListItems.setSecondName(c1.getString(c1.getColumnIndex(DatabaseHelper.COLUMN_SECONDNAME)));
}
databaseList.add(databaseListItems);
} while (c1.moveToNext());
}
c1.close();
DatabaseListAdapter databaseListAdapter = new DatabaseListAdapter(getActivity(), databaseList);
ListView.setAdapter(databaseListAdapter);
}
DatabaseListItems
public class DatabaseListItems {
String firstname;
String secondname;
Boolean FirstRow;
public String getFirstName() {
return firstname;
}
public void setFirstName(String first name) {
this.firstname= firstname;
}
public String getSecondName() {
return secondname;
}
public void setSecondName(String secondname) {
this.secondname = secondname;
}
public Boolean getFirstRow() {
return FirstRow;
}
public void setFirstRow(Boolean FirstRow) {
this.FirstRow = FirstRow;
}
}
DatabaseListAdapter
public class DatabaseListAdapter extends BaseAdapter {
Context context;
ArrayList<DatabaseListItems> databaseList;
ArrayList<String> DeleteList = new ArrayList<>();
public DatabaseListAdapter(Context context, ArrayList<DatabaseListItems> list) {
this.context = context;
databaseList = list;
}
#Override
public int getCount() {
return databaseList.size();
}
#Override
public Object getItem(int position) {
return databaseList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
final DatabaseListItems databaseListItems = databaseList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_listview_item, null);
}
TextView tvFirstName = (TextView) convertView.findViewById(R.id.txtViewFirstName);
tvFirstName.setText(databaseListItems.getFirstName());
TextView tvSecondName = (TextView) convertView.findViewById(R.id.txtViewSecondName);
tvSecondName.setText(databaseListItems.getSecondName());
View row = convertView.findViewById(R.id.row);
ImageView Arrow = (ImageView) convertView.findViewById(R.id.Arrow);
// First Static Row
if (databaseListItems.getFirstRow() == true) {
tvSecondName.setAlpha(0.0f);
Arrow.setAlpha(1.0f);
tvFirstName.setTextColor(Color.parseColor("#161616"));
row.setBackgroundColor(Color.parseColor("#f7f7f7"));
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(context,OtherActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
} else {
tvSecondName.setAlpha(1.0f);
Arrow.setAlpha(0.0f);
}
return convertView;
}
}
On the first view, all looks fine.
but if i scroll down and up, random rows get the background color and the onClick function , which only the first row should get.
anyone an idea what i do wrong?
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