Aure
Aure

Reputation: 11

How come only the TextView of the first item in the ListView is being changed?

I set my adapter as two TextViews in a LinearLayout and I'm trying to make it so that every time i click on an item, the 2nd TextView appears. If I click on it again it disappears. However i cant figure out why only the TextView of the first item is disappearing and appearing no matter which item i click on.

My adapter for my ListView

public class adapter extends ArrayAdapter<User> {
public adapter(Context context, ArrayList<User> users) {
    super(context, 0, users);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    User user = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
    }
    // Lookup view for data population
    TextView question = (TextView) convertView.findViewById(R.id.question);
    TextView answer = (TextView) convertView.findViewById(R.id.answer);
    // Populate the data into the template view using the data object
    question.setText(user.name);
    answer.setText(user.hometown);
            // Return the completed view to render on screen
    return convertView;


    }
}

My Activity

public class MainActivity extends Activity {

private TextView image;
private TextView text;
private FrameLayout frame;
private String value;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardlist);
    populateUsersList();


}

private void populateUsersList() {
    // Construct the data source
    ArrayList<User> arrayOfUsers = User.getUsers();
    // Create the adapter to convert the array to views
    adapter adapter = new adapter(this, arrayOfUsers);
    // Attach the adapter to a ListView
    ListView listView = (ListView) findViewById(R.id.rlUsers);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            text = (TextView)findViewById(R.id.answer);
            if(text.getVisibility() == View.GONE) {
                text.setVisibility(View.VISIBLE);
            }else{
                text.setVisibility(View.GONE);
            }
        }

    });

My Array

public class User {
public String name;
public String hometown;

public User(String name, String hometown) {
    this.name = name;
    this.hometown = hometown;
}

public static ArrayList<User> getUsers() {
    ArrayList<User> users = new ArrayList<User>();
    users.add(new User("Harry", "San Diego"));
    users.add(new User("Marla", "San Francisco"));
    users.add(new User("Sarah", "San Marco"));
    return users;
}

Upvotes: 0

Views: 135

Answers (1)

panini
panini

Reputation: 2026

you are searching the entire view hierarchy for the first instance of a view with the id answer - which will be the first TextView in the list.

You can use the view supplied by the onItemClick callback to search just the view that was clicked:

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        text = (TextView)arg1.findViewById(R.id.answer);
        if(text.getVisibility() == View.GONE) {
            text.setVisibility(View.VISIBLE);
        }else{
            text.setVisibility(View.GONE);
        }
    }

Upvotes: 2

Related Questions