0

I'm inflating linearlayouts in a linear layout, by doing this:

    LinearLayout parent = ((LinearLayout) findViewById(R.id.main_parent)); 


for (int i = 0; i < 3; i++) {
        View r = LayoutInflater.from(getBaseContext()).inflate(
                R.layout.main_item, parent, false);
        ImageView itemBg = (ImageView) r.findViewById(R.id.main_item_img);
        TextView title = (TextView) r.findViewById(R.id.main_item_title);
        TextView time = (TextView) r.findViewById(R.id.main_item_time);
        TextView persons = (TextView) r
                .findViewById(R.id.main_item_persons);
        String imgLink = withoTechnics.get(i).getImage_1()
                .replace("[HEIGHT]", parent.getHeight() + "")
                .replace("[WIDTH]", (int) parent.getWidth() / 3 + "");
        Log.v("--", imgLink);
        title.setText(withoTechnics.get(i).getTitle());
        time.setText(withoTechnics.get(i).getPreparation_time());
        persons.setText(withoTechnics.get(i).getPersons() + "");
        ImageLoader.getInstance().displayImage(imgLink, itemBg);
        parent.addView(r);
    }

Now can I set on item click listener by executing this code, or what I need to modify so I can set the onitemclicklistener for these objects?

Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117

2 Answers2

0

EDIT:

I suggest that you switch to a ListView with a custom adapter.

  • Then you could change the getView method of the adapter, to implement all your TextView's and ImageView's into a list item.

  • And for the clicks you could implement
    listView.setOnItemClickListener().

Here's a basic tutorial on how to create a ListView with a custom adapter.

Simas
  • 43,548
  • 10
  • 88
  • 116
0

You have an object of textview with below code:

TextView persons = (TextView) r
            .findViewById(R.id.main_item_persons);

So like any other events you can add click event's to this textview.

persons .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
   Toast.makeText(context,"textview person clicked",Toast.LENGTH_LONG).show();
}
});
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61