1

I use this code for my simple ListView now:

final ListView lv = (ListView)findViewById(R.id.apps_list);
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myString.split("\n")));

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });

and layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        style="?android:attr/listSeparatorTextViewStyle"
        android:id="@+id/separator_apps"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/apps" />


    <ListView
        android:id="@+id/apps_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/separator_apps" >

    </ListView>

</RelativeLayout>

But I need to add Image and CheckBox to every row to THIS ListView, could someone give me a good example?

Adam
  • 2,152
  • 4
  • 35
  • 45
  • http://www.vogella.de/articles/AndroidListView/article.html – Lalit Poptani Feb 17 '12 at 12:24
  • refer this:http://stackoverflow.com/questions/4797926/how-to-make-a-listactivity-with-custom-list-item-with-nested-clickable-button.It is similar to your problem.you just need to replace button by checkbox and Textview by image. – Hiral Vadodaria Feb 17 '12 at 12:25

4 Answers4

1

Found this great tutorial. Try this

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Swati Rawat
  • 1,729
  • 1
  • 19
  • 34
1

you need to create your own custom adapter to load image views and check box components to each item, learn some basics regarding creating custom adapter in android.

Rest, you would need to handle check box, see my earlier solution:

How to use checkbox in listview

Community
  • 1
  • 1
jeet
  • 29,001
  • 6
  • 52
  • 53
1

I prefer to use TableLayout instead of ListView for showing checkboxes, because the checked boxes will recycles everytime user scrolls the listview, so you'll have to cache all the data from the checkboxes. However if you want to use images or other static views the ListView can be used.

The code will look like this for checkbox and images, if you got some method to cache the data from checkboxes.

public class SDLibrary extends Activity {
ArrayList<LinearLayout> layout;
Context ctx;
ListView sdlistv;
private int[] bitmapArray;  
String[] mFiles;
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.sdlay);
    ctx=this;
    ListView sdlistv=(ListView) findViewById(R.id.list);
    layout=new ArrayList<LinearLayout>();

    for(int i=0;i<bitmapArray.length;i++){
        LinearLayout ll=new LinearLayout(this);
        layout.add(ll);
    }
    CustomAdapter ca=new CustomAdapter(this, R.id.linear, layout);
    sdlistv.setAdapter(ca);
    sdlistv.setFadingEdgeLength(40);
}
private class CustomAdapter extends ArrayAdapter<LinearLayout>{

    public CustomAdapter(Context context, int resources, List<LinearLayout> objects){
        super(context, resources, objects);
    }
    public View getView(int position, View convertView, ViewGroup parent){
        LinearLayout row;

        LayoutInflater mInflater = getLayoutInflater();
        if (convertView == null){
            row = getItem(position);
        }
        else{
            row = (LinearLayout) mInflater.inflate(R.layout.sdlay, null);
        }
        ImageView imageView =new ImageView(ctx);
        CheckBox cb=new CheckBox(ctx);
        imageView.setImageResource(bitmapArray[position]); //bitmapArray would be the array of images from the drawable
        imgData.add(imageView);
        row.addView(cb);
        row.addView(imageView);
        return row;
    }
}
}

sdlay.xml will look like this:-

<?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" >
<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>   

<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
 </LinearLayout>
</LinearLayout>
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
  • Note: I've given an old code that worked for me, except that recycling problem I've mentioned. If this code doesn't work, please let me know. – 0xC0DED00D Feb 17 '12 at 12:43
1

Thanks to @Lalit Poptani for tutorials.

I solved it with this code:

Create new class:

public class AppsArrayAdapter extends ArrayAdapter<String> {
    private final Context context;

    private final String DefaultApps = "def_apps";

    public AppsArrayAdapter(Context context, String[] values) {
        super(context, R.layout.apps, values);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
        String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
        String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");


        LayoutInflater inflater = (LayoutInflater)context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.apps, parent, false);
        TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
        CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
        text_apps.setText(prefNameDefaultApps.split("\n")[position]);

        return rowView;
    }   
}

Then create xml:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/text_apps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <CheckBox
        android:id="@+id/check_apps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

And in your Activity to onCreate (for example) add this code:

SharedPreferences myPrefsApps = AppsActivity.this.getSharedPreferences("myPrefsApps", MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");

            AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n"));
            setListAdapter(adapter);
Adam
  • 2,152
  • 4
  • 35
  • 45