3 Replies - 1693 Views - Last Post: 07 May 2015 - 03:29 AM

#1 frankwright1   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 07-May 12

Custom list view with checkbox

Posted 02 May 2015 - 07:22 AM

I am trying to create a custom listview with a checkbox and a string list of "observations" retrieved from my sqlite database. The idea is that when I click on the "retrieve" button, all checked items are shown in a toast message.

I can populate the listview through my customadapter just fine, butit doesnt seem to recognise the status of each checkbox, as no toast messages are shown, regardless of whether they are checked.

Please can someone show me where I am going wrong?

Here is my custom listview xm that I have called list_o:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:id="@+id/obsgrid">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chkbx"
        android:layout_row="0"
        android:layout_column="0" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/obs"
        android:layout_row="0"
        android:layout_column="1" />
</GridLayout>




Here is my custom adapter:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

class CustomAdObs extends ArrayAdapter<String> {



    private String [] observation;
    private Boolean [] checked;
    private Context context;

    public CustomAdObs(Context context, String[] observation) {
        super(context, R.layout.list_o, observation);
        this.observation = observation;
        this.checked = checked;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater siteInflater = LayoutInflater.from(getContext());
        View customView = siteInflater.inflate(R.layout.list_o, parent, false);

        TextView observationTV = (TextView) customView.findViewById(R.id.obs);
        CheckBox checkCB = (CheckBox) customView.findViewById(R.id.chkbx);

        checkCB.setTag(Integer.valueOf(position)); 

        observationTV.setText(observation[position]);
        checkCB.setChecked(checked[position]);

        return customView;
    }
}




Finally here is my activity:

import android.app.Activity;
import android.content.Intent;
import android.os.StrictMode;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class selobs extends Activity {

    List< List<String> > listArray = new ArrayList< List<String> >();
    List<String> array1 = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode. setThreadPolicy(policy);
        setContentView(R.layout.activity_selobs);

        final Button retrieve= (Button) findViewById(R.id.btnret);
        final EditText txtob = (EditText) findViewById(R.id.editText23);

        filladapter();

        retrieve.setonclickListener(
                new View.onclickListener() {
                    public void onclick(View view) {

                        ListView obsListView = (ListView) findViewById(R.id.obsList);
                        View v;

                        for (int i = 0; i < obsListView.getCount(); i++) {
                            v = obsListView.getAdapter().getView(i, null, null);
                            CheckBox check = (CheckBox) v.findViewById(R.id.chkbx);
                            TextView obsItem = (TextView) v.findViewById(R.id.obs);

                            if (check.isChecked()) {
                                String p = obsItem.getText().toString();
                                Toast.makeText(selobs.this, p, Toast.LENGTH_LONG).show();
                            }

                        }
                    }
                }
        );
    }


    public void filladapter(){

        myDBhandler1 dbHandler;
        dbHandler = new myDBhandler1(selobs.this, null, null, 1);
        listArray = dbHandler.databaseToStringObs();

        List array1 = listArray.get(0);

        String[] observ = (String[]) array1.toArray(new String[0]);
        Boolean[] checked = new Boolean[0];
        Arrays.fill(checked, Boolean.FALSE);

        final ListAdapter ObsAdapter = new CustomAdObs(this, observ, checked);
        final ListView obsListView = (ListView) findViewById(R.id.obsList);

        obsListView.setAdapter(ObsAdapter);

        obsListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String item = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(selobs.this, item, Toast.LENGTH_LONG).show();

                        TextView txtobs = (TextView) findViewById(R.id.editText23);

                        txtobs.setText(item);


                    }
                }
        );


    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Custom list view with checkbox

#2 frankwright1   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 07-May 12

Re: Custom list view with checkbox

Posted 06 May 2015 - 04:14 AM

posted elsewhere due to 0 views after 4 days. Will update if I have any success.
Was This Post Helpful? 0
  • +
  • -

#3 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Custom list view with checkbox

Posted 06 May 2015 - 11:20 PM

Hi,

You passed in 3 params to the Constructor here,
73    final ListAdapter ObsAdapter = new CustomAdObs(this, observ, checked);


and the Constructor accepts only 2.
17    public CustomAdObs(Context context, String[] observation) {


and in the constructor's body, you did a self-assignment,
20    this.checked = checked;


I presume that is why when you check for true there is none.

Hope it Helps!

This post has been edited by AKMafia001: 06 May 2015 - 11:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 frankwright1   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 07-May 12

Re: Custom list view with checkbox

Posted 07 May 2015 - 03:29 AM

Thanks - is it correct that I need all three? I have adjusted my constructor as follows:

public CustomAdObs(Context context, String[] observation, Boolean[] checked) {



Unfortunately I was then geting an array out of bounds exception on this line in my custom adapter:

checkCB.setChecked(checked[position]);



So I made sure the length of the boolean array was the same lengh as the string array as follows:
String[] observ = (String[]) array1.toArray(new String[0]);
Integer al = observ.length;
Boolean[] checked = new Boolean[al];



but now I am getting a null object reference exception on the same line in my custom adapter:
checkCB.setChecked(checked[position]);


This post has been edited by frankwright1: 07 May 2015 - 03:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1