I'm trying to do a custom listview with a favorite option, the thing is that when I add some items to favorite and then scroll down the fav button change order randomly, the other thing that I need help with is that I don't know how to change a favorite imageview when other is clicked (Because You can only have 1 favorite option)
This is my custom adapter
public class LineasAdapter extends BaseAdapter {
Context context;
protected List<Lineas> lineas;
LayoutInflater inflater;
public LineasAdapter(Context context, List<Lineas> lista){
this.context = context;
this.inflater = LayoutInflater.from(context);
this.lineas = lista;
}
#Override
public int getCount() {
return lineas.size();
}
#Override
public Object getItem(int position) {
return lineas.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.item_lista,
parent, false);
holder.numero = (TextView) convertView
.findViewById(R.id.number);
holder.titulo = (TextView) convertView
.findViewById(R.id.titulo);
holder.subtitulo = (TextView) convertView
.findViewById(R.id.subtitulo);
holder.star = (ImageView) convertView
.findViewById(R.id.star);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Lineas linea = lineas.get(position);
holder.numero.setText(linea.numero_s());
holder.titulo.setText(linea.titulo());
holder.subtitulo.setText(linea.subtitulo());
final SharedPreferences.Editor editor = convertView.getContext().getSharedPreferences("Favorito",0).edit();
final int numero = linea.numero();
final Context c = convertView.getContext();
final ImageView estrella = holder.star;
/** this is where I change my fav button and add the number to some sharedpreferences **/
holder.star.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editor.putInt("linea", numero );
editor.commit();
Toast.makeText(c,"Has agregado a favoritos a el cole numero "+String.valueOf(numero), Toast.LENGTH_SHORT ).show();
estrella.setImageResource(R.drawable.star);
}
});
return convertView;
}
private class ViewHolder {
TextView numero;
TextView titulo;
TextView subtitulo;
ImageView star;
}
}
replace getview method with follwing ...
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
holder = new ViewHolder();
convertView = this.inflater.inflate(R.layout.item_lista,parent, false);
holder.numero = (TextView) convertView.findViewById(R.id.number);
holder.titulo = (TextView) convertView.findViewById(R.id.titulo);
holder.subtitulo = (TextView) convertView.findViewById(R.id.subtitulo);
holder.star = (ImageView) convertView.findViewById(R.id.star);
convertView.setTag(holder);
Lineas linea = lineas.get(position);
holder.numero.setText(linea.numero_s());
holder.titulo.setText(linea.titulo());
holder.subtitulo.setText(linea.subtitulo());
final SharedPreferences.Editor editor = convertView.getContext().getSharedPreferences("Favorito",0).edit();
final int numero = linea.numero();
final Context c = convertView.getContext();
final ImageView estrella = holder.star;
/** this is where I change my fav button and add the number to some sharedpreferences **/
holder.star.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editor.putInt("linea", numero );
editor.commit();
Toast.makeText(c,"Has agregado a favoritos a el cole numero "+String.valueOf(numero), Toast.LENGTH_SHORT ).show();
estrella.setImageResource(R.drawable.star);
}
});
return convertView;
}
Related
I have a custom listview with a button to add more elements
but when I add and element the app crash, but when I restart I find that the element is added, (rarely it doesn't crash)
And i
I use custom adapter
class CustomAdapter extends BaseAdapter {
ArrayList<ListItem> listItems = new ArrayList<ListItem>();
CustomAdapter(ArrayList<ListItem> list){
this.listItems = list;
}
#Override
public int getCount() {
return listItems.size();
}
#Override
public Object getItem(int position) {
return listItems.get(position).name;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int i, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.list_item,null);
TextView name = (TextView) view.findViewById(R.id.name);
TextView lastm = (TextView) view.findViewById(R.id.last);
TextView time = (TextView) view.findViewById(R.id.time);
CircleImageView pic= (CircleImageView) view.findViewById(R.id.pic);
name.setText(listItems.get(i).name);
lastm.setText(listItems.get(i).lastm);
time.setText(listItems.get(i).time);
Bitmap bmp = BitmapFactory.decodeByteArray(listItems.get(i).pic,0,listItems.get(i).pic.length);
pic.setImageBitmap(bmp);
return view;
}
}
and when I add an element the app crashs
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText editText=(EditText) mView.findViewById(R.id.name);
String name=editText.getText().toString();
boolean result=myDataBase.insertData(imageViewToByte(img),name,"no messages yet","");
if (result) {
Toast.makeText(Main2Activity.this, "Saved in DATABASE", Toast.LENGTH_SHORT).show();
viewLastData();
dialog.dismiss();
}
If your ListView lags it's because you used wrap_content for the listView's layout_height. It forces your ListView to count all the items inside and it has a huge performance impact.
So replace wrap_content by match_parent to avoid those lags.
EDIT: Use a ViewHolder pattern too in your Adapter, see this link.
Here is an example:
// ViewHolder Pattern
private static class ViewHolder {
TextView name, status;
}
#Override #NonNull
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = LayoutInflater.from(getContext());
convertView = vi.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.text_name);
holder.status = (TextView) convertView.findViewById(R.id.another_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Then do the other stuff with your item
// to populate your listView
// ...
return convertView
}
For my list view on tablets, I'm trying to get my selected list item selection to keep its state when selected but unfortunately I'm seeing some weird behaviour. For some reason whenever I scroll through the list to the point where the selected item is not visible and then scroll back to the point where the selected item IS visible, the background colour unexpectedly gets reused. I believe something needs to go in the getView method but I'm not sure what to do with this method. What must be done in order to prevent the background colour from being reused?
Adapter class
public class VictoriaListAdapter extends BaseAdapter {
private List<Victoria> mData;
private LayoutInflater mInflater;
public VictoriaListAdapter (List<Victoria> data, Context context) {
mData = data;
mData = new ArrayList(mData);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position).getStation();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_dualline, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.item_station);
holder.description = (TextView) convertView.findViewById(R.id.item_zone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(mData.get(position).getStation());
holder.description.setText(mData.get(position).getZone());
return convertView;
}
/**
* View holder
*/
static class ViewHolder {
private TextView title;
private TextView description;
}
}
Fragment class
public class FragmentVictoriaLine extends ListFragment {
private VictoriaListAdapter mAdapter;
public FragmentVictoriaLine() {
}
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
public boolean mTwoPane;
public static FragmentVictoriaLine newInstance() {
return new FragmentVictoriaLine();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_victoria_line, container, false);
initialize();
return view;
}
List<Victoria> list = new ArrayList<>();
private void initialize() {
String[] items = getActivity().getResources().getStringArray(R.array.victoria_stations);
String[] itemDescriptions = getActivity().getResources().getStringArray(R.array.victoria_zones);
for (int n = 0; n < items.length; n++){
Victoria victoria = new Victoria();
victoria.setID();
victoria.setStation(items[n]);
victoria.setZone(itemDescriptions[n]);
list.add(victoria);
}
mAdapter = new VictoriaListAdapter(list, getActivity());
setListAdapter(mAdapter);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
View v = getView();
mTwoPane = getActivity().findViewById(R.id.detail_container) != null;
assert v != null;
ListView lv = (ListView)v.findViewById(android.R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private Victoria selectedMain;
private View selectedView;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
VictoriaListAdapter adapter = (VictoriaListAdapter) parent.getAdapter();
String station = adapter.getItem(position);
if (mTwoPane) {
setItemNormal();
View rowView = view;
setItemSelected(rowView);
Fragment newFragment;
if (station.equals(view.getResources().getString(R.string.bho))) {
newFragment = new FragmentVictoriaBHO();
} else if (station.equals(view.getResources().getString(R.string.brx))) {
newFragment = new FragmentVictoriaBRX();
} else if (station.equals(view.getResources().getString(R.string.eus))) {
newFragment = new FragmentVictoriaEUS();
} else if (station.equals(view.getResources().getString(R.string.fpk))) {
newFragment = new FragmentVictoriaFPK();
} else if (station.equals(view.getResources().getString(R.string.green_park))) {
newFragment = new FragmentVictoriaGreenPark();
} else if (station.equals(view.getResources().getString(R.string.hhy))) {
newFragment = new FragmentVictoriaHHY();
} else if (station.equals(view.getResources().getString(R.string.kxsp))) {
newFragment = new FragmentVictoriaKXSP();
} else {
newFragment = new FragmentVictoriaBHO();
}
VictoriaLineActivity activity = (VictoriaLineActivity) view.getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fade_out, R.anim.fade_in);
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
} else {
Intent intent;
if (station.equals(view.getResources().getString(R.string.bho))) {
intent = new Intent(getActivity(), VictoriaBHOActivity.class);
} else if (station.equals(view.getResources().getString(R.string.brx))) {
intent = new Intent(getActivity(), VictoriaBRXActivity.class);
} else if (station.equals(view.getResources().getString(R.string.eus))) {
intent = new Intent(getActivity(), VictoriaEUSActivity.class);
} else if (station.equals(view.getResources().getString(R.string.fpk))) {
intent = new Intent(getActivity(), VictoriaFPKActivity.class);
} else if (station.equals(view.getResources().getString(R.string.green_park))) {
intent = new Intent(getActivity(), VictoriaGreenParkActivity.class);
} else if (station.equals(view.getResources().getString(R.string.hhy))) {
intent = new Intent(getActivity(), VictoriaHHYActivity.class);
} else if (station.equals(view.getResources().getString(R.string.kxsp))) {
intent = new Intent(getActivity(), VictoriaKXSPActivity.class);
} else {
intent = new Intent(getActivity(), VictoriaBHOActivity.class);
}
startActivity(intent);
}
}
public void setItemSelected(View view) {
View rowView = view;
view.setBackgroundColor(Color.parseColor("#868F98"));
TextView tv0 = (TextView) rowView.findViewById(R.id.item_station);
tv0.setTextColor(Color.WHITE);
TextView tv1 = (TextView) rowView.findViewById(R.id.item_zone);
tv1.setTextColor(Color.WHITE);
}
public void setItemNormal() {
for (int i = 0; i < getListView().getChildCount(); i++) {
View v = getListView().getChildAt(i);
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
}
});
super.onActivityCreated(savedInstanceState);
}
}
data class
public class Victoria {
public Victoria(){}
private String station;
private String zone;
private boolean selected;
public String getStation(){
return station;
}
public void setStation(String item){
this.station = item;
}
public String getZone(){
return zone;
}
public void setZone(String zone){
this.zone = zone;
}
private int _id;
public void getID(int _id){
this._id = _id;
}
public int setID(){
return _id;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
When you scroll through the list, the items/views in the ListView will get reused to optimize the memory.So when you set a selected state to a list item, you will see that selected state in multiple list items as you scroll through. The best way to prevent this is to preserve the state in your Data Model and set the state in getView function of the Adapter.
Here is what you can do -
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
VictoriaListAdapter adapter = (VictoriaListAdapter) parent.getAdapter();
//reverse the selected state in data model
for (int i = 0; i < adapter.getCount(); i++) {
Victoria victoria = (Victoria)adapter.getItem(i);
victoria.setSelected(i == position ? true : false);
}
Victoria victoria = (Victoria)adapter.getItem(position);
---
---
And in adapter -
#Override
public Object getItem(int position) {
//Return full object, coz we need to access other
//member variables too
return mData.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_dualline, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.item_station);
holder.description = (TextView) convertView.findViewById(R.id.item_zone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Victoria victoria = (Victoria)getItem(position);
holder.title.setText(victoria.getStation());
holder.description.setText(victoria.getZone());
if (victoria.isSelected()) {
setItemSelected(convertView);
} else {
setItemNormal(convertView);
}
return convertView;
}
public void setItemSelected(View view) {
View rowView = view;
view.setBackgroundColor(Color.parseColor("#868F98"));
TextView tv0 = (TextView) rowView.findViewById(R.id.item_station);
tv0.setTextColor(Color.WHITE);
TextView tv1 = (TextView) rowView.findViewById(R.id.item_zone);
tv1.setTextColor(Color.WHITE);
}
public void setItemNormal(View v) {
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
Hope it helps!
I am trying to create something like this: https://i.stack.imgur.com/l8ZOc.png
However, i ran into a problem. When i create the list with my adapter, it is supposed to be a list of 8 items. However, it just shows the first 4 of these items in a random order two times. Do you see what is wrong with my code?
public class MyActivity extends Activity{
String headers[];
String image_urls[];
List<MyMenuItem> menuItems;
ListView mylistview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
menuItems = new ArrayList<MyMenuItem>();
headers = getResources().getStringArray(R.array.header_names);
image_urls = getResources().getStringArray(R.array.image_urls);
for (int i = 0; i < headers.length; i++) {
MyMenuItem item = new MyMenuItem(headers[i], image_urls[i]);
menuItems.add(item);
}
mylistview = (ListView) findViewById(R.id.list);
MenuAdapter adapter = new MenuAdapter(this, menuItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
}
public class MyMenuItem {
private String item_header;
private String item_image_url;
public MyMenuItem(String item_header, String item_image_url){
this.item_header=item_header;
this.item_image_url=item_image_url;
}
public String getItem_header(){
return item_header;
}
public void setItem_header(String item_header){
this.item_header=item_header;
}
public String getItem_image_url(){
return item_image_url;
}
public void setItem_image_url(String item_image_url){
this.item_image_url=item_image_url;
}
}
public class MenuAdapter extends BaseAdapter{
Context context;
List<MyMenuItem> menuItems;
MenuAdapter(Context context, List<MyMenuItem> rowItems) {
this.context = context;
this.menuItems = rowItems;
}
#Override
public int getCount() {
return menuItems.size();
}
#Override
public Object getItem(int position) {
return menuItems.get(position);
}
#Override
public long getItemId(int position) {
return menuItems.indexOf(getItem(position));
}
private class ViewHolder {
ImageView ivMenu;
TextView tvMenuHeader;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();
holder.tvMenuHeader = (TextView) convertView.findViewById(R.id.tvMenuHeader);
holder.ivMenu = (ImageView) convertView.findViewById(R.id.ivMenuItem);
MyMenuItem row_pos = menuItems.get(position);
Picasso.with(context)
.load(row_pos.getItem_image_url())
.placeholder(R.drawable.empty)
.error(R.drawable.error)
.into(holder.ivMenu);
holder.tvMenuHeader.setText(row_pos.getItem_header());
Log.e("Test", "headers:" + row_pos.getItem_header());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
You are setting the data only when the convertView is null.So when it is not null,list items are inflated with data of previous list items. getView should be something like this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();
holder.tvMenuHeader = (TextView) convertView.findViewById(R.id.tvMenuHeader);
holder.ivMenu = (ImageView) convertView.findViewById(R.id.ivMenuItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyMenuItem row_pos = menuItems.get(position);
Picasso.with(context)
.load(row_pos.getItem_image_url())
.placeholder(R.drawable.empty)
.error(R.drawable.error)
.into(holder.ivMenu);
holder.tvMenuHeader.setText(row_pos.getItem_header());
Log.e("Test", "headers:" + row_pos.getItem_header());
return convertView;
}
I think this is the solution
else {
holder = (ViewHolder) convertView.getTag();
holder.tvMenuHeader.setText(row_pos.getItem_header());
}
I also encountered this problem earlier and this one works for me.
I'm attempting to set and onClick listener on the items in my listView however for some reason the toast never appears when they are clicked. I'm not sure exactly what is happening in this instance but any suggestions are greatly appreciated.
source:
public class CustomListViewAdapter extends ArrayAdapter<Cmd> {
Activity context;
List<Cmd> videos;
public CustomListViewAdapter(Activity context, List<Cmd> videos) {
super(context, R.layout.list_item2, videos);
this.context = context;
this.videos = videos;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtSuccess;
TextView txtCmd;
TextView txtPrice;
}
public Cmd getItem(int position) {
return videos.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item2, null);
holder = new ViewHolder();
holder.txtSuccess = (TextView) convertView
.findViewById(R.id.success);
holder.txtCmd = (TextView) convertView.findViewById(R.id.cmd);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Cmd cmd = (Cmd) getItem(position);
holder.txtSuccess.setText(cmd.getVideoName());
holder.txtCmd.setText(cmd.getCmd());
// holder.imageView.setImageBitmap(cmd.getImageBitmap());
holder.txtPrice.setText(cmd.getVideoURL() + "");
holder.imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Clicked on image", Toast.LENGTH_LONG)
.show();
}
});
return convertView;
}
}
Make your ImageView clickable and focusable
Your use of the ViewHolder pattern could be improved in a couple of places. For one, you keep resetting your onClickListener() every time getView is called. You should make the following changes:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if(convertView == null){
view = context.getLayoutInflater().inflate(R.layout.list_item2, null);
final ViewHolder holder = new ViewHolder();
holder.txtSuccess = (TextView) view
.findViewById(R.id.success);
holder.txtCmd = (TextView) view.findViewById(R.id.cmd);
holder.txtPrice = (TextView) view.findViewById(R.id.price);
holder.imageView = (ImageView) view.findViewById(R.id.thumbnail);
holder.imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "Clicked on image", Toast.LENGTH_LONG)
.show();
}
});
view.setTag(holder);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
Cmd cmd = (Cmd) getItem(position);
holder.txtSuccess.setText(cmd.getVideoName());
holder.txtCmd.setText(cmd.getCmd());
// holder.imageView.setImageBitmap(cmd.getImageBitmap());
holder.txtPrice.setText(cmd.getVideoURL() + "");
return view;
}
Also, you should make your ViewHolder inner class static.
/* private view holder class */
private static class ViewHolder {
ImageView imageView;
TextView txtSuccess;
TextView txtCmd;
TextView txtPrice;
}
Try to implement touch instead of click or using ImageButton instead of ImageView
holder.imageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN: {
Toast.makeText(context, "Clicked on image", Toast.LENGTH_LONG)
.show();
break;
}
}
return true;
}
});
Is it possible to get an item view based on its position in the adapter and not in the visible views in the ListView?
I am aware of functions like getChildAt() and getItemIdAtPosition() however they provide information based on the visible views inside ListView. I am also aware that Android recycles views which means that I can only work with the visible views in the ListView.
My objective is to have a universal identifier for each item since I am using CursorAdapter so I don't have to calculate the item's position relative to the visible items.
Here's how I accomplished this. Within my (custom) adapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(textViewResourceId, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.button = (ImageButton) view.findViewById(R.id.button);
viewHolder.button.setOnClickListener
(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = (int) viewHolder.button.getTag();
Log.d(TAG, "Position is: " +position);
}
});
view.setTag(viewHolder);
viewHolder.button.setTag(items.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).button.setTag(items.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
return view;
}
Essentially the trick is to set and retrieve the position index via the setTag and getTag methods. The items variable refers to the ArrayList containing my custom (adapter) objects.
Also see this tutorial for in-depth examples. Let me know if you need me to clarify anything.
See below code:
public static class ViewHolder
{
public TextView nm;
public TextView tnm;
public TextView tr;
public TextView re;
public TextView membercount;
public TextView membernm;
public TextView email;
public TextView phone;
public ImageView ii;
}
class ImageAdapter extends ArrayAdapter<CoordinatorData>
{
private ArrayList<CoordinatorData> items;
public FoodDriveImageLoader imageLoader;
public ImageAdapter(Context context, int textViewResourceId,ArrayList<CoordinatorData> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder = null;
if (v == null)
{
try
{
holder=new ViewHolder();
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new FoodDriveImageLoader(FoodDriveModule.this);
v = vi.inflate(R.layout.virtual_food_drive_row, null);
//System.out.println("layout is null.......");
holder.nm = (TextView) v.findViewById(R.id.name);
holder.tnm = (TextView) v.findViewById(R.id.teamname);
holder.tr = (TextView) v.findViewById(R.id.target);
holder.re = (TextView) v.findViewById(R.id.received);
holder.membercount = new TextView(FoodDriveModule.this);
holder.membernm = new TextView(FoodDriveModule.this);
holder.email = new TextView(FoodDriveModule.this);
holder.phone = new TextView(FoodDriveModule.this);
holder.ii = (ImageView) v.findViewById(R.id.icon);
v.setTag(holder);
}
catch(Exception e)
{
System.out.println("Excption Caught"+e);
}
}
else
{
holder=(ViewHolder)v.getTag();
}
CoordinatorData co = items.get(position);
holder.nm.setText(co.getName());
holder.tnm.setText(co.getTeamName());
holder.tr.setText(co.getTarget());
holder.re.setText(co.getReceived());
holder.ii.setTag(co.getImage());
imageLoader.DisplayImage(co.getImage(), FoodDriveModule.this , holder.ii);
if (co != null)
{
}
return v;
}
}
A better option is to identify using the data returned by the CursorAdapter rather than visible views.
For example if your data is in a Array , each data item has a unique index.
Here, Its very short described code, you can simple re-use viewholder pattern to increase listview performance
Write below code in your getview() method
ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.skateparklist, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView
.findViewById(R.id.textView1);
holder.DistanceView = (TextView) convertView
.findViewById(R.id.textView2);
holder.imgview = (NetworkImageView) convertView
.findViewById(R.id.imgSkatepark);
convertView.setTag(holder); //PLEASE PASS HOLDER AS OBJECT PARAM , CAUSE YOU CAN NOY PASS POSITION IT WILL BE CONFLICT Holder and Integer can not cast
//BECAUSE WE NEED TO REUSE CREATED HOLDER
} else {
holder = (ViewHolder) convertView.getTag();
}
// your controls/UI setup
holder.DistanceView.setText(strDistance);
......
return convertview
Listview lv = (ListView) findViewById(R.id.previewlist);
final BaseAdapter adapter = new PreviewAdapter(this, name, age);
confirm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = null;
String value;
for (int i = 0; i < adapter.getCount(); i++) {
view = adapter.getView(i, view, lv);
Textview et = (TextView) view.findViewById(R.id.passfare);
value=et.getText().toString();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
}
});