【发布时间】:2015-11-17 07:47:02
【问题描述】:
我使用适配器创建了一个列表视图。列表视图包含表中特定字段的不同值。现在,当单击列表视图项目时,它应该检索具有该特定数据的 id 的所有项目。我设法从列表视图中获取数据,但是当项目未正确获取时。
row.xml
<ImageView
android:id="@+id/img"
android:src="@mipmap/ic_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtid"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sd"
/>
Tab2.java
ListView list;
final ArrayList<HashMap<String, String>> mylist;
ListAdapter adapter;
HashMap<String, String> map2;
final TextView a1;
list = (ListView) v.findViewById(R.id.listView2);
a1 = (TextView) v.findViewById(R.id.txt1);
mylist = new ArrayList<HashMap<String, String>>();
DatabaseHandler db = new DatabaseHandler(getActivity());
final List<Product> sent = db.getSentProducts();
for (Product s : sent) {
map2 = new HashMap<String, String>();
map2.put("txtid", s.getMsg_id());
mylist.add(map2);
Log.d("msgid", s.getMsg_id());
}
for (Product s : sent) {
}
try {
adapter = new SimpleAdapter(getActivity(),mylist,R.layout.sent_row, new String[]{"txtid"}, new int[]{R.id.txtid});
list.setAdapter(adapter);
} catch (Exception e) {
}
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
TextView a = (TextView)v.findViewById(R.id.txtid);
String content1 = a.getText().toString();
DatabaseHandler db = new DatabaseHandler(getActivity());
List<Product> sentinfo = db.getSentProductInfo(content1);
for (Product si : sentinfo){
Log.d("asdsd", si.getStorCode()+" "+si.getCode()+" "+si.getBstock()+" "+si.getDeliveries()+" "+si.getSpoilage()+" "+ si.getEstock()+" ");
}
return true;
}
});
return v;
}
数据库
安卓用户界面
当我在列表视图中单击 20150824070159 时,它应该在表中获取两行,但是,它只获取一个并且它是另一个 id 的数据,20150820162104。
我该如何回答这个问题?
【问题讨论】:
-
ListView 位置从 0 开始计算,你确定你得到了正确的值吗?尝试将其他项目添加到列表中,看看它是否总是获得高于您单击的值的值,或者它是否总是返回第一个。
-
我尝试添加其他值,但结果仍然相同。它总是返回第一个。
标签: android sqlite listview android-listview