Reputation: 107
My question is very simple: i have a Custom adapter that sets image in a listview:
package com.tred.stars;
import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class CustomListAdapter extends ArrayAdapter<String> {
public static String selected_pers= "Graziana Grasso";
Activity context;
String[] itemname;
Integer[] imgid;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
String[] description ={
"desc",
"desc",
"desc",
"desc",
"desc",
"desc",
"desc",
"desc"
};
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.pers_name);
ImageView imageView = (ImageView) rowView.findViewById(R.id.pers_image);
TextView extratxt = (TextView) rowView.findViewById(R.id.pers_comment);
LinearLayout llrow = (LinearLayout) rowView.findViewById(R.id.row);
Toast.makeText(getContext(), selected_pers, Toast.LENGTH_SHORT).show();
if (getItem(position).toString()==selected_pers){
llrow.setBackgroundColor(Color.parseColor("#29A3CC"));
}
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText(description[position]);
return rowView;
};
}
And in my activity i set the adapter to my listview:
DrawerListView = (ListView) findViewById(R.id.drawerLW);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
DrawerListView.setAdapter(adapter);
But when i run the application the method getView in CustomListAdaper seems to repeat itself continously and the variable selected_pers seems to be null.
Upvotes: 1
Views: 205
Reputation: 3766
Change this line:
mInflater.inflate(R.layout.mylist, null, true);
To:
mInflater.inflate(R.layout.mylist, parent, false);
And you can not use ==
when comparing strings. Change this line:
if (getItem(position).toString()==selected_pers)
To:
if (getItem(position).toString().equals(selected_pers))
Also, take a look at this example to learn how to implement correct & efficient ListView.
Upvotes: 1