Reputation: 411
So I have a simple 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).
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).
Will include more code if needed. Any help would be greatly appreciated.
Upvotes: 0
Views: 28
Reputation: 13335
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;
}
}
Upvotes: 1