I'm using a customAdapter to show mydata in a listview. It has some condition which checks some fileds and then changes background color of linearlayout, or changes the color of some textview.
Every thing is right untill loading...problems are below: 1-After it when I scroll the list down and scroll back up color of the all items will change!!! 2- when i check one check box and scroll down some other rows will be check automatically!!!
3- I use this custom adapter in main activity.it's ok. but when i set listview.setOnItemClickListener for listview is won't work!!!
What is wrong with my code?
below is customadapter class codes:
package ir.telad.houseagancy;
...
public class CustomAdapter extends BaseAdapter {
private Activity activity;
private ArrayList data;
private LayoutInflater inflater;
int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,String re) {
/********** Take passed values **********/
activity = a;
data=d;
inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
public class ViewHolder{
public TextView name;
public TextView moaref;
public TextView address;
public TextView tel;
public TextView rahn;
public TextView ejare;
public TextView date;
public ImageView image;
public TextView foori;
public LinearLayout layout1;
public CheckBox checkBox;
//public TableRow tblr;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View vi;
final ViewHolder holder;
ListModel tempValues;
if(convertView==null){
/****** Inflate list_item.xml file for each row ( Defined below ) *******/
convertView = inflater.inflate(R.layout.list_item,null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.layout1=(LinearLayout)convertView.findViewById(R.id.layout1);
holder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
holder.name = (TextView) convertView.findViewById(R.id.list_txtName);
holder.tel=(TextView)convertView.findViewById(R.id.list_txtTel);
holder.address=(TextView)convertView.findViewById(R.id.list_txtAddress);
holder.rahn=(TextView) convertView.findViewById(R.id.list_txtRahn);
holder.ejare=(TextView) convertView.findViewById(R.id.list_txtEjare);
holder.image=(ImageView) convertView.findViewById(R.id.list_image);
holder.foori=(TextView) convertView.findViewById(R.id.list_txtFoori);
holder.date=(TextView) convertView.findViewById(R.id.list_txtDate);
vi=convertView;
/************ Set holder with LayoutInflater ************/
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
vi=convertView;
}
if(data.size()<=0)
{
holder.name.setText("no data");
holder.address.setText("");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues = ( ListModel ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.name.setText( tempValues.Name );
holder.tel.setText( tempValues.Phone );
holder.address.setText( tempValues.Address );
holder.rahn.setText( tempValues.Rahn );
holder.ejare.setText( tempValues.Ejare );
holder.date.setText(tempValues.Date);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b==true)
holder.layout1.setBackgroundColor(Color.argb(150,250,100,100));
else if(b==false) holder.layout1.setBackgroundColor(Color.TRANSPARENT);
}
});
if(tempValues.foori==1)
holder.foori.setVisibility(View.VISIBLE);
if(tempValues.state==3){
//holder.tblr.setBackgroundColor(Color.parseColor("#00ff00"));
holder.name.setTextColor(Color.parseColor("#3C7700"));
holder.tel.setTextColor(Color.parseColor("#2C6700"));
holder.rahn.setTextColor(Color.parseColor("#397249"));
holder.ejare.setTextColor(Color.parseColor("#92CD00"));
}
if(tempValues.isAzad==1){
//holder.tblr.setBackgroundColor(Color.parseColor("#00ff00"));
holder.name.setTextColor(Color.parseColor("#0000FF"));
holder.tel.setTextColor(Color.parseColor("#0000FF"));
holder.rahn.setTextColor(Color.parseColor("#0000FF"));
holder.ejare.setTextColor(Color.parseColor("#0000FF"));
}
}
return convertView;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
2-
You have implemented only
if
case why not implementedelse
case its happened because view is created every time when you scroll up and down so define else also for check and color and to maintain thecheckbox
checked or not you have to make aint Array
inadapter
to save his checked state at specific position and put theif else
condition to check uncheck your checkbox.your
OnItemClick
is not working because you are usingcheckbox
inListView
Custom Row, so there is on solution setfocusable
false
inxml
of custom row andtrue
inlistview
xml.hope it will help you.