这个问题让我发疯....
我有一个 FragmentActivity。在里面我有一个 listView 我列出了一些客户。我需要在列表中单击进行操作。但是 OnItemClickListener 没有被触发。我不知道为什么。我尝试了我查看的所有解决方案,但都不起作用......
public class MainActivity extends FragmentActivity implements OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor>, SimpleCursorAdapter.ViewBinder, OnItemClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listActivos = getListView();
cursorFiltroAutocomplete = Client.listActiveClients(this);
cursorAdapter = new SimpleCursorAdapter(this,
R.layout.client_row,
cursorFiltroAutocomplete,
new String[]{Client.NAME,Client.ACCOUNT,Client.ICON,Client.PHOTO,Client.DATEINI,Client.DATEFIN},
new int[]{R.id.nameClient,/*R.id.accountClient*/R.id.star,R.id.photo,R.id.dateIni,R.id.dateFim}
);
listActivos.setAdapter(cursorAdapter);
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
switch (view.getId()){
case R.id.nameClient:
TextView tv = (TextView)view;
tv.setText(cursor.getString(columnIndex));
return true;
//case R.id.accountClient:
// return true;
case R.id.star:
star = cursor.getInt(cursor.getColumnIndex(Client.ICON));
tbFavorite = (ToggleButton)view;
if(star==1)
tbFavorite.setChecked(true);
else
tbFavorite.setChecked(false);
return true;
case R.id.photo:
byte[] bb = cursor.getBlob(cursor.getColumnIndex(Client.PHOTO));
ImageView myImage = (ImageView)view;
if (bb != null) {
myImage.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
} else {
Bitmap bm = null;
try {
bm = getBitmapFromAsset("contact_userUpdate.png");
} catch (IOException ex){ex.printStackTrace();}
if ( bm != null)
myImage.setImageBitmap(bm);
else
Log.w(MainActivity.class.getName(), "ICON IS NULL");
}
return true;
case R.id.dateIni:
dateIni = cursor.getString(cursor.getColumnIndex(Client.DATEINI));
TextView dateI = (TextView)view.findViewById(R.id.dateIni);
dateI.setText(dateIni);
return true;
case R.id.dateFim:
dateFim = cursor.getString(cursor.getColumnIndex(Client.DATEFIN));
TextView dateF = (TextView)view.findViewById(R.id.dateFim);
dateF.setText(dateFim);
return true;
}
return false;
}
});
registerForContextMenu(listActivos);
listActivos.setClickable(true);
listActivos.setOnItemClickListener(this);
getSupportLoaderManager().initLoader(0, null, this);
}
/*********************************Function to get ListView************************/
private ListView getListView() {
return (ListView)findViewById(android.R.id.list);
}
/*********************************Function onItemClick*****************************/
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.w(MainActivity.class.getName(), "ITEM CLICADO");
}
/******************************XML with the row of the List************************/
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/list_selector_background"
android:gravity="fill_horizontal"
android:longClickable="true"
android:clickable="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="8dip"
android:paddingTop="5dip">
<ImageView
android:id="@+id/photo"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:scaleType="fitXY"
android:src="@drawable/contact_user"
android:focusable="false"
android:clickable="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="fill"
android:orientation="vertical">
<TextView
android:id="@+id/nameClient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceMedium"
android:minLines="1"
android:maxLines="1"
android:focusable="false"
android:clickable="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="fill"
android:orientation="horizontal">
<TextView android:id="@+id/dateIni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dateIni"
android:textColor="#59df8f"
android:textAppearance="?android:attr/textAppearanceMedium"
android:minLines="1"
android:maxLines="1"
android:focusable="false"
android:clickable="false" />
<TextView android:id="@+id/dateFim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dateFim"
android:textColor="#ff3100"
android:textAppearance="?android:attr/textAppearanceMedium"
android:minLines="1"
android:maxLines="1"
android:focusable="false"
android:clickable="false"/>
</LinearLayout>
</LinearLayout>
<ToggleButton
android:id="@+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:background="@drawable/list_star_selector"
android:focusable="false"
android:clickable="false"/>
</TableRow>
/******************************XML with the list View******************************/
<?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="fill_parent"
android:background="@drawable/background_tile"
android:orientation="vertical" >
<ListView
android:id="@+id/@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
/>
</LinearLayout>
有人有想法吗?我把列表中的项目“android:focusable="false" 但不起作用。我把 android:descendantFocusability="beforeDescendants" 但结果是一样的......