单击时自定义 ListView 无法正常工作
Custom ListView is not working properly when clicked
所以我有一个简单的自定义 ListView。单击 ListView 中的按钮时,应该创建一个 Toast(作为测试,稍后将根据按下的类别切换到 activity)。
问题是,单击任何按钮都没有任何反应。
目前不确定如何解决。
类别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();
}
}
});
}
自定义适配器
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 文件(虽然似乎没有任何改变)。
如果需要,将包含更多代码。
任何帮助将不胜感激。
这是因为当您单击实际行而不是行内的按钮时,您有侦听器。如果你想点击按钮,你需要做这样的事情
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;
}
}
所以我有一个简单的自定义 ListView。单击 ListView 中的按钮时,应该创建一个 Toast(作为测试,稍后将根据按下的类别切换到 activity)。
问题是,单击任何按钮都没有任何反应。
目前不确定如何解决。
类别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();
}
}
});
}
自定义适配器
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 文件(虽然似乎没有任何改变)。
如果需要,将包含更多代码。 任何帮助将不胜感激。
这是因为当您单击实际行而不是行内的按钮时,您有侦听器。如果你想点击按钮,你需要做这样的事情
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;
}
}