简体   繁体   中英

How can i add favourite (CheckBox/ToggleButton/ImageView) to my ListView?

Please help me.

How can i add favourite (CheckBox/ToggleButton/ImageView) to my ListView? ... I have never added a favourite button before so I would like to add one in listView with CheckBox or ToggleButton or ImageView.

Hope everyone understand my question

My code:

This is my ListAdapter

    static class ListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private Context ctx;

        public ListAdapter(Context context) {
            inflater = LayoutInflater.from(context);
            ctx = context;
        }

        public int getCount() {
            return RecipeName.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;

            if(convertView == null){
                convertView = inflater.inflate(R.layout.row, null);
                holder = new ViewHolder();
                holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);


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

            holder.txtRecipeName.setText(RecipeName[position]);

            return convertView;
        }

        static class ViewHolder {
            TextView txtRecipeName;
        }
    }

This is my row xml

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="1"
    android:gravity="center_horizontal"
    android:background="@drawable/row">

    <TextView
        android:id="@+id/txtRecipeName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/text"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_weight="0.04"
        android:typeface="normal"
        android:background="@drawable/button2"/>
</LinearLayout>

For adding image u can use ImageView same as TextView in the.xml file..

Just add the src of the image after it "android:src="@drawable/beach" like this. Here "@drawable/beach" is the path of image.

`<ImageView

     android:id="@+id/photo_image_view" //for id

     android:layout_width="match_parent"

     android:layout_height="match_parent"

     android:scaleType="centerCrop" //this will display image in center

     android:src="@drawable/beach" /> //path of image`

For Toggle Button..use the following code in.xml file

<Switch

android:id="@+id/toggle_switch" //give id so can use this in java code later

android:layout_width="wrap_content" //set width

android:layout_height="wrap_content" //set height

android:text="Toggle Button" // string to be displayed

android:textAppearance="?android:textAppearanceSmall" />

How it will look Toggle Button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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