tags:

views:

32

answers:

1

I have a listview which populates the data using CheckedTextView. Here is the xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="14dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingRight="5dp"
    android:paddingLeft="5dp"
    android:textColor="@color/blue"
    android:checked="false"
    android:id="@+id/listviewsubview_textview" >
</CheckedTextView>

On item click listener, show tick on the right of the item, but not only that item shows tick other multiple items also show tick. Wierd thing is all are ticks after same number of row. I dont know where I am doing wrong.

Here is the code

package com.chegg.android.account;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.chegg.android.R;
import com.chegg.android.api.RestClient;
import com.chegg.android.objects.StudentInfo;
import com.chegg.android.util.Util;

public class MajorActivity extends MainActivity {

    public ListView majorsListView;
    public String url;
    public HashMap<String, String> majorMap = new HashMap<String, String>();
    public JSONArray resultArray = new JSONArray();
    public JSONObject resultObj = new JSONObject();
    public ArrayList<String> majorNameArray = new ArrayList<String>();
    public RestClient restClient = null;
    public StudentInfo studentInfo = null;
    public HashMap majors = null; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.majors);

        Object campusObj =  this.getIntent().getStringExtra("campus_id");
        restClient = new RestClient();
        if(campusObj!=null) {
            String campusId = campusObj.toString();
            url = "school/majors/"+campusId;
        } else {
            url = "school/majors";
        }
        String result = restClient.connect(url);
        try {
            JSONArray jsonArray = new JSONArray(result);
            for(int i=0;i<jsonArray.length();i++) {
                JSONObject jsonObj = jsonArray.getJSONObject(i);
                String name = jsonObj.getString("name");
                majorMap.put(name,jsonObj.getString("id"));
                majorNameArray.add(name);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        majorsListView = (ListView)findViewById(R.id.majors_listview);
        majorsListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
        majorsListView.setAdapter(new ArrayAdapter<String>(MajorActivity.this, R.layout.list_view_subview,majorNameArray));


        studentInfo = (StudentInfo)this.getIntent().getSerializableExtra("studentInfo");
        if(studentInfo.getMajors()!=null) {
            majors = (HashMap)studentInfo.getMajors();
        } else {
            majors = new HashMap();
        }
        //majorsListView.setAdapter(new MajorsAdapter(MajorActivity.this, R.layout.list_view_subview,majorNameArray,majors));
        majorsListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                CheckedTextView selectedView = (CheckedTextView)view;
                Drawable chevron = getResources().getDrawable(R.drawable.chevron);
                chevron.setBounds( 0, 0, chevron.getMinimumWidth(), chevron.getMinimumHeight() );

                String text = selectedView.getText().toString();


                if(!selectedView.isChecked()) {
                    selectedView.setChecked(true);
                    selectedView.setCheckMarkDrawable(R.drawable.chevron);
                    majors.put(text, majorMap.get(text));
                } else {
                    selectedView.setChecked(false);
                    selectedView.setCheckMarkDrawable(null);
                    if(majors.containsKey(text)) {
                        majors.remove(text);
                    }
                }
                JSONObject jsonFinalResult = new JSONObject();
                try {
                     Iterator it = majors.entrySet().iterator();
                        while (it.hasNext()) {
                            Map.Entry pairs = (Map.Entry)it.next();
                            JSONObject jsonObj = new JSONObject();
                            System.out.println(pairs.getKey() + " = " + pairs.getValue());
                            jsonObj.put("name", pairs.getKey());
                            jsonObj.put("id",pairs.getValue());
                            resultArray.put(jsonObj);
                        }
                    jsonFinalResult.accumulate("majors", resultArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                String url = "account?access_token="+ accessToken;
                Log.i("json *****",jsonFinalResult.toString());
                String result = restClient.connectHttpsPostPutJson(url, jsonFinalResult.toString(), "PUT");

                Log.i("*******",result);

                String checkError = Util.checkResponseForError(result);
                if(checkError.equals("") || checkError == null) {
                    studentInfo = StudentInfo.getObjectFromJson(result);
                    cacheData(studentInfo, "studentInfo", "update");
                } else {
                    alertbox.setMessage(checkError);
                    alertbox.setNeutralButton("ok", null);
                    alertbox.show();
                }

            }
        }); 



    } 


}

Any help is appreciated

A: 

majorsListView.setChoiceMode(ListView.CHOICE_MODE_NONE);

That should be ListView.CHOICE_MODE_MULTIPLE, so Android will track your selections for you. Then, you can also get rid of your OnItemClickListener. When the user is done making selections, getCheckedItemPositions() will tell you which ones they chose, so you can update your data model.

Your current implementation ignores the effects of row recycling (one CheckedTextView widget will be used for any number of possible positions in your ArrayAdapter).

CommonsWare
Thanks buddy, will try this out.
rachana
How can i get rid of OnItemClickListener
rachana
I want show the tick icon on the right when the row is selected + want to send selected information to the server at every click. I dont have luxury of have "save information" button :(
rachana
@rachana: "want to send selected information to the server at every click" -- I do not recommend this.
CommonsWare
I know, that what I talked abt with my manager but he wants it in this way. But I observed one this is that getting a content and sending to server is working good. Only problem is when I want to show my custome tick to show that row is selected using android:checkMark
rachana