单击时,自定义ListView无法正常工作

时间:2018-01-26 19:53:41

标签: android listview custom-adapter

所以我有一个简单的Custom ListView。单击ListView中的按钮时,应该创建Toast(作为测试,稍后将根据按下的类别切换到活动)。

问题是,单击任何按钮时没有任何反应。

目前还不确定如何修复它。

类别表

public class CategoryTable extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_table);

    populateTable();

}

private void populateTable() {

    final ListView mListView = findViewById(R.id.listView);

    Category One = new Category("  One");
    Category Two = new Category("  Two");
    Category Three = new Category("  Three");
    Category Four = new Category("  Four");

    final ArrayList<Category> categoryList = new ArrayList<>();
    categoryList.add(One);
    categoryList.add(Two);
    categoryList.add(Three);
    categoryList.add(Four);

    CategoryListAdapter adapter = new CategoryListAdapter(this, R.layout.adapter_view_layout, categoryList);
    mListView.setAdapter(adapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Toast toast = Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT);
            toast.show();
            }
        }
    });
}

自定义适配器

public class CategoryListAdapter extends ArrayAdapter<Category> {

private Context mContext;
int mResource;

public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
    super(context, resource, objects);
    mContext = context;
    mResource = resource;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String name = getItem(position).getName();

    Category category = new Category(name);

    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource, parent, false);

    Button catName = convertView.findViewById(R.id.btnNextTbl);

    catName.setText(name);

    return convertView;
}
}

我还添加了

android:descendantFocusability="blocksDescendants"

到自定义适配器xml文件(虽然似乎没有改变任何东西)。

如果需要,将包含更多代码。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

因为你在点击实际行时有监听器,而不是行内的按钮。如果你想点击按钮,你需要做这样的事情

public class CategoryListAdapter extends ArrayAdapter<Category> {

private Context mContext;
int mResource;

public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
    super(context, resource, objects);
    mContext = context;
    mResource = resource;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String name = getItem(position).getName();

    Category category = new Category(name);

    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource, parent, false);

    Button catName = convertView.findViewById(R.id.btnNextTbl);

    catName.setText(name);
    catName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //make call back to activity to do whatever you want
        }
    });
    return convertView;
}
}