I have an activity that loads text views into it. I add a click listener to these text views and want them to open up an activity with different values based on what I click. It ends up that no matter which I click, the same results show up, and more precisely, I use the same info in creating it - which I don't want to do.
public void setTextToTextView (JSONArray jsonArray)
{
RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_main);
String s = "";
for (int i = 0; i < jsonArray.length(); i++) {
TextView info = new TextView(this); //actually really confused as to what the context I'm setting is - why this? Just saw other people do it like so
JSONObject json = null;
try {
json = jsonArray.getJSONObject(i);
s = s + "ID : " + json.getString("Id") + " Parent: " + json.getString("Parent") +
" Content: " + json.getString("Content") + " User: " + json.getString("User") +
" Timestamp: " + json.getString("Timestamp") + "\n\n";
} catch (JSONException e) {
e.printStackTrace();
}
info.setText(s);
try {
info.setId(Integer.parseInt(json.getString("Id")));
} catch (JSONException e) {
e.printStackTrace();
}
info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
NewActivity.class);
myIntent.putExtra("key", v.getId()); //this is always the same
startActivity(myIntent);
}
});
layout.addView(info);
}
}
Using two text views, this results in the ID of the second view to always be the value of the key,value pair in the activity I start. I'm not sure what I'm doing wrong at the moment. I believe my problem is in this section, as I can't see where else it might come from.
Any help or suggestions on my code in general would be welcomed. Thank you.
This still isn't solved, so I'll focus on the problem area:
info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this,
NewActivity.class);
myIntent.putExtra("key", v.getId()); //this is always the same
startActivity(myIntent);
}
});
No matter what changes I make, this will always give me the exact same v.getId/v.getTag - every time.