【发布时间】:2014-05-02 20:06:55
【问题描述】:
我以编程方式在列表视图适配器中创建了一个表视图。为此,我首先创建了一个适配器布局:
<?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();
}
但它显示了我点击的位置。
我该怎么做,请指导我。
【问题讨论】:
标签: android android-listview android-custom-view android-adapter