从ListView CustomAdapter中获取所选项目值

时间:2018-03-18 22:32:51

标签: android listview baseadapter custom-adapter

我有自定义适配器的listview。每行项目都有一个按钮,用于激活弹出菜单。当用户点击其中一个项目时,它应显示一些数据。

这是项目持有者类:

public class cNalog {

public String IDNalog;
public String NazivKlijenta;
public String OpisNaloga;
public String Napomena;
public int Hitnost;

public cNalog(String IDNalog, String nazivKlijenta, String opisNaloga, int hitnost) {
    this.IDNalog = IDNalog;
    NazivKlijenta = nazivKlijenta;
    OpisNaloga = opisNaloga;
    Hitnost = hitnost;
}

public cNalog() {}

public String getIDNalog() {
    return IDNalog;
}

public void setIDNalog(String IDNalog) {
    this.IDNalog = IDNalog;
}

public String getNazivKlijenta() {
    return NazivKlijenta;
}

public void setNazivKlijenta(String nazivKlijenta) {
    NazivKlijenta = nazivKlijenta;
}

public String getOpisNaloga() {
    return OpisNaloga;
}

public void setOpisNaloga(String opisNaloga) {
    OpisNaloga = opisNaloga;
}

public String getNapomena() {
    return Napomena;
}

public void setNapomena(String napomena) {
    Napomena = napomena;
}

public int getHitnost() {
    return Hitnost;
}

public void setHitnost(int hitnost) {
    Hitnost = hitnost;
}

}

以下是CustomAdapter中的getView方法,它扩展了BaseAdapter类:

 @Override
public View getView(final int i, View view, ViewGroup viewGroup) {

    final ViewHolder holder;

    if (view == null)
    {
        holder = new ViewHolder();

        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);

        view = mInflater.inflate(R.layout.popisnaloga_red, null);

        holder.btnPopUpMenu = (Button) view.findViewById(R.id.btnPopUpNalog);

        holder.btnPopUpMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PopupMenu popup = new PopupMenu(mContext, view);
                popup.getMenuInflater().inflate(R.menu.popup_nalog, popup.getMenu());
                //holder.uidNalog = mData.get(_i).getIDNalog();

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        Toast.makeText(mContext,
                                "Your Message", Toast.LENGTH_LONG).show();

                        switch (menuItem.getItemId()) {
                            case R.id.mnuActionInfo:
                                Log.i("Selekcija", mData.get(i).getIDNalog()); //Info(mData.get(i).getOpisNaloga());
                            default:
                                return false;
                        }
                    }
                });

                popup.show();
            }
        });
    }

    TextView tvOpisRada = (TextView)view.findViewById(R.id.viewNazivNaloga);
    tvOpisRada.setText(mData.get(i).getOpisNaloga());

    return view;
}


private class ViewHolder {

    protected Button btnPopUpMenu;      

}

当用户选择某个项目时,应该打印出UID。当我有3-4个项目时,这样可以正常工作......但是如果我扯下来让我说他选择了第12个项目,他会得到与第一个四个项目相同的UID。似乎当我在列表视图中扯下来时,列表中总有4个项目不是12个或更多......如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我认为您想要的最佳方式是使用recyclerview而不是listview For any help take a look here

答案 1 :(得分:1)

使用ViewHolder时你做错了。您的代码仅检查view = null然后为该位置充气。所以你不能存储观点的价值。

当查看view!= null并获取该视图时,您需要将行存储为viewholder的标记。

您可以查看该链接,了解Viewholder在listview中的工作方式:Implements ViewHolder on a ListView AndroidStudio

我知道有人建议您使用RecyclerView,但我想建议您首先了解viewHolder / listview。最危险的是复制代码,但不知道它是如何工作的。