1

I need to add a click event on each textview that is dynamically generated.

The textviews are in a form of array and I m adding textviews fetched from a json string.

Guide me on how to achieve it.

below is my code:

    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);
        }
}
5
  • 1
    have a listview with textviews for the rows. then you can use setOnItemClickListener for lsitview Commented Apr 14, 2014 at 15:46
  • u mean I ll have to take another listview where ill have to add the tableRow in which my textviews are being generated dynamically? Commented Apr 14, 2014 at 15:53
  • i mean there is no need for a table. just use a custom lsitview Commented Apr 14, 2014 at 15:55
  • bt I need to add textviews to a layout so I have used a tablelayout instead of listview and in a listview is each item clickable? Commented Apr 14, 2014 at 15:59
  • yes you can click each row of the lsitview or individual textviews Commented Apr 14, 2014 at 16:10

1 Answer 1

1
tx[i].setOnClickListener(this);

and then in on click method

public void onClick(View arg0) {


        if(arg0==tx[i]){

        }
}
Sign up to request clarification or add additional context in comments.

4 Comments

n what if I want to add different conditions to different textviews?will the above code work for it?
so should I add the above code in doInBackground() or the onClick() of button? because I need to display the data after the click event in the listview
I added it bt it says "i" should be declared final ! bt I is incremented so it wont be declared final. what can I do now?
for (int i = 0; i < items.length(); i++) { change this line to for (i = 0; i < items.length(); i++) { now declare i as global variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.