我有一个listview,它使用CheckedTextView填充数据。这是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>
在项目点击监听器上,在项目右侧显示勾选,但不仅该项目显示勾选其他多个项目也显示勾选。奇怪的是,在相同数量的行之后都是滴答声。我不知道我在哪里做错了。
这是代码
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();
}
}
});
}
}
感谢任何帮助
答案 0 :(得分:2)
majorsListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
应该是ListView.CHOICE_MODE_MULTIPLE
,因此Android会跟踪您的选择。然后,您还可以摆脱OnItemClickListener
。当用户完成选择后,getCheckedItemPositions()
会告诉您他们选择了哪些,因此您可以更新数据模型。
您当前的实现忽略了行回收的影响(一个CheckedTextView
窗口小部件将用于ArrayAdapter
中的任意数量的可能位置。