为什么只更改ListView中第一项的TextView?

时间:2014-12-16 20:15:52

标签: android listview adapter

我将我的适配器设置为LinearLayout中的两个TextViews,并且我试图使它在每次单击某个项目时显示第二个TextView。如果我再次点击它就会消失。但是我无法弄清楚为什么只有第一个项目的TextView消失并出现,无论我点击哪个项目。

我的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;


    }
}

我的活动

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);
            }
        }

    });

我的数组

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;
}

1 个答案:

答案 0 :(得分:2)

您正在整个视图层次结构中搜索ID为answer的第一个视图实例 - 这将是列表中的第一个TextView

您可以使用onItemClick回调提供的视图来搜索被点击的视图:

    @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);
        }
    }
相关问题