简体   繁体   English

单击自定义ListView不能正常工作

[英]Custom ListView is not working properly when clicked

So I have a simple Custom ListView. 因此,我有一个简单的Custom ListView。 When a button in the ListView is clicked, a Toast is supposed to be created (as a test, will later switch to an activity depending on the pressed category). 单击ListView中的按钮时,应该创建一个Toast(作为测试,以后将根据所按下的类别切换到活动)。

The problem is, that nothing happens when any of the buttons are clicked. 问题是,当单击任何按钮时,什么也不会发生。

Not sure how to fix it at the moment. 目前尚不确定如何解决。

Category Table 分类表

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();
            }
        }
    });
}

Custom Adapter 定制适配器

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;
}
}

I've also added 我还添加了

android:descendantFocusability="blocksDescendants"

to the custom adapter xml file (didn't seem to change anything though). 到自定义适配器xml文件(尽管似乎没有任何变化)。

Will include more code if needed. 如果需要,将包含更多代码。 Any help would be greatly appreciated. 任何帮助将不胜感激。

its because you have the listener for when you click the actual row, not the button inside the row. 这是因为您具有单击实际行时的侦听器,而不是单击行内的按钮的侦听器。 if you want it on button click you will need to do something like this 如果要在按钮上单击,则需要执行以下操作

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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ListView Clicked项目突出显示无法正常工作 - ListView Clicked Item Highlight not working properly 自定义小部件ListView无法正常工作 - Custom widget ListView not working properly 单击自定义列表视图按钮时开始活动 - start activity when custom listview button clicked 在listview上过滤自定义适配器在android中无法正常工作? - Filtering a Custom Adapter on listview is not working properly in android? 带有复选框和自定义适配器的 ListView,片段无法正常工作 - ListView with checkbox and custom adapter, fragment not working properly Android自定义适配器无法正常用于聊天列表视图 - Android custom adapter not working properly for chat listview 单击自定义列表视图项目Edittext时打开自定义对话框 - open a custom dialog when custom listview item Edittext is clicked 当我单击一个时隐藏所有Custom ListView Child元素 - Hide all Custom ListView Child element when i clicked one 单击列表项时如何在编辑列表视图项时使用自定义适配器? - How to use custom adapter while editing listview items when they are clicked? Android - 从自定义列表视图中删除一个项目并在长按时更新它 - Android - Delete an item from Custom Listview and update it when long clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM