【发布时间】:2017-08-10 18:05:57
【问题描述】:
我看到很多人都遇到过这个问题,但是提供给他们的解决方案(例如将布局放入另一个布局中)不起作用。因此我决定上传我自己的代码,希望有人知道它有什么问题。我有一个列表视图,每行有 2 个按钮(subtract_button 和 add_button),使用以下适配器类显示:
public class AdapterSIUsed2 extends CursorAdapter implements View.OnClickListener {
private RowViewHolder rowView = new RowViewHolder();
public static class RowViewHolder {
public TextView name,used,category,amount;
public Button subtract,add;
public int position;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case (rowView.add.getId()):
// Do something
Log.d("Button clicked","add");
return;
case (rowView.subtract.getId()):
Log.d("Button clicked","subtract");
return;
}
}
public AdapterSIUsed2(Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor csr, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View listView = inflater.inflate(R.layout.listlayout_shopping_items_used, null);
rowView.name = (TextView) listView.findViewById(R.id.shopping_items_name);
rowView.used = (TextView) listView.findViewById(R.id.shopping_items_used);
rowView.category = (TextView) listView.findViewById(R.id.shopping_items_category);
rowView.amount = (TextView) listView.findViewById(R.id.shopping_items_amount);
rowView.subtract = (Button) listView.findViewById(R.id.subtract_button);
rowView.add = (Button) listView.findViewById(R.id.shopping_items_add_button);
rowView.position = csr.getPosition();
rowView.subtract.setOnClickListener(this);
rowView.add.setOnClickListener(this);
listView.setTag(rowView);
rowView.name.setTag(rowView);
rowView.used.setTag(rowView);
rowView.category.setTag(rowView);
rowView.amount.setTag(rowView);
return listView;
}
@Override
public void bindView(View view, Context context, Cursor csr) {
TextView name = (TextView) view.findViewById(R.id.shopping_items_name);
TextView used = (TextView) view.findViewById(R.id.shopping_items_used);
TextView category = (TextView) view.findViewById(R.id.shopping_items_category);
TextView amount = (TextView) view.findViewById(R.id.shopping_items_amount);
name.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_1)));
used.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_2)));
category.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_3)));
amount.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_4)));
Button add = (Button) view.findViewById(R.id.shopping_items_add_button);
add.setOnClickListener(this);
Button subtract = (Button) view.findViewById(R.id.subtract_button);
subtract.setOnClickListener(this);
int pos = csr.getPosition();
add.setTag(pos);
subtract.setTag(pos);
}
奇怪的是,当我单击加法或减法按钮时,在这两种情况下,我的日志都显示以下两行: D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 我/项目点击::( 当我单击列表项本身时,我的日志仅显示以下行: D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
有谁知道这里发生了什么以及如何解决它?提前致谢!
【问题讨论】:
标签: android listview android-studio button logging