user2829650
user2829650

Reputation: 35

setonclicklistener doesn't respond to the custom listview

 customlistadapter adapter1=new customlistadapter(userfrontpage.this, usernameq,groupnames, imgid);
            ListView list = (ListView) findViewById(R.id.listView);

            list.setAdapter(adapter1);


            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    TextView extratxt = (TextView) findViewById(R.id.textView1);
                    String text = extratxt.getText().toString();
                    TextView txtTitle = (TextView) findViewById(R.id.item);
                    String texx = txtTitle.getText().toString();
                    if(text.equals("personal")) {
                        String friend = ((TextView) view).getText().toString();
                        XMPPManager.getInstance().connect(username, password);
                        startMainActivity(friend,"personal");
                    }
                    else{
                        String friend = ((TextView) view).getText().toString();
                        XMPPManager.getInstance().connect(username, password);
                        startMainActivity(friend,"group");
                    }
                }

and following is customlistadapter:

public class customlistadapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
private final String[] groupnamess;
public customlistadapter(Activity context, String[] itemname,String[] groupnames, Integer[] imgid) {
    super(context, R.layout.mylist, itemname);
    // TODO Auto-generated constructor stub

    this.context = context;
    this.itemname = itemname;
    this.imgid = imgid;
    this.groupnamess= groupnames;
}

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.mylist, null, true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
    if(position< itemname.length-groupnamess.length) {
        txtTitle.setText(itemname[position]);
        imageView.setImageResource(imgid[0]);
        extratxt.setText("personal");
    }
    else {
        txtTitle.setText(itemname[position]);
        imageView.setImageResource(imgid[1]);
        extratxt.setText("group");
    }
    return rowView;
}

there are 5 rows that are shown. whatever i click the fourth one or fifth one,,the id and position is correct. however, String text and texx refer to the first one. why is it happened?? please help.thank you very much.

Upvotes: 0

Views: 63

Answers (3)

Joe Plante
Joe Plante

Reputation: 6368

This looks like a scoping issue (what gets returned when you call findViewById()). Basically it's calling findViewById on the Content View set to the Activity (setContentView). It's VERY hard to predict what TextView just got selected. I'm surprised that the app didn't crash

On the following line (the tldr answer):

 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

You may notice there's several variables involved with the callback. To get that particular TextView, try view.findViewById(_____) (Probably what Andro Selva was trying to convey).

What's most likely happening is that your findViewById() is returning one of many TextViews that's on the screen. That's because each Id is an integer associated with the TextView. However, every TextView in each line in your ListView adapter has the exact same Id.

findViewById goes to the top-most View, checks all of the Ids of its children, and if it finds one, returns that View. This includes children of children, like in a ListView. In the case of the onItemClick() call, directing it to view.findViewById starts at a level closer to what you are looking for

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54322

The way you initialize the TextView is not correct.

TextView extratxt = (TextView) findViewById(R.id.textView1);

this should be changed to this

TextView extratxt = (TextView)view. findViewById(R.id.textView1);

Upvotes: 1

DeRagan
DeRagan

Reputation: 22920

Change this

TextView txtTitle = (TextView) findViewById(R.id.item);
String texx = txtTitle.getText().toString();

to

 String texx = itemname[position]

Upvotes: 0

Related Questions