我已经制作了自定义ListView,我希望能够点击一个小按钮或整行。所以我为2个案件制作了2个单独的onclicklisteners。我如何参考按钮?它给了我这个错误:
Cannot refer to a non-final variable viewHolder inside an inner class defined in a different method
我不能让它成为最终版,因为它会不断变化。 这是我的代码。
public class ListViewAdapter extends ArrayAdapter<String> {
private static LayoutInflater inflater = null;
public Context context;
public int layoutResourceId;
public ArrayList<HashMap<String, Object>> items;
public Bitmap icon;
private static int REFRESH_THRESHOLD = 2;
//public ImageLoader imageLoader;
public ListViewAdapter(Context context, int listviewItemRow, ArrayList<HashMap<String, Object>> items, Bitmap icon) {
// TODO Auto-generated constructor stub
super(context, listviewItemRow);
this.items = items;
this.context = context;
this.icon = icon;
}
public int getCount() {
return items.size();
}
public Item getItem(Item position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder viewHolder = new ViewHolder();
if (row == null) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listview_item_row, null);
viewHolder.ic_thumbnail = (ImageView)row.findViewById(R.id.ic_thumbnail);
viewHolder.scadenza = (TextView)row.findViewById(R.id.tvScadenza);
viewHolder.prezzo = (TextView)row.findViewById(R.id.tvPrezzo);
viewHolder.followers = (TextView)row.findViewById(R.id.tvFollowers);
viewHolder.hProgressBar = (ProgressBar)row.findViewById(R.id.hProgressBar);
viewHolder.followButton = (Button) row.findViewById(R.id.btnFollow);
row.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)row.getTag();
}
HashMap<String, Object> item = items.get(position);
viewHolder.ic_thumbnail.setImageBitmap((Bitmap) item.get("pic1m"));
viewHolder.scadenza.setText((CharSequence) item.get("scadenza"));
viewHolder.prezzo.setText((CharSequence) item.get("prezzo"));
viewHolder.followers.setText((CharSequence) item.get("followers"));
viewHolder.hProgressBar.setProgress((Integer) item.get("coefficient"));
viewHolder.followButton.setText("non segui");
viewHolder.followButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//here i get the error
viewHolder.followButton = (Button) row.findViewById(R.drawable.action_object_button_gray);
followButton.setText("segui");
}
});
row.setOnClickListener(new OnItemClickListener(position));
return row;
}
private class OnItemClickListener implements OnClickListener {
private int mPosition;
private OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View v) {
Log.i("onListItemClickList", "Item clicked: " + mPosition);
Toast.makeText(context, "Message " + Integer.toString(mPosition), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, DettagliActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("id", mPosition);
intent.putExtras(bundle);
context.startActivity(intent);
}
}
public static class ViewHolder {
public TextView prezzo;
public TextView scadenza;
public TextView followers;
public ImageView ic_thumbnail;
public ProgressBar hProgressBar;
public Button followButton;
}
}
答案 0 :(得分:3)
而不是这个
viewHolder.followButton = (Button) row.findViewById(R.drawable.action_object_button_gray);
followButton.setText("segui");
您可以使用
Buttom button =(Button)v; // casting view to button
button.setText("segui"); // set text to button
如果在匿名内部类中使用变量,则要求变量为final。
参考
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#accessing
答案 1 :(得分:1)
由于您正在对您感兴趣的按钮设置onClickListener,为什么不将视图转换为按钮并使用view参数对其进行修改?
viewHolder.followButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//here i get the error
Button view = (Button) v;
view.setText("segui");
}
});
答案 2 :(得分:0)
简单的解决方案是
final ViewHolder temp = viewHolder;
并使用它。