Reputation: 29
I have table 'notes' which has N notes. For every note, I want to print the value of 'title' column in separate textview which i create dynamically. So, for N notes, I will have N textviews. Now, Whenever the user clicks on any textView, I want to display all other data of that row which has the clicked title.
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;
}
Upvotes: 0
Views: 74
Reputation: 29
I spent 24 hours to find the solution to this problem believing the problem to be related to some listener-specific-logic.
But The problem was the bloody Semicoln ';' on this line that caused all the problems
if(res1.getString(1).equals(title)); // Here :'(
{
final String text1=res1.getString(0);
Intent i=new Intent("com.example.dell.todolister.ViewNoteActivity");
i.putExtra("TextBox1",text1);
startActivity(i);
break;
}
Upvotes: 0
Reputation: 537
You should use id to identify textview to display text,use this --> v.getid()
inside onclick(View v)
Upvotes: 0
Reputation: 3074
I think you need to give id to text view.
initialize global variable like int i = 1;
then in createNewTextView, textView.setId(i)
at the end before you return a view increment i = i + 1;
so for next textView you will have different id for text view.
Upvotes: 1