如何在Listview的Base适配器中添加setOnclick

时间:2018-04-12 17:15:06

标签: android listview onclicklistener baseadapter

我想在列表视图中添加一个呼叫按钮。我尝试在列表行文件中添加按钮,但无法在Java文件上添加Onclick。发生了更多错误。有没有按钮的其他方法吗?有人请帮助我。  谢谢。

以下是适配器代码

class adapter extends BaseAdapter {
        LayoutInflater Inflater;
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return place.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            Inflater=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=Inflater.inflate(R.layout.blood_lst,null);
           Viewholder holder=new adapter.Viewholder();

            holder.pl=(TextView)convertView.findViewById(R.id.bld_name);
            holder.pl.setText(place.get(position));

            holder.in=(TextView)convertView.findViewById(R.id.bld_nm);
            holder.in.setText(incharge.get(position));

            holder.em=(TextView)convertView.findViewById(R.id.bld_em);
            holder.em.setText(email.get(position));

            holder.ph=(TextView)convertView.findViewById(R.id.bld_phn);
            holder.ph.setText(phone.get(position));

            holder.ph=(TextView)convertView.findViewById(R.id.bld_grp);
            holder.ph.setText(Bld.get(position));

            return convertView;
        }
        class Viewholder{
            TextView pl;
            TextView in;
            TextView em;
            TextView ph;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

如果您只想在该列表视图中添加一个按钮,则需要将其添加到项目xml文件中,如下所示:

<Button
        android:id="@+id/btn_affiche_reu"
        android:layout_width="wrap_content"
        android:layout_height="35dp"

        android:background="@drawable/buttonstylenormal_user"

        android:textColor="@color/white"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:text="Détail"
        android:textAllCaps="false"
        android:layout_marginRight="10dp"/>

然后在你的类适配器中,在getView方法

中添加这一行
Button bt = (Button) rowView.findViewById(R.id.btn_affiche_reu);

            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //here you add your action 
                }
            });

我希望这可以帮助你。

答案 1 :(得分:1)

onClick添加到convertView,如下所示:

convertView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // do stuff here
    }
});

答案 2 :(得分:0)

Instaed of writing `onClickListener()` , you write `onItemClickListener()` for your ListView. for exemple:



     listView.setOnItemClickListener(new OnItemClickListener()
    {
          @Override
          public void onItemClick(AdapterView<?> arg0, View view, int pos,

 long viewId = view.getId();
 if (viewId == R.id.button1) {
        Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
    } else if (viewId == R.id.button2) {
        Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "ListView clicked" + id, Toast.LENGTH_SHORT).show();
    } 


          }
    });