简体   繁体   English

Android ListView获取按钮行单击

[英]Android ListView Get row on button click

I have a custom adapter class for a listview and I want to be able to access the content of a specific row by clicking a button on it. 我有一个用于列表视图的自定义适配器类,我希望能够通过单击特定行的按钮来访问特定行的内容。 I tried to create a ViewHolder, but I get a NPE error when I try to click it. 我试图创建一个ViewHolder,但是当我尝试单击它时出现NPE错误。

static class ViewHolder {
    TextView camera;
    TextView players;
    TextView max_players;
    ImageView privata;
    Button Buton;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    String variabile[] = getItem(position).split("\\s+");
    LayoutInflater linflater = LayoutInflater.from(getContext());
        View customView = linflater.inflate(R.layout.custom_row, parent, false);
        final ViewHolder holder = new ViewHolder();
        holder.camera = (TextView) customView.findViewById(R.id.Nume);
        holder.players = (TextView) customView.findViewById(R.id.players);
        holder.max_players = (TextView) customView.findViewById(R.id.max_players);
        holder.privata = (ImageView) customView.findViewById(R.id.privata);
        holder.Buton = (Button) customView.findViewById(R.id.Buton);
        holder.camera.setText(variabile[0]);
        if (!variabile[1].equals("true")) {
            parola = false;
            holder.privata.setVisibility(View.INVISIBLE);
        }
        holder.players.setText(variabile[2]);
        holder.max_players.setText(variabile[3]);
        room_id = variabile[4];
        nume = variabile[5];
        holder.Buton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                hash = new HashMap<String, String>();
                hash.put("name", nume);
                hash.put("room", room_id);
                if (intra) {
                    holder.Buton.setText("Iesi");
                    site = siteul + "/join";
                    intra = false;
                } else {
                    holder.Buton.setText("Intra");
                    site = siteul + "/leave";
                    intra = true;
                }
                new ATask().execute(site);
            }
        });
    return customView;
}

You need to check if the convertView is null so it is already has been visited or not then store the holder in tag Like 您需要检查convertView是否为null,以便已经被访问过,然后将其保存在标签Like中。

    ViewHolder holder;
    if (convertView == null) {
        LayoutInflater linflater = LayoutInflater.from(getContext());
        holder = linflater.inflate(R.layout.custom_row, parent, false);....
        convertView.setTag(holder);
    }else{
      holder = (ViewHolder) convertView.getTag();
    }//Common code

When using the ViewHolder pattern, you should check if the convertView in null or has been created before, in the getView method, and after that use setTag and getTag methods. 使用ViewHolder模式时,应检查是否在null之前创建了convertView,或者是否已经在getView方法中,之前以及之后使用setTaggetTag方法创建了convertView。 like this : 像这样 :

    if (convertView == null) 
    {
        LayoutInflater linflater = LayoutInflater.from(getContext());
        convertView = linflater.inflate(R.layout.your_list_item_view, parent, false);
         viewHolder.textView = (TextView)convertView.findViewById([the id]);
         .
         .
         .

        convertView.setTag(holder);
    }
    else
    {
      holder = (ViewHolder) convertView.getTag();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM