当我在列表视图中按下按钮时 那时我很震惊地看到OnItemClick& OnItemLongClick无法正常工作。 请帮忙......
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");
}
}
<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>
<?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>
答案 0 :(得分:1)
请看一下本教程。 http://examples.javacodegeeks.com/android/core/ui/listview/android-multitouch-listview-example/
基本上,您必须创建自定义ArrayAdapter,并在其xml和getView上创建如下内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Name:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textName"
android:layout_marginTop="5dp"
android:text="Age:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp" />
<Button
android:id="@+id/btnEdit"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#99CC"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Edit"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/btnDelete"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/btnEdit"
android:layout_marginTop="3dp"
android:background="#99CC"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Delete"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/textAddr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textAge"
android:layout_marginTop="5dp"
android:text="Address:" />
</RelativeLayout>
对于自定义适配器:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
StudentWrapper StudentWrapper = null;
if (item == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
item = inflater.inflate(layoutResourceId, parent, false);
StudentWrapper = new StudentWrapper();
StudentWrapper.name = (TextView) item.findViewById(R.id.textName);
StudentWrapper.age = (TextView) item.findViewById(R.id.textAge);
StudentWrapper.address = (TextView) item.findViewById(R.id.textAddr);
StudentWrapper.edit = (Button) item.findViewById(R.id.btnEdit);
StudentWrapper.delete = (Button) item.findViewById(R.id.btnDelete);
item.setTag(StudentWrapper);
} else {
StudentWrapper = (StudentWrapper) item.getTag();
}
Student student = students.get(position);
StudentWrapper.name.setText(student.getName());
StudentWrapper.age.setText(student.getAge());
StudentWrapper.address.setText(student.getAddress());
StudentWrapper.edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Edit", Toast.LENGTH_LONG).show();
}
});
StudentWrapper.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Delete", Toast.LENGTH_LONG).show();
}
});
return item;
}
在您的主Activity中,您还可以启用listview项目:
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
Toast.makeText(MainActivity.this,
"List Item Clicked:" + position, Toast.LENGTH_LONG)
.show();
}
});
答案 1 :(得分:-1)
在自定义Adapter类的getView方法中的按钮上设置单击侦听器(OnClickListener)。
步骤 - 阅读列表视图的自定义适配器,并使用View Holder模式实现它。 [新的RecyclerView强制执行视图持有者模式。] - 在您自己的适配器类的getView方法中设置单击侦听器[包含在listview行中] [例如:BaseAdapter]。