0
我以編程方式在列表視圖適配器中創建了一個表視圖。對於第一次我創建了一個適配器佈局:Android:自定義列表視圖,適配器中的表視圖
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:stretchColumns="*"
android:id="@+id/tablelayout" >
</ableLayout>
那我已經在做的適配器是:
public class DemoAdapter extends ArrayAdapter<String> {
//Global Variable declaration
Context myContext;
String[] key, value, loop;
public DemoAdapter(Context context, String[] key, String[] value, String[] loop){
super(context, R.layout.demo_screen_adapter, loop);
this.myContext = context;
this.key = key;
this.value = value;
this.loop = loop;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
if (row == null){
// get reference to the activity
LayoutInflater inflater = ((Activity) myContext).getLayoutInflater();
// Inflate the custom text which will replace the default text view
//list_item is a simple linear layout which contain textView1 in it.
row = inflater.inflate(R.layout.demo_screen_adapter, parent, false);
}
TableLayout tableLayout = (TableLayout) row.findViewById(R.id.tablelayout);
for(int i=0; i<5; i++){
TableRow tableRow = new TableRow(myContext);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView tvKey = new TextView(myContext);
tvKey.setText(key[(position*5)+i]);
tvKey.setTextColor(Color.WHITE);
tvKey.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tableRow.addView(tvKey);
TextView tvValue = new TextView(myContext);
tvValue.setText(value[(position*5)+i]);
tvValue.setTextColor(Color.WHITE);
tvValue.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tableRow.addView(tvValue);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
}
return row;
}
}
這是工作好。在我的每個列表項中都有一個包含兩列和五行的表視圖。
現在問題是:
我想在列表項的點擊獲取所述特定的值。我無法這樣做。 我的嘗試是:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast.makeText(this, ""+(String) arg0.getItemAtPosition(position), Toast.LENGTH_LONG).show();
}
但它顯示了我點擊的位置。
我該怎麼做,請指導我。