Sean
Sean

Reputation: 1123

checked text view doesn't check

I created a custom list view that displays a name and has a check next to it. I decided not to go with using a checkbox but instead use checked text view. The problem is when I click on the row, the check does not light up. I was wondering how I could fix this problem. This is the layout for my list view, each element will have a checked text view with differing text. I also need a way to scroll through the list and whichever element is unchecked should have its text greyed out. Thanks

<?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">

    <CheckedTextView android:text="CheckedTextView"
        android:textColor="#000000" android:id="@+id/checkedTextView1" 
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:checkMark="?android:attr/textCheckMark" android:paddingLeft="6dip"
        android:paddingRight="6dip"></CheckedTextView>
</LinearLayout>

EDIT Here is the code that my list view goes through

 static final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

private void refreshList(String id, ListView lv) {
    // removes the list and rebuilds it will choose different response
    // string to get the refreshed times.

    try {
        HttpClient client1 = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://iphone-radar.com/accounts/"+id);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String response = client1.execute(request, responseHandler);



        list.removeAll(list);
        SpecialAdapter adapter = new SpecialAdapter(this, list,
                R.layout.trackingme_row_layout, new String[] { "name" },
                new int[] { R.id.checkedTextView1 });

        org.json.JSONObject obj = new org.json.JSONObject(response);
        JSONArray tracking_users = obj.getJSONArray("d");

        for (int i = 0; i < tracking_users.length(); i++) {

            HashMap<String, String> temp = new HashMap<String, String>();
            JSONObject user = tracking_users.getJSONObject(i);
            temp.put("name", user.getString("full_name"));
            // upload location time

            list.add(temp);

        }
        lv.setAdapter(adapter);

    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

Here is the special adapter that it goes through so that every other row is either white or grey.

 public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[]{R.drawable.row_background_grey,       R.drawable.row_background_white};

public SpecialAdapter(Context context,
        ArrayList<HashMap<String, String>> list, int resource,
        String[] from, int[] to) {
    super(context, list, resource, from, to);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    int colorPos = position % colors.length;
    view.setBackgroundResource(colors[colorPos]);
    return view;
}

}

Upvotes: 0

Views: 3631

Answers (2)

rochdev
rochdev

Reputation: 3855

From what I got you want to have multiple CheckedTextView's in your list.

Below you find one approach to solve this problem. As the ListView is reusing views we need a way to keep track of the different CheckedTextView's, hence the use of HashMaps. An alternative way would be to just keep track of the states and with an OnItemClickListener change the state and then update the entire list.

public class CheckedTextViewExample extends ListActivity implements OnClickListener {   

    // store CheckTextView's
    private HashMap<Integer, CheckedTextView> mCheckedList = 
            new HashMap<Integer, CheckedTextView>();
    // store state
    private HashMap<Integer, Boolean> mIsChecked = 
            new HashMap<Integer, Boolean>();
    // the list objects
    private ArrayList<MyObject> mList;

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

        mList = new ArrayList<MyObject>();

        MyObject one = new MyObject();
        one.setName("One");
        ...
        MyObject thirteen = new MyObject();
        thirteen.setName("thirteen");
        mList.add(one);
        ...
        mList.add(thirteen);        

        setListAdapter(new MyAdapter(this, R.layout.row, mList));        
    }

    @Override
    public void onClick(View v) {
        // get the CheckedTextView
        CheckedTextView ct = mCheckedList.get(v.getTag());
        if (ct != null) {
            // change the state and colors
            ct.toggle();
            if (ct.isChecked()) {
                ct.setTextColor(Color.WHITE);
            } else {
                ct.setTextColor(Color.GRAY);
            }
            // add current state to map
            mIsChecked.put((Integer) v.getTag(), ct.isChecked());
        }       
    }

    private class MyAdapter extends ArrayAdapter<MyObject> {

        private ArrayList<MyObject> items;

        public MyAdapter(Context context, int textViewResourceId, ArrayList<MyObject> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        public View getView(final int position, View view, ViewGroup parent) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.row, null);

            CheckedTextView ct = (CheckedTextView) view.findViewById(R.id.checkedTextView);
            ct.setText(items.get(position).getName());
            if (mIsChecked.get(position) != null) {
                if (mIsChecked.get(position)) {
                    ct.setChecked(true);
                    ct.setTextColor(Color.WHITE);
                } else {
                    ct.setTextColor(Color.GRAY);
                }
            }
            // tag it
            ct.setTag(position);
            mCheckedList.put(position, ct);
            ct.setOnClickListener(CheckedTextViewExample.this);

            return view;
        }
    }

    private class MyObject {
        private String name;    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
}

I posted the eclipse project on my blog

Upvotes: 2

Sudar Nimalan
Sudar Nimalan

Reputation: 3992

performClick method is not override for CheckedTextView as done for CompoundButton. So following code would do:


        final CheckedTextView checkedTextView = (CheckedTextView)findViewById(R.id.checkedTextView1);
        checkedTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                checkedTextView.toggle();
            }
        });

Upvotes: 2

Related Questions