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
I have given some links below. The type of list which you are making is called Custom Listview (you may be knowing that)
Please see the below links. You will get how to make it
Using ArrayAdapter:
https://www.javatpoint.com/android-custom-listview
With Checkbox - https://o7planning.org/10923/android-listview-with-checkbox-using-arrayadapter
Using BaseAdapter:
http://tutorialscache.com/custom-listview-using-baseadapter-android-example/ https://www.sanktips.com/2017/10/12/android-custom-listview-with-image-and-text-example/
Using ListAdapter:
https://www.tutorialspoint.com/how-to-create-a-custom-listview-in-android
I hope that I understood your question correctly Please tell me whether it is suitable for you or not.
Best Wishes
Ayesha Ganai VII
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.