views:

549

answers:

2

Hi

I have made an BaseAdapter that does not show the onclick effect on the rows when the items get selected (orange background).

Im wondering what is needed to make the OnClickedItem effect appear. I tried to set the OnItemClickListener on the list, but it did not help.

here is my code:

    private class MyAdapter extends BaseAdapter{

      private Bingo activity;


  public MyAdapter(Bingo context){
   this.activity = context;
   items = new ArrayList<String>();

  }

  public void add(String item){
   items.add(item);
   notifyDataSetChanged();
  }

  public void clear(){
   items.clear();
   notifyDataSetChanged();
  }

  private ArrayList<String> items;


  public int getCount() {
   return items.size();
  }

  public Object getItem(int position) {
   return items.get(position);
  }

  public long getItemId(int position) {
   return position;
  }

  public boolean isEnabled(int position){
   return true;
  }

  public View getView(int position, View convertView, ViewGroup parent) {

   if(position == getCount() - 5 && getCount() < 100){
    Log.d("dbg","Autoadding...");
    activity.add();
   }

   View view = null;
            if (convertView == null) {
             Log.i("dbg","new:"+position);
             view = View.inflate(activity, R.layout.item, null);
            }
            else {
             Log.i("dbg","recycle:"+position);
             view = convertView;
            }
            TextView text = (TextView)view.findViewById(R.id.text);
            text.setText(items.get(position)+" -------> pos:"+position);

   return view;
  }

 }

item.xml that is inflated...

LinearLayout
android:id="@+id/root" 
android:orientation="horizontal"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"

 ImageView 
 android:id="@+id/image" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:scaleType="fitXY" 
 android:layout_marginRight="10dp" 
 android:src="@drawable/icon"


 LinearLayout
 android:orientation="horizontal" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:gravity="right"

  TextView 
  android:id="@+id/text" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:textColor="#FFF"
  android:singleLine="true" 

  Button 
  android:id="@+id/button" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:text="OK"
  android:layout_gravity="right" 
  android:gravity="center_horizontal"
  android:inputType="textUri"

 /LinearLayout
/LinearLayout
+1  A: 

Ohh, i think i found the problem... its the button ... if i remove the button from the inflated view the row can be selected.... if the button is there i can only click/focus on the button...

PHP_Jedi
That's right - a list item can have its constituents be selectable, or can be selectable itself, but not both.
Klondike
+2  A: 

Try this
For ListView,

final ListView list = (ListView) findViewById(R.id.list);
list.setItemsCanFocus(false);

Also, make sure that for Button inside list item set focusable false

android:focusable="false"
android:focusableInTouchMode="false"

Similar Question

bhatt4982
Yep, this also works!
PHP_Jedi