[英]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.