2014-04-01 63 views
0

下面表格佈局是我的代碼動態添加錶行,`無法獲取動態添加的行的行ID在Android的

for (int j = 0; j < list.size(); j++) {   


TableRow tableRow = new TableRow(getActivity()); 
     table_layout.addView(tableRow); 
     tableRow.setLayoutParams(new TableRow.LayoutParams(
       TableRow.LayoutParams.MATCH_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 
     tableRow.setId(j); 
     tableRow.setClickable(true); 
     tableRow.setOnClickListener(tablerowOnClickListener); 




TextView textView0 = new TextView(getActivity()); 
     textView0.setLayoutParams(new TableRow.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1)); 
     textView0.setText(currencySymbol.get(j)); 
     textView0.setGravity(Gravity.CENTER_HORIZONTAL 
       | Gravity.CLIP_VERTICAL); 
     tableRow.addView(textView0); 


     TextView textView1 = new TextView(getActivity()); 
     textView1.setLayoutParams(new TableRow.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1)); 

     textView1.setGravity(Gravity.RIGHT); 
     tableRow.addView(textView1); 
} 

,這裏是onclicklistener

OnClickListener tablerowOnClickListener = new OnClickListener() { 
    public void onClick(View v) { 
     //GET TEXT HERE 
     //String currenttext = ((TextView)v).getText().toString()); 
    Toast.makeText(getActivity(), "row selected" + View.getId , 30).show(); 

    } 
}; 

但是我不通過這種方法獲得行ID,我指的是this問題和this one also

回答

0

您需要使用v.getId()才能在OnClickListner中獲取點擊視圖的ID。

替換此

Toast.makeText(getActivity(), "row selected" + View,getId , 30).show(); 

通過這一個

Toast.makeText(getActivity(), "row selected --> " + v.getId() , 30).show(); 
+0

嘿嘿,謝謝BlackTiger,我應該嘗試這樣做。 – Sumit

相關問題