2012-11-04 20 views
0

我試圖開發一個Android應用程序,並且我目前被某些東西卡住了。如何從ListView中的單個項目中烘烤特定的文本ID

我使用自定義列表視圖看起來如下:

<?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="wrap_content" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" > 

    <TextView 
     android:id="@+id/name" 
     style="@style/..."/> 

    <TextView 
     android:id="@+id/number" 
     android:layout_below="@+id/..." 
     style="@style/..." /> 

    <TextView 
     android:id="@+id/id" 
     android:layout_below="@+id/..." 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="visible" /> 


</RelativeLayout> 

的ID在這裏從我的SQLite數據庫,在那裏我通過我的聯繫表查詢得到他們的文字。

現在我想要點擊列表視圖中顯示的項目時發生的事情。點擊已註冊,但現在我想要一個Toast,它只顯示了正在傳入ID的數字(每行的主鍵)。

我不能擁有arrayindex +1,因爲我沒有通過id命令查詢。

這裏是我的onItemClick:

contactList.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 

      Toast.makeText(Contacts.this,"My unique rowID should be presented here.", 
         Toast.LENGTH_SHORT).show(); 

     } 

    }); 

我送的唯一ID對TextView的,因爲我相信它會讓點擊具體項目時可以更輕鬆地提取信息。

我的基本適配器:

public class ListViewCustomAdapter extends BaseAdapter { 

    public ArrayList<String> name; 
    public ArrayList<String> number; 
    public ArrayList<String> id; 

    public Activity context; 

    public LayoutInflater inflater; 

    public ListViewCustomAdapter(Activity context, 
      ArrayList<String> name, ArrayList<String> number, ArrayList<String> id) { 
     // TODO Auto-generated constructor stub 
     super(); 
     this.context = context; 
     this.name = name; 
     this.number = number; 
     this.id = id; 

     this.inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return name.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public long getItemId(int id) { 

     return id; 
    } 

    public class ViewHolder { 
     TextView txtViewName; 
     TextView txtViewNumber; 
     TextView txtViewId; 

    } 


    public View getView(int position, View convertview, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     ViewHolder holder; 
     if (convertview == null) { 
      holder = new ViewHolder(); 
      convertview = inflater.inflate(R.layout.listview_row, null); 

      holder.txtViewName = (TextView) convertview 
        .findViewById(R.id.name); 
      holder.txtViewNumber = (TextView) convertview 
        .findViewById(R.id.number); 
      holder.txtViewId = (TextView) convertview 
        .findViewById(R.id.id); 

      convertview.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertview.getTag(); 
     } 

     holder.txtViewName.setText(name.get(position)); 
     holder.txtViewNumber.setText(number.get(position)); 
     holder.txtViewId.setText(id.get(position)); 

     return convertview; 
    } 
} 

任何幫助,將不勝感激。

+0

是什麼問題? –

+0

是你的每個listview元素的XML? –

+0

是的,它是詹姆斯。而我的主鍵目前正在傳遞到@ + id/id。所以第一個項目會得到1,第二個會得到2,第三個會得到3等。如果我通過contact_id命令asc – Alex07

回答

1

現在我想要一個Toast,只顯示傳入ID的數字。 (每行的主鍵)。

如果你想從你的數據庫中的主鍵,(取決於適配器),它應該是id參數傳遞給onItemClick()

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // Use id 
    Toast.makeText(Contacts.this, id + "",Toast.LENGTH_SHORT).show(); 

    // Or you can find it yourself 
    TextView idTV = (TextView) view.findViewById(R.id.id); 
    Toast.makeText(Contacts.this, idTV.getText().toString(),Toast.LENGTH_SHORT).show(); 
} 

加成現在
你張貼您的適配器我看到您可以調整您的getItemId()方法以返回您的ArrayList<String> id中的數據以使id參數之三在onItemClick()工作:

public long getItemId(int position) { 
    return Long.parseLong(id.get(position)); 
} 

(你甚至可以改變從ArrayList<String>容器類型ArrayList<Long>如果它在你的背景下才有意義。)

+0

我現在也添加了適配器。當我使用getId並返回id時,它給了我arraylists索引。 – Alex07

+0

太棒了!這似乎工作。謝謝。 – Alex07

相關問題