onListItemClick does not (always) gets called with ListActivity
I have used a ListActivity to display a view, inflated from a xml layout file. When I initially display this view in the list, I hide a part of the view by setting the visible property of the target view to View.GONE. In the method onListItemClick, I set the visibility of this 'hidden' view to View.GONE, if it is visible. On the next click, I want to hide the view again. To do this, I detect the visibility of the view, and change it to View.GONE if it is View.VISIBLE.
My code executes method onListItemClick, when the hidden view's visibility os View.GONE and correctly displays the hidden view on click. However, it does not executes the onListItemClick method, when the visiblity of the view is set to View.VISIBLE.
I understand that this situation is a good candidate for usage of ExpandableListActivity, but I am unable to use it due to other unresolvable issue!
Here's the (partial relevant) code:
public class MyListActivity extends ListActivity implements OnClickListener {
// images that depict whether part of the view is visible or not
private Drawable imgUp, imgDown;
private class MyOrderAdapter extends ArrayAdapter<Order> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// infalte this view from xml
v = vi.inflate(R.layout.brief_order, null);
// display down arrow key depicting that more is available to be
// displayed when a user clicks on it.
ImageView arrow = (ImageView) v.findViewById开发者_运维技巧(R.id.imgArrow);
// get a reference to the table layout for details
// which should not be displayed initially
TableLayout tb = (TableLayout) v.findViewById(R.id.view_detail);
// DO NOT DISPLAY PART OF THE VIEW INITIALLY
tb.setVisibility(View.GONE);
}
// code to initialize the text filed values in the view v
return v;
}
// onListItemClick
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d("MyList", "item click called");
// RETRIEVE THE detail view
TableLayout tb = (TableLayout) v.findViewById(R.id.view_detail);
if(tb.getVisibility() == View.GONE) {
tb.setVisibility(View.VISIBLE);
img.setImageDrawable(imgUp);
}
else {
img.setImageDrawable(imgDown);
tb.setVisibility(View.GONE);
}
}
// rest of the code
}
Thanks.
cheers
But just in case, you have to set the images and other items inside your ListView
as not focusable
and the onListItemClick
method will be invoked.
精彩评论