Adding button to each row in listview

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I want to add button to each row of my listview. I created an XML file called row.xml in my layout folder and added two textviews and a button in that file. But when a button is added, I am unable to click the item of listview. I'm only able to click the button. Here is row.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     >       <TextView         android:id="@+id/text11"          android:layout_alignParentLeft="true"         android:layout_width="wrap_content"         android:layout_height="wrap_content"   android:textSize="25sp"   android:textColor="#000000"          />           <TextView         android:id="@+id/text2"          android:layout_alignParentLeft="true"         android:layout_width="wrap_content"         android:layout_height="wrap_content"   android:textSize="10sp"     android:textColor="#000000"          />           <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> 

I want to refer to textviews and button in my activity. Please help me and suggest some ideas.

回答1:

look at Android - Dynamically Add Views into View, add and remove views in android dynamically? and Android, add new view without XML Layout,Android, Part III: Dynamic Layouts

Hope this will help you.

Thanks,

EDIT: For button click event in listview just check Handling Button clicks in a ListView Row,click event for the button inside listview in android,



回答2:

I had a similar issue. The simple trick is to add android:focusable="false" to your Button.



回答3:

You can use a custom adapter (extending an array adapter is fairly simple). In the getView method, set a onClickListener on your TextView, this way both your button and the other parts of the ListItem will respond to touch.



回答4:

If you want to experiment something new for problem check LINK

Hope this will be helpfull.



回答5:

you must add focusable="false" and you can

<Button         android:id="@+id/bt_do"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:gravity="center"         **android:focusable="false"** /> 

in your adapter

public class MyAdapter extends BaseAdapter {     private Context context;     private List< Objet > objects;     private OnClickListener listener;      public MyAdapter(Context context, List<Objet> objects,             OnClickListener listener) {         this.context = context;         this.objects = objects;         this.listener = listener;         // TODO Auto-generated constructor stub     }      @Override     public View getView(int position, View convertView, ViewGroup parent) {         if (convertView == null) {             LayoutInflater infalInflater = ((Activity) context)                     .getLayoutInflater();             convertView = infalInflater.inflate(R.layout.my_line_list, null);         }         Button bt_do=(Button)convertView.findViewById(R.id.bt_do);         bt_do.setOnClickListener(listener);         return convertView;      }      @Override     public boolean hasStableIds() {         return true;     }      @Override     public int getCount() {         // TODO Auto-generated method stub         return objects.size();     }      @Override     public Object getItem(int position) {         // TODO Auto-generated method stub         return objects.get(position);     }      @Override     public long getItemId(int position) {         // TODO Auto-generated method stub         return position;     }  } 

and in your activity create un adapter and implement listener of button.



回答6:

Please check this link...

http://commonsware.com/Android/excerpt.pdf

In that pdf in page 104, he added an image in every row of a list view. You replace it with button..

Hope this solves your problem.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!