【发布时间】:2017-05-29 13:33:52
【问题描述】:
我正在使用 customAdapter 在列表视图中显示 mydata。 它有一些条件检查一些文件,然后改变线性布局的背景颜色,或者改变一些文本视图的颜色。
在加载之前一切都是正确的...问题如下: 1-之后,当我向下滚动列表并向上滚动时,所有项目的颜色都会改变!!! 2-当我选中一个复选框并向下滚动时,将自动检查其他一些行!!!
3- 我在主要活动中使用此自定义适配器。没关系。但是当我为 listview 设置 listview.setOnItemClickListener 时不起作用!!!
我的代码有什么问题?
以下是自定义适配器类代码:
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-
【问题讨论】:
-
我认为 Override public Object getItem(int position) { return position; } 应该是 Override public Object getItem(int position) { return data.get(position); }
-
不,没有区别!
标签: android listview custom-adapter