我有一个显示数据库信息的列表视图。我希望能够允许用户长按行项目。
到目前为止,我可以长按行项目并检索行ID(此部分有效),但我不相信这是我想要的最终结果。我希望能够检索与行项关联的数据库中的主键;此信息显示在行项目中。我似乎无法走得那么远;我无法弄清楚如何连接'列表视图中的行id返回行中的信息(例如主键和其他文本)。下面是我到目前为止的代码。
DatabaseHandler db = new DatabaseHandler(this);
SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
Cursor cursor=db.getAllLocationsCursor();
String from [] = new String[] {db.COLUMN_DESCRIPTION, db.COLUMN_LOCATION, db.KEY_ID};
final int to [] = new int[]{R.id.descriptionList, R.id.locationList, R.id.idList};
dataAdapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to, 0);
ListView lv = getListView();
lv.setAdapter(dataAdapter);
OnItemLongClickListener listener = new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id){
Toast.makeText(getApplicationContext(), "Long Clicked " + id, Toast.LENGTH_SHORT).show();
return false;
}
};
lv.setOnItemLongClickListener(listener);
}
这是我的行项目xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:clickable="true"
android:focusable="true"
android:longClickable="true"
android:onClick="itemClick" >
<TextView
android:id="@+id/descriptionList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/mainText" />
<TextView
android:id="@+id/locationList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/descriptionList"
android:lines="2"
android:text="TextView"
android:textColor="@color/locationWordsText" />
<TextView
android:id="@+id/idList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:alpha="0"
android:text="TextView" />
</RelativeLayout>
我已经搜索了这个,但我似乎无法找到专门解决此问题的答案
这个答案能让我走上正确的轨道,直到解决长按: can't getting data from cursor adapter
答案 0 :(得分:0)
OnItemLongClickListener listener = new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id){
Toast.makeText(getApplicationContext(), "Long Clicked " + id, Toast.LENGTH_SHORT).show();
return false;
}
};
lv.setOnItemLongClickListener(listener);
到
OnItemLongClickListener listener = new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long id){
TextView test = (TextView) view.findViewById(R.id.descriptionList);
description_string = test.getText().toString();
Toast.makeText(getApplicationContext(), "Long Clicked " + description_string, Toast.LENGTH_SHORT).show();
return false;
}
};
lv.setOnItemLongClickListener(listener);