Java CheckTextView使用适配器更改状态
我无法从我的Java CheckTextView使用适配器更改状态,java,android,listview,simpleadapter,Java,Android,Listview,Simpleadapter,我无法从我的SimpleAdapter::getView()列表视图中更改CheckedTextView复选框的状态。我还尝试了CheckedTextView.invalidate(),但没有效果 我将列表元素的状态存储在一个单独的容器中:gather\u data 整个源代码位于 V2:(添加了notifyDataSetChanged()) V1:(原件) 用于ListView的布局: 当前代码: package org.raboss.gamification.scavengerhun
SimpleAdapter::getView()
列表视图中更改CheckedTextView
复选框的状态。我还尝试了CheckedTextView.invalidate()
,但没有效果
我将列表元素的状态存储在一个单独的容器中:gather\u data
- 整个源代码位于
- V2:(添加了
)notifyDataSetChanged()
- V1:(原件)
- V2:(添加了
- 用于ListView的布局:
package org.raboss.gamification.scavengerhunt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.util.Log;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GatherListActivity extends Activity {
protected ListView gather_list;
protected AsyncQueryHandler asyncGatherlistQueryHandler;
private SharedPreferences prefs;
protected ArrayList<HashMap<String,Object>> gather_data;
final private int INTENT_REQUESTCODE_QRSCAN = 1;
private class CheckedListAdapter extends SimpleAdapter {
public CheckedListAdapter(Context context,
List<? extends Map<String, ?>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (parent != null) {
CheckedTextView ctv = (CheckedTextView)parent.findViewById(android.R.id.text1);
if (ctv != null) {
try {
if (position < gather_data.size()) {
HashMap<String,Object> hm = gather_data.get(position);
if (ctv.isChecked() != (Boolean)hm.get("mark")) {
Log.v(this.getClass().getName(), String.format("Draw %s->%s %s", ctv.isChecked() ? "set" : "unset", (Boolean)hm.get("mark") ? "set" : "unset", hm.get("context")));
//ctv.setChecked((Boolean)hm.get("mark"));
//parent.invalidate();
ctv.toggle();
}
ctv.setTextColor(ctv.isChecked() ? Color.GREEN : Color.BLACK);
}
}
catch (Exception ex) {
Log.v(this.getClass().getName(), ex.toString());
}
}
}
return v;
}
}
protected CheckedListAdapter gatherlist_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gatherlist);
Log.v(this.getClass().getName(), "onCreate()");
internalRestoreInstanceState(savedInstanceState);
String[] from = new String[] {"title", "context"};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
try {
gather_list = (ListView)findViewById(R.id.gather_list);
gatherlist_adapter = new CheckedListAdapter(gather_list.getContext(), this.gather_data, R.layout.gather_list_item, from, to);
ListView lv = (ListView)this.findViewById(R.id.gather_list);
lv.setAdapter(gatherlist_adapter);
}
catch (IllegalArgumentException ex) {
Log.e(this.getClass().getName(), ex.toString());
throw(ex);
}
Button qrscanner_button = (Button)findViewById(R.id.qrscanner_button);
qrscanner_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v(this.getClass().getName(), "onClick()");
Intent intent = new Intent("org.raboss.gamification.scavengerhunt.SCAN");
intent.putExtra("EMBEDDED_INTENT", true);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, INTENT_REQUESTCODE_QRSCAN);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case INTENT_REQUESTCODE_QRSCAN:
onActivityResultQRScan(resultCode, data);
break;
}
}
private void onActivityResultQRScan(int resultCode, Intent data) {
// Handle successful scan
String contents = data.getStringExtra("SCAN_RESULT");
Log.i(this.getClass().getName(), "Scanned QR-Code: "+contents);
try {
Uri link = Uri.parse(contents);
String link_where = link.getQueryParameter("where");
Log.i(this.getClass().getName(), String.format("Stand: %s", link_where));
for(HashMap<String,Object> hm : this.gather_data) {
String context = (String)hm.get("context");
if (context.contains(link_where)) {
hm.put("mark", Boolean.TRUE);
}
else if (link_where.contains("restart")) {
hm.put("mark", Boolean.FALSE);
}
Log.i(this.getClass().getName(), String.format("%s ~ %s: %s", context, link_where, (Boolean)hm.get("mark") ? "set" : "unset"));
}
if (this.gatherlist_adapter != null) {
this.gatherlist_adapter.notifyDataSetChanged();
}
}
catch (Exception ex) {
Log.i(this.getClass().getName(), ex.toString());
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.v(this.getClass().getName(), "onSaveInstanceState()");
this.internalSaveInstanceState(outState);
}
protected void internalSaveInstanceState(Bundle outState) {
Log.v(this.getClass().getName(), "internalSaveInstanceState()");
prefs = getSharedPreferences(this.getClass().getName(), MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
try {
for(HashMap<String,Object> hm : this.gather_data) {
editor.putBoolean((String)hm.get("context"), (Boolean)hm.get("mark"));
}
}
catch (Exception ex) {
Log.v(this.getClass().getName(), ex.toString());
}
editor.commit();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v(this.getClass().getName(), "onRestoreInstanceState()");
this.internalRestoreInstanceState(savedInstanceState);
}
protected void internalRestoreInstanceState(Bundle savedInstanceState) {
Log.v(this.getClass().getName(), "internalRestoreInstanceState()");
if (this.gather_data != null) {
Log.v(this.getClass().getName(), String.format("this.gather_data already initialised: %d", gather_data.size()));
}
else {
Resources res = getResources();
String[] cebit_stands = res.getStringArray(R.array.cebit_stands);
String[] go_slogen = res.getStringArray(R.array.go_slogan);
this.gather_data = new ArrayList<HashMap<String,Object>>();
for (int i = 0; i < cebit_stands.length; i++) {
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("context", cebit_stands[i]);
hm.put("title", go_slogen[i % go_slogen.length]);
this.gather_data.add(hm);
}
}
prefs = getSharedPreferences(this.getClass().getName(), MODE_PRIVATE);
try {
for(HashMap<String,Object> hm : this.gather_data) {
hm.put("mark", prefs.getBoolean((String)hm.get("context"), Boolean.FALSE));
Log.v(this.getClass().getName(), String.format("Restore %s = %s", (String)hm.get("context"), (Boolean)hm.get("mark") ? "set" : "unset"));
}
}
catch (Exception ex) {
Log.v(this.getClass().getName(), ex.toString());
}
}
@Override
public void finish() {
Log.v(this.getClass().getName(), "finish()");
}
}
package org.raboss.gamification.cleavererhunt;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入android.net.Uri;
导入android.os.Bundle;
导入android.provider.Contacts contract.Contacts;
导入android.widget.Button;
导入android.widget.CheckedTextView;
导入android.widget.simpledapter;
导入android.widget.SimpleCursorAdapter;
导入android.app.Activity;
导入android.content.AsyncQueryHandler;
导入android.content.ContentResolver;
导入android.content.ContentValues;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.content.res.Resources;
导入android.database.Cursor;
导入android.graphics.Color;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.ListView;
导入android.util.Log;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类GatherListActivity扩展了活动{
受保护的ListView集合列表;
受保护的AsyncQueryHandler asyncGatherlistQueryHandler;
私人共享参考优先权;
受保护的ArrayList收集数据;
最终专用int int int int int int int int int\u REQUESTCODE\u QRSCAN=1;
私有类CheckedListAdapter扩展了SimpleAdapter{
公共CheckedListAdapter(上下文,
列表>数据,int资源,
字符串[]从,整数[]到){
超级(上下文、数据、资源、从、到);
//TODO自动生成的构造函数存根
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=super.getView(位置、转换视图、父级);
如果(父项!=null){
CheckedTextView ctv=(CheckedTextView)parent.findViewById(android.R.id.text1);
如果(ctv!=null){
试一试{
if(位置%s%s”,ctv.isChecked()?“set”:“unset”,(布尔)hm.get(“mark”)?“set”:“unset”,hm.get(“context”);
//ctv.setChecked((布尔)hm.get(“标记”);
//parent.invalidate();
ctv.toggle();
}
ctv.setTextColor(ctv.isChecked()?Color.GREEN:Color.BLACK);
}
}
捕获(例外情况除外){
Log.v(this.getClass().getName(),例如toString());
}
}
}
返回v;
}
}
受保护的CheckedListAdapter gatherlist_适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u gatherlist);
Log.v(this.getClass().getName(),“onCreate()”);
internalRestoreInstanceState(savedInstanceState);
String[]from=新字符串[]{“title”,“context”};
int[]to=newint[]{android.R.id.text1,android.R.id.text2};
试一试{
聚集列表=(ListView)findViewById(R.id.gather\u列表);
gatherlist\u adapter=新的CheckedListAdapter(gather\u list.getContext(),this.gather\u data,R.layout.gather\u list\u项,from,to);
ListView lv=(ListView)this.findViewById(R.id.gather\u list);
lv.setAdapter(gatherlist_适配器);
}
捕获(IllegalArgumentException ex){
Log.e(this.getClass().getName(),例如toString());
投掷(ex);
}
按钮qrscanner\u按钮=(按钮)findViewById(R.id.qrscanner\u按钮);
qrchanner_button.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Log.v(this.getClass().getName(),“onClick()”);
Intent Intent=新的Intent(“org.raboss.gamification.cleavererhunt.SCAN”);
intent.putExtra(“嵌入式意图”,真);
意向。额外(“扫描模式”、“二维码模式”);
startActivityForResult(意图、意图请求代码\u QRSCAN);
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode!=结果\u确定)
返回;
开关(请求代码){
案例意图\u请求代码\u QRSCAN:
onActivityResultQRScan(结果代码、数据);
打破
}
}
activityresultqrscan上的私有void(int-resultCode,Intent数据){
//处理成功的扫描
字符串内容=data.getStringExtra(“扫描结果”);
Log.i(this.getClass().getName(),“扫描二维码:”+内容);
试一试{
urilink=Uri.parse(内容);
字符串link_where=link.getQueryParameter(“where”);
Log.i(this.getClass().getName(),String.format(“Stand:%s”,link_-where));
for(HashMap hm:this.collect_数据){
字符串上下文=(字符串)hm.get(“上下文”);
if(context.contains(link_where)){
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView ctv = null;
if (convertView != null) {
ctv = (CheckedTextView)convertView.findViewById(android.R.id.text1);
}
if (ctv != null) {