1

Been trying with different type of listview. I'm now able to display it in a list, implement search filter. However, checking the checkbox will check multiple checkboxes which isn't what I want. Thus, I'm faced with the followed problem.

  1. How can I modify it such a way that it check only the row that I wanted with multiple select?

  2. I will shift this code to a fragment, so how do i refresh the 'listview' each time I want to change the 'clerkship' which will retrieve the query from database again and repopulate the list. Will I be able to call the method or anything located in the fragment from the activity to trigger a refresh?

  3. Retrieve the IDs stored for those that has the checkbox checked.

Thanks so much!

Activity - Will move it to fragment soon.

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_encounter);
        encouterlist = new ArrayList<HashMap<String, String>>();
        myContext = this;
        encounterDB = new eAndPDbAdapter(myContext);
        encounterDB.open();
        Cursor mCursor = encounterDB.retrieveAllEncounterCursor();
        int count = mCursor.getCount();

        displaySharedPreferences();
        if ((mCursor != null) && (mCursor.getCount() > 0)) {
            mCursor.moveToFirst();

            do {
                // Get the data
                // Convert the initial to String
                if (clerkShip.equals(mCursor.getString(2)))
                {
                String eID = mCursor.getString(1);
                String encounter_name = mCursor.getString(3);
                String encounter_type = mCursor.getString(4);

            //  ename = (TextView) findViewById(R.id.eid);
            //  this.ename.setTextColor(Color.RED);
                //indicate that its successful
                Log.i("Successful retrival of", encounter_name);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_E_ID, eID);
                map.put(TAG_E_NAME, encounter_name);
                map.put(TAG_E_TYPE, encounter_type);

                // Add it to the array such that i can handle the array
                // afterwhich
                encouterlist.add(map);
            }
            }
            // move to the next row
                        while (mCursor.moveToNext());

        }

        filterText = (EditText) findViewById(R.id.encountersSearch);
        filterText.addTextChangedListener(filterTextWatcher);
        // need to link the layout
        encounterList = (ListView) findViewById(R.id.encounterslist);

        cBox = (CheckBox) findViewById(R.id.listCheck);
        encounterList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            ((CheckBox)parent.getChildAt(position).findViewById(R.id.listCheck)).setChecked(true);

            }
        }
        );
this.adapter = new SimpleAdapter(EncounterActivity.this,
        encouterlist, R.layout.list_v, new String[] { TAG_E_ID,
                 TAG_E_NAME, TAG_E_TYPE,
                TAG_E_REQUIRED_ATTEMPTS }, new int[] { R.id.eid,
                R.id.ename,
                R.id.etype });

this.encounterList.setAdapter(adapter);


};


private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        ((Filterable) adapter).getFilter().filter(s);
    }

};

public void displaySharedPreferences() {
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(EncounterActivity.this);

    String listPrefs = prefs.getString("listpref", "No Clerkship Selected");

    StringBuilder builder2 = new StringBuilder();
    builder2.append(listPrefs);

clerkShip = builder2.toString();

}

Activity_Encounter.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <EditText 
     android:id="@+id/encountersSearch"
     android:hint="Enter encounter here"
    android:layout_width="fill_parent" 
    android:windowSoftInputMode="stateHidden"
    android:imeOptions="actionSearch"
                android:singleLine="true"
    android:layout_height="wrap_content"/>

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


</LinearLayout>

list_v.xml

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

<TextView
    android:id="@+id/eid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/listCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:clickable="false"
        android:focusable="false" >
    </CheckBox>

    <TextView
        android:id="@+id/ename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left" />
</LinearLayout>

<TextView
    android:id="@+id/etype"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

<TextView
    android:id="@+id/erequiredattempts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

    </LinearLayout>
Walalala
  • 77
  • 8
  • ListView.CHOICE_MODE_MULTIPLE read more: http://stackoverflow.com/a/5926002/2412041 – Luc Jan 28 '14 at 03:46

1 Answers1

0

You can save the check states in an array and in getview set the checked/unchecked state base on position. Lets say 10 items, have a bool array of 10 items, and this stores checked state or not. then in getView, check this array using position variable and set the UI state

Pulkit Sethi
  • 1,325
  • 1
  • 14
  • 22
  • i'm faced with listview recycle view issue. Thus, if i check item 1 and scroll down. item 17 and 33 will be checked as well. – Walalala Jan 28 '14 at 03:57
  • thats wat i am saying if u have 100 items u need to have boolean array of 100 items, arr[0] = true (checked), in ur getView do if(arr[position] {// set checkbox checked} // else { //uncheck}), the position is coming from the getView arguments. I think u would have a click listener for checkbox everytime that is checked, set that particular instance to true – Pulkit Sethi Jan 28 '14 at 04:00
  • @Walalala you can use a `SparseBooleanArray`. Check Romain Guy's solution @ https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M – Raghunandan Jan 28 '14 at 04:14
  • again same as i what posted – Pulkit Sethi Jan 28 '14 at 04:24
  • @PulkitSethi you are better off posting a solution with code. – Raghunandan Jan 28 '14 at 04:27