Reputation: 3293
Buttons in listview
do not fire onclick
event but if I click the line it does.
I have a listview in a fragment
. Each row in the listview has 2 buttons
and a textview
. Problem is, if I click the textview
the event is fired but if I click the buttons the event is not fired. I only have the string in my data adapter since I am dealing with files and that is the filename. The buttons will be actions on the files.
Here is the adapter(I don't have a class for the data adapter, do I have to?)
public class fileExplorerAdapter extends ArrayAdapter<String> {
private ArrayList<String> items;
public fileExplorerAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.file_explorer_cell, null);
}
String fileName = items.get(position);
TextView tt = (TextView) v.findViewById(R.id.txtViewFileName);
tt.setText(fileName);
return v;
}
}
here is the setup for the event in the fragment
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d("debug", "in item clicked");
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView
.getItemAtPosition(position);
// Show Alert
Toast.makeText(
rootView.getContext(),
"Position :" + itemPosition + " ListItem : "
+ itemValue, Toast.LENGTH_LONG).show();
}
});
Upvotes: 0
Views: 434
Reputation: 358
Please go through the basics of android programming.
The onItemClick listener responds to the click event of the row of the list, not the button.
Specify the button listener in the getView method first. There are no event listeners for buttons in your code.
Upvotes: 0
Reputation: 394
You need to create separate View.OnClickListener
for your TextView
and your Button
s.
If you have a OnItemClickListener, then only the whole item view will respond to click events.
So you should instead do something like this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.file_explorer_cell, null);
}
String fileName = items.get(position);
// hook up text view
TextView tt = (TextView) v.findViewById(R.id.txtViewFileName);
tt.setOnClickListener(textview_onclick);
// hook up button one
Button button1 = (Button) v.findViewById(R.id.button1);
button.setOnClickListener(button_onclick);
// hook up button two
Button button2 = (Button) v.findViewById(R.id.button2);
button.setOnClickListener(button_onclick2);
tt.setText(fileName);
return v;
}
and remove the OnItemClickListener.
As a side note, you should set up listeners for individual widgets in each row when convertView is null. I.e. just set them up when they are first created.
Upvotes: 2
Reputation: 201
I dont see any buttons in your code. There is even no eventlistener except for the items (in your case a textview) in the list. From that point of view, your provided code does exactly what you say.
Upvotes: 0