【发布时间】:2016-10-11 16:23:32
【问题描述】:
我有一张表“笔记”,里面有 N 个笔记。对于每个注释,我想在我动态创建的单独文本视图中打印“标题”列的值。所以,对于 N 个笔记,我将有 N 个文本视图。 现在,每当用户单击任何 textView 时,我都想显示具有单击标题的那一行的所有其他数据。
问题:当我对这些动态创建的文本视图使用 onClickListener 时,当用户点击任何文本视图时,它总是生成最后一行(或最后一个动态创建的文本视图)的数据
public void dataview(){
//onView is a userdefined function which simply executes a select query
Cursor res=db.onView();
len=res.getCount();
//below code is, If no notes stored in the table.
if(res.getCount()==0)
{
Toast.makeText(ViewActivity.this,"No Notes.",Toast.LENGTH_SHORT).show();
return;
}
//parsing the select query result using the Cursor res
while(res.moveToNext()) {
relative = (LinearLayout) findViewById(R.id.activity_view);
relative.addView(createNewTextView(res.getString(1).toString()));
}
}
public TextView createNewTextView(String text){
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.setMargins(0,10,0,0);
//creating TextView and assigning properties here
textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
index++;
textView.setTextSize(18);
textView.setBackgroundColor(Color.parseColor("#ff0099cc"));
// onClick Action here
final String title=textView.getText().toString();
textView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res1=db.onView();
while(res1.moveToNext()){
if(res1.getString(1).equals(title));
{
final String text1=res1.getString(0);
Intent i=new Intent("com.example.dell.todolister.ViewNoteActivity");
i.putExtra("TextBox1",text1);
startActivity(i);
break;
}
}
}
}
);
return textView;
}
【问题讨论】:
-
你为什么不用listview或者recyclerview?
标签: android