0

I am writing an android app, and I am having trouble getting a row's position in ListView with CheckBox.

Here is a screenshot on the emulator: https://dl.dropbox.com/u/24223262/help.png

I already made the ListView with CheckBoxes. But I can't figure out how find the row's position for each checked entry. And once I get the checked positions, I want to implement a Remove Selected action.

Please help!

Here is my code:

public class ListTest extends ListActivity implements OnCheckedChangeListener {
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        //--- Importing StringsArray---     
        Bundle gotBasketScanner = getIntent().getExtras();
        String[] StringArrayHistory = gotBasketScanner.getStringArray("fromHistory");

        // ---Make Adapter---
        setListAdapter( new ArrayAdapter<String>(ListTest.this,
                    android.R.layout.simple_list_item_multiple_choice,
                    StringArrayHistory));

        // ---Put Array into a ListView and add Checkboxes to Listview
        ListView list = getListView();
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
user1465656
  • 153
  • 3
  • 10

4 Answers4

1

hey check this it will help you...

http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php

Android ListView with Checkbox and all clickable

http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/

Community
  • 1
  • 1
Goofy
  • 6,098
  • 17
  • 90
  • 156
  • yea.. i researched A LOT before asking.. i read those pages (and a lot more) already but I can't figure it out =/ I am trying to use the CHOICE_MODE_MULTIPLE – user1465656 Jun 29 '12 at 04:46
1

Use onItemClick() in instead of onCheckedChangeListener...

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
         Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
                              Toast.LENGTH_SHORT).show();
    }
}

This is the better way to get the clicked position and id.

I hope this will solve your problem.

EDIT

I have updated the following line:-

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

now try ur code.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
himanshu
  • 1,990
  • 3
  • 18
  • 36
  • thanks!! I am feeling good about this direction. I am getting an error on the "new OnItemClickListener". It says "Cannot instantiate the type AdapterView.OnItemClickListener"... any ideas on solving? I will continue trying – user1465656 Jun 29 '12 at 05:24
  • thank you! I ended up solving my problem another way - took me a long time though. Thanks again! – user1465656 Jun 30 '12 at 08:49
0

You can use PrefrenceScreen with CheckBoxPreference for easier and scalellable implementation

RPB
  • 16,006
  • 16
  • 55
  • 79
0

You can use CheckedTextView to implement the same:

main.xml

  <?xml version="1.0" encoding="utf-8"?>
  <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:clickable="true"
    android:focusable="true"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple">
  </CheckedTextView>

addlist.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

  <ListView
     android:id="@+id/mainListView"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     >
  </ListView>

  <Button android:id="@+id/click"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click"/>
 </LinearLayout>

And here is the activity GetCheckedTextPositionActivity. On click of the button you will get the positions of the listitem checked :

 public class GetCheckedTextPositionActivity extends Activity {

private ListView myList;
private EfficientAdapter myAdapter;
private ArrayList<String> data;
private LayoutInflater mInflater;
private Button click;
private ArrayList<Integer> positions;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addlist);

    positions = new ArrayList<Integer>();

    data = new ArrayList<String>();
    data.add("Bangalore");
    data.add("Delhi");
    data.add("Patna");
    data.add("Mumbai");

    click = (Button)findViewById(R.id.click);
    mInflater = LayoutInflater.from(getApplicationContext());
    myList = (ListView) findViewById(R.id.mainListView);
    myAdapter = new EfficientAdapter(data, getApplicationContext());
    myList.setAdapter(myAdapter);

    click.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(positions.size() != 0){
                for(Integer pos : positions){
                Log.e("Positions checked :"," Positions :"+pos);
                }
            }

        }
    });

}

public class EfficientAdapter extends BaseAdapter {
    private ArrayList<String> myData;

    public EfficientAdapter(ArrayList<String> mData, Context ctx) {
        // TODO Auto-generated constructor stub
        this.myData = mData;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myData.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final int currentPos = position;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.main, null);
        }

        ((CheckedTextView) convertView).setText(myData.get(position));
        ((CheckedTextView) convertView).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        ((CheckedTextView) v).toggle();
                        if(((CheckedTextView) v).isChecked()){
                            positions.add(currentPos);
                        }
                    }
                });
        return convertView;
    }

}
 }

Hope this helps.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
AndroGeek
  • 1,280
  • 3
  • 13
  • 25
  • Thanks! and I saw a this method before I asked my question. The reason I did not use this is that 1) my array is imported, so I can't do the "data.add" individual reference, and 2) I didn't understand this method.. – user1465656 Jun 29 '12 at 06:16
  • 1
    What method dint you understand? What do you mean by ArrayList is imported? Even if you are getting an ArrayList, use it directly instead of creating an ArrayList and adding Item to it. – AndroGeek Jun 29 '12 at 06:50
  • Thanks for you help! I figured out the code on my own after several hours yesterday and today! I didn't understand this method because there was 1) a for-loop using Integers to reference the position (My situation wasn't too fitting for this setup); 2) I didn't understand BaseAdapter. But anyway, I solved my problem now! – user1465656 Jun 30 '12 at 08:48