【发布时间】:2015-05-12 20:48:23
【问题描述】:
仅将按钮放入列表视图时,OnItemClick 和 OnItemLongClick 不起作用
当我只在列表视图中使用按钮时
当时我很震惊地看到 OnItemClick 和 OnItemLongClick 不起作用。
请帮忙...
dbshow.java
public class dbshow extends Activity implements OnItemClickListener,
OnItemLongClickListener {
private ListView lv_database;
private DAtahelper mhelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
init();
showdataquery();
}
private void init() {
// TODO Auto-generated method stub
lv_database = (ListView) findViewById(R.id.lv_database);
lv_database.setOnItemClickListener(this);
lv_database.setOnItemLongClickListener(this);
}
@SuppressLint("NewApi")
private void showdataquery() {
// TODO Auto-generated method stub
mhelper = new DAtahelper(this);
db = mhelper.getReadableDatabase();
String columns[] = { "_id,name,city,phone" };
Cursor c = db.query("vishal", columns, null, null, null, null, null);
c.moveToFirst();
String[] from = { "name", "city", "phone" };
int[] to = { R.id.tv_name, R.id.tv_city, R.id.tv_phone };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.showdata, c, from, to, 0) {
};
lv_database.setAdapter(adapter);
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.e("", "long");
return false;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.e("", "item");
}
}
listview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_database"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
showdata.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@android:color/black" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@android:color/black" />
</LinearLayout>
列表视图的图像
【问题讨论】:
-
您需要使用自定义适配器!
-
@IndexOutOfBounds 请你举个例子..??带按钮
-
Here。这是使用
ArrayAdapter。您可以轻松地将其更改为SimpleCursorAdapter。 -
将 tis 属性添加到用户列表视图 android:clickable="false"
-
但是先生,我正在从数据库中获取数据如何将 simplecursoradpter 转换为 arrayadapter???
标签: android xml listview button android-listview