【发布时间】:2014-04-14 23:58:44
【问题描述】:
我需要在每个动态生成的文本视图上添加一个点击事件。
文本视图采用数组形式,我正在添加从 json 字符串中获取的文本视图。
指导我如何实现它。
下面是我的代码:
Button b =(Button) findViewById(R.id.start);
b.setOnClickListener(this);
starters = (TextView) findViewById(R.id.textView1);
starters.setOnClickListener(this);
tl = (TableLayout) findViewById(R.id.tablelayout1);
rl = (RelativeLayout) findViewById(R.id.rel1);
itemList = new ArrayList<HashMap<String, String>>();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
Intent in = new Intent(getApplicationContext(),
SingleContactActivity.class);
in.putExtra(TAG_Name, name);
startActivity(in);
}
});
new Getitems().execute();
}
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.textView1:
ontextview1click();
break;
case R.id.start:
try {
onbuttonclick();
} catch (JSONException e) {
e.printStackTrace();
}
break;
}
}
private void onbuttonclick() throws JSONException {
TableRow[] tr = new TableRow[items.length()];
TextView[] tx = new TextView[items.length()];
TableLayout tl = (TableLayout) findViewById(R.id.tablelayout1);
for (int i = 0; i < items.length(); i++) {
JSONObject c = items.getJSONObject(i);
String category = c.getString(TAG_Cat);
tx[i] = new TextView(S1.this);
tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tx[i].setText(category);
tr[i] = new TableRow(S1.this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i],new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tl.addView(tr[i],new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
private void ontextview1click() {
if (key==0){
key=1;
lv.setVisibility(View.VISIBLE);
}
else if(key==1) {
key=0;
lv.setVisibility(View.GONE);
}
}
【问题讨论】:
-
有一个列表视图,其中包含行的文本视图。然后您可以将 setOnItemClickListener 用于 lsitview
-
你的意思是我将不得不采取另一个列表视图,我必须添加动态生成我的文本视图的 tableRow?
-
我的意思是不需要桌子。只需使用自定义 lsitview
-
bt 我需要将文本视图添加到布局中,因此我使用了表格布局而不是列表视图,并且在列表视图中每个项目都是可点击的?
-
是的,您可以单击 lsitview 或单个文本视图的每一行
标签: android