我使用getItemIdAtPosition()来获取Sqlite数据库中记录的Basecoulmns id。
代码如下:
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = managedQuery(Constants.CONTENT_URI,
PROJECTION, BaseColumns._ID + "="
+ l.getItemIdAtPosition(position), null, null);
}
但它没有正确检索id。这种方法在设置适配器或创建数据库时是否取决于某些方面?我不知道。为什么它显示列表视图的位置。有什么想法吗?
编辑:
getView mehod的代码:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.brutube_videos, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.vdo_text1);
holder.text2 = (TextView) convertView
.findViewById(R.id.vdo_text2);
holder.text3 = (TextView) convertView
.findViewById(R.id.vdo_rate_text);
holder.icon = (ImageView) convertView
.findViewById(R.id.vdo_icon);
convertView.setTag(holder);
} else {
holder.text1 = (TextView) convertView
.findViewById(R.id.vdo_text1);
holder.text2 = (TextView) convertView
.findViewById(R.id.vdo_text2);
holder.text3 = (TextView) convertView
.findViewById(R.id.vdo_rate_text);
holder.icon = (ImageView) convertView
.findViewById(R.id.vdo_icon);
}
if (!mBusy) {
try {
Log.v("getview", "" + (position) + " - " + VAL3[position]);
holder.icon.setImageBitmap(BitmapFactory
.decodeStream(new URL(VAL3[position])
.openConnection().getInputStream()));
notifyDataSetChanged();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
holder.icon.setTag(null);
} else {
holder.icon.setImageResource(R.drawable.no_video_icon);
// Non-null tag means the view still needs to load it's data
holder.icon.setTag(this);
}
holder.text1.setText(VAL1[position]);
holder.text2.setText(VAL2[position]);
holder.text3.setText(VAL4[position] + " Ratings");
return convertView;
}
答案 0 :(得分:1)
您可以尝试将列表行视图的所需值设置为标记。在列表的viewbinder或适配器中,在行视图上使用setTag()。
然后调用(long)v.getTag();在列表项上单击
编辑以包含BaseAdapter的示例代码
/* In Your BaseAdapter */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout view;
if(convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
view = (RelativeLayout)layoutInflator.inflate(R.layout.my_layout_row, null);
} else {
view = (RelativeLayout)convertView;
}
long myIdFromDB = // Do your database work
// Set Tag
view.setTag(myIdFromDB);
return view;
}
/* Your listItemClicked method */
protected void onListItemClick(ListView l, View v, int position, long id) {
// Get Tag
long baseColumnId = (long)v.getTag();
Cursor cursor = managedQuery(Constants.CONTENT_URI,
PROJECTION, BaseColumns._ID + "="
+ baseColumnId, null, null);
}
答案 1 :(得分:0)
不知道这是否有用,但你可以check out标准的Android CursorAdapter如何传递id,也许可以在你的实现中做类似的事情。
更具体地说,请查看getItemId和hasStableIds。