2010-05-31 92 views
0

我正在使用getItemIdAtPosition()來獲取Sqlite數據庫中記錄的Basecoulmns id。Android中的getItemIdAtPosition()問題?

的代碼如下:

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。這種方法在設置適配器或創建數據庫時依賴於某些東西?我不知道。爲什麼它顯示了listview的位置。任何想法?

編輯:

爲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; 
    } 

回答

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); 
} 
+0

ThanX的回覆。你能用一些示例代碼來解釋它嗎?我無法完全理解你。 – Praveen 2010-05-31 18:00:35

+0

當然,你能解釋一些關於你如何填寫你的清單嗎?你是從光標填充它嗎?如果是這樣,你使用SimpleCursorAdapter嗎? – m6tt 2010-05-31 18:09:33

+0

不,我只是擴展基礎適配器。如何設置它? – Praveen 2010-05-31 18:17:40

0

不知道這是有益的,但你可以check out如何在標準Android的CursorAdapter一起傳遞的ID,也許做一些事情類似於你的實現。

更具體地說,檢查getItemId和hasStableIds。