我的应用程序有问题;在我的应用程序中,我优化了列表视图适配器,它是使用按钮的项目,但是当我单击“显示所有记录”之类的按钮时,列表视图中的项目显示为灰色。
我认为代码不言自明:
show_all_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Rest_menu menu = new Rest_menu();
menu = (Rest_menu)v.getTag();
myAdapter adapter = new myAdapter(getApplicationContext(), menu.all_array);
listView.setAdapter(adapter);
}
});
如您所见,通过单击 show_all_button,listview 的适配器设置为显示所有项目,并且显示项目,但行显示为灰色。我该怎么做才能使它们不灰显?编辑:经过一些编码,编辑了适配器,这是列表视图适配器的代码:
public class foodAdapter extends ArrayAdapter<Food> {
Context context;
public Food[] iteminarow;
public foodAdapter(Context context,Food[] iteminarow)
{
super(context, R.layout.listitem,iteminarow);
this.context=context;
this.iteminarow=iteminarow;
}
static class ViewHolder{
TextView foodname;
TextView foodprice;
TextView foodinfo;
public ViewHolder(TextView foodname,TextView foodprice,TextView foodinfo)
{
this.foodname=foodname;
this.foodprice=foodprice;
this.foodinfo=foodinfo;
}
}//end of ViewHolder
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView name,price,info;
Food f = (Food)this.getItem(position);
if(convertView==null)
{
LayoutInflater LI = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = LI.inflate(R.layout.listitem,parent,false);
name=(TextView)convertView.findViewById(R.id.foodname);
price=(TextView)convertView.findViewById(R.id.foodprice);
info=(TextView)convertView.findViewById(R.id.foodinfo);
convertView.setTag(new ViewHolder(name, price, info));
}//end of if
else {
ViewHolder holder = (ViewHolder) convertView.getTag();
name = holder.foodname;
price = holder.foodprice;
info = holder.foodinfo;
}//end of else
name.setText(f.getName().toString());
price.setText(f.getPriceString());
info.setText(f.getInfo().toString());
return convertView;
}
}//end of adapter