I have a custom listview with two textviews . If I use the emulator d-pad, everything works fine, the row is selected, but if I click on the item on the emulator (or try to select on the phone) nothing get selected.
addresslist.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:paddingBottom="6dip"
android:paddingTop="4dip">
<TextView android:id="@+id/DENUMIRE_CELL"
android:layout_width="50dp" android:layout_height="wrap_content"
android:layout_weight="1.03" />
<TextView android:id="@+id/ADRESA_CELL" android:layout_width="50dp"
android:layout_height="wrap_content" android:layout_weight="0.84" />
</LinearLayout>
clienti.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:focusable="true" android:focusableInTouchMode="true">
<EditText android:id="@+id/editTextCauta"
android:layout_width="100dp" android:layout_height="wrap_content"
android:layout_weight="0.04" />
<Button android:id="@+id/buttonCauta" android:layout_width="70dp"
android:layout_height="wrap_content" android:text="Cauta" />
</LinearLayout>
</TableRow>
<TableRow android:id="@+id/tableRowHeader"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#000000">
<LinearLayout android:orientation="horizontal">
<TextView android:id="@+id/textView1" android:layout_width="159dp"
android:layout_height="wrap_content" android:text="Denumire"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#BBBBBB" android:textSize="15sp" />
<TextView android:id="@+id/textView2" android:layout_width="159dp"
android:layout_height="wrap_content" android:text="Adresa"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#BBBBBB" android:textSize="15sp" />
</LinearLayout>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:baselineAligned="false">
<ListView android:id="@+id/adresslist" android:layout_width="wrap_content"
android:choiceMode="singleChoice" android:layout_height="wrap_content"
android:scrollbars="horizontal|vertical" />
</TableRow>
</TableLayout>
Code:
public void adresalistBind()
{
listview = (ListView) findViewById(R.id.adresslist);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
ArrayList<ClientClass> clientlist=new ArrayList<ClientClass>();
ClientClass.setClientContextForDB(this);
clientlist=ClientClass.ClientiGet();
listaAll=clientlist;
HashMap<String, String> map;
for(int i=0;i<clientlist.size();i++)
{
map = new HashMap<String, String>();
map.put("denumire", clientlist.get(i).getDenumire());
map.put("adresa", clientlist.get(i).getAdresa());
map.put("clientid", String.valueOf(clientlist.get(i).getClientID()));
mylist.add(map);
}
SimpleAdapter mAdapter = new SimpleAdapter(this, mylist, R.layout.addresslist,
new String[] {"denumire", "adresa"}, new int[] {R.id.DENUMIRE_CELL, R.id.ADRESA_CELL});
listview.setAdapter(mAdapter);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setSelection(1);
listview.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> adaptview, View clickedview, int position,
long id)
{
//adaptview.setSelected(true);
listview.setSelection(position);
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
listview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> adaptview, View arg1,
int position, long arg3)
{
//adaptview.setSelected(true);
// View rowview = (View) adaptview.getChildAt(position);
// rowview.setSelected(true);
listview.setSelection(position);
//Toast.makeText(ClientiForm.this, (String) listview.getItemAtPosition(position).toString(), 10000).show();
}
});
listview.setOnItemLongClickListener(new OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3)
{
listview.setSelection(position);
openOptionsMenu();
Toast.makeText(ClientiForm.this, (String) listview.getItemAtPosition(position).toString(), 10000).show();
return false;
}
});
}
In order for item to be “checkable”, the top view of the item should implement
Checkableinterface. Only in this case you’ll get “checked”/”unchecked” states withinListView.Note that “selectable” and “checkable” items have different meanings. In your case you mean “checkable” item. Not “selectable”. You need your items to be “checked”, but
ListViewmakes sure there is at most one item checked at a time inCHOICE_MODE_SINGLE. While “selected” item is item that is currently active (this is mostly used for accessibility needs).You can verify what I said by simply replacing
LinearLayoutwithCheckBoxview in your list item’s layout. AndListViewshould start handling “checked” states automatically.If you want to use
LinearLayoutand still be able to support “checked”/”unchecked” states, you need to implement custom layout that extendsLinearLayoutand implementsCheckableinterface.Here is what my version of this implementation is:
Your item layout should look something like:
And note that “drawable/checakble_item”. It should handle different states. In my example it looks like:
Where
drawable/item_pressed,drawable/item_checkedanddrawable/item_uncheckedare my custom drawables.The last note: in your
Adapter‘sgetView()you’ll have to callview.setClickable(false);for checkable states to be handled byListViewand not byCheckableLinearLayout.