我有一个listview,我希望它能做两个不同的功能。第一个是工作,当你从列表中选择它开始另一个活动时,我在onListItemClick()方法上执行此操作。第二个是我遇到问题的那个。我在列表的每个成员上都有一个复选框,我希望能够选择多个元素并对所选成员执行某些操作,任何事情!因为在我的代码中它不起作用。目前我试图在OnItemClickListener()方法中实现,但它没有顺利
这是我的带有listview的java代码:
public class Lista_general extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_general);
ListView list = getListView();
final Button bt = (Button) findViewById(R.id.button2);
// open database
AdminSQLiteOpenHelper dbhelper = new AdminSQLiteOpenHelper(getBaseContext());
SQLiteDatabase db = dbhelper.getReadableDatabase();
//necessary for the simple cursor adapter
String columns[] = new String[] { "_id", "nombre", "categoria" };
// cursor whith the query of the database
Cursor c = db.query("PRODUCTO ORDER BY categoria ", columns, null,null, null, null, null);
//necessary for the simple cursor adapter
c.moveToFirst();
String from[] = new String[] { "nombre", "categoria", };
int to[] = new int[] { R.id.name, R.id.cate, };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.productos, c, from, to,SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
// close database
db.close();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
// I think here is where i can implement the use of the checkboxes
}
});
}
@Override
public void onListItemClick(ListView l, View v, int arg2, long arg3) {
Intent i = new Intent(Lista_general.this, Agregar_articulo.class);
i.putExtra("id", String.valueOf(arg3));
i.putExtra("num", "y");
startActivity(i);
}
public void lanzar(View view) {
Intent i = new Intent(Lista_general.this, Agregar_articulo.class);
i.putExtra("num", "n");
startActivity(i);
}
}
这是我使用listview的布局:lista_general.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/eliminar"
android:onClick="eliminar"
android:enabled="false"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="lanzar"
android:text="@string/agregar_art" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:fadingEdge="none"
android:cacheColorHint="@null"
android:layout_height="fill_parent"
android:choiceMode="multipleChoice">
</ListView>
</LinearLayout>
以下是每行的代码:productos.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/cate"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/cate"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:clickable="true"
android:textSize="16sp"
android:id="@+id/name"
/>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text=""
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="true"
/>
</RelativeLayout>
这是我的应用程序的图像。我的目标是,当您使用复选框选择不同的元素时,会出现名为Eliminar的按钮,然后您可以消除它们。