【问题标题】:Android : Listview First button selected errorAndroid:Listview第一个按钮选择错误
【发布时间】:2016-06-09 00:53:25
【问题描述】:

当 listview Start 我发现第一个按钮被选中时? 我尝试使用 listview 进行单击按钮事件,但我不知道原因!我怎样才能让它像列表中的所有按钮一样默认? 请看一下图片。

 @Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    View row;
    row = convertView;
    final ContactHolder contactHolder;
    if (row == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row, parent, false);
        contactHolder = new ContactHolder();
        contactHolder.tx_id = (TextView) row.findViewById(R.id.usersName2);
        contactHolder.tx_id = (TextView) row.findViewById(R.id.usersName2);
        contactHolder.loadId = (TextView) row.findViewById(R.id.loadMoreID);
        contactHolder.tx_name = (TextView) row.findViewById(R.id.usersName);
        contactHolder.image_tx = (ImageView) row.findViewById(R.id.usersPic);
        contactHolder.sug_add = (Button) row.findViewById(R.id.sug_id);

        row.setTag(contactHolder);


    } else {
        contactHolder = (ContactHolder) row.getTag();

    }
    final Contacts_Sug contacts = (Contacts_Sug) this.getItem(position);
    contactHolder.image_tx.setImageResource(R.mipmap.ic_launcher);
    contactHolder.tx_id.setText(contacts.getId());
    contactHolder.tx_name.setText(contacts.getName());
    imgLoader.DisplayImage(contacts.getImage(), contactHolder.image_tx);

    contactHolder.tx_name.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            contacts.setSelectedPosition(position); //Set position here
            contactHolder.sug_add.setText("Selected");
        }


    });
    if(contacts.getSelectedPosition() == position){
        contactHolder.sug_add.setText("Selected");
    } else{
        contactHolder.sug_add.setText("Follow");

    }

    return row;
}


public class ContactHolder {
    TextView tx_id, tx_name,loadId;
    ImageView image_tx;
    public Button sug_add;

}

【问题讨论】:

    标签: android listview


    【解决方案1】:

    这是因为默认情况下selectedPos的值为“0”。

    更改您的 Contacts_Sug 类以将 selectedPos 成员变量初始化为 -1。

    private int selectedPos = -1;
    

    【讨论】:

    • 感谢 agian 先生的完美回答和时间 :)
    猜你喜欢
    • 2011-08-30
    • 2016-12-12
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2016-09-24
    相关资源
    最近更新 更多