Android 如何使用共享首选项删除列表项或清除listview?
我有以下基本适配器自定义类,创建listview和项。但我想在单击“重置”按钮时从列表中删除所有项目。 我的代码:Android 如何使用共享首选项删除列表项或清除listview?,android,listview,baseadapter,Android,Listview,Baseadapter,我有以下基本适配器自定义类,创建listview和项。但我想在单击“重置”按钮时从列表中删除所有项目。 我的代码: public class Scores extends Activity implements OnClickListener { public static final String MY_PREFS_NAME = "PrefName"; SharedPreferences pref; static String[] tempTime = new String[10]; sta
public class Scores extends Activity implements OnClickListener {
public static final String MY_PREFS_NAME = "PrefName";
SharedPreferences pref;
static String[] tempTime = new String[10];
static String[] tempScore = new String[10];
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return tempTime.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.mathmatch_score_format, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.time_text);
holder.text2 = (TextView) convertView
.findViewById(R.id.score_text);
/*final ImageView deleteButton = (ImageView)
convertView.findViewById(R.id.score_reset);
deleteButton.setOnClickListener(this);*/
convertView.setTag(holder);
//deleteButton.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(tempTime[position]);
holder.text2.setText(tempScore[position]);
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mathmatch_score);
setUpViews();
pref = getSharedPreferences(MY_PREFS_NAME, 0);
strTime = pref.getString("high_score_times", "");
intScore = pref.getString("high_score_values", "");
tempTime = strTime.split(",");
tempScore = intScore.split(",");
Comparator<String> comparator = new CustomArrayComparator<String, String>(tempScore, tempTime);
Arrays.sort(tempTime, comparator);
Arrays.sort(tempScore, Collections.reverseOrder());
lv.setAdapter(new EfficientAdapter(this));
}
private void setUpViews() {
lv = (ListView) findViewById(R.id.list);
reset = (ImageView) findViewById(R.id.score_reset);
reset.setOnClickListener(this);
}
@Override
protected void onPause() {
super.onPause();
pref = getSharedPreferences(MY_PREFS_NAME, 0);
SharedPreferences.Editor edit = pref.edit();
edit.putString("high_score_times", strTime);
edit.putString("high_score_values", intScore);
edit.commit();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.score_reset:
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setTitle("Reset");
alertbox.setMessage("Are you sure all time ans score are reset?");
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pref = getSharedPreferences(MY_PREFS_NAME, 0);
SharedPreferences.Editor edit = pref.edit();
/*edit.remove("high_score_times");
edit.remove("high_score_values");*/
/*edit.remove(intScore);
edit.remove(strTime);
*/
//edit.clear();
edit.remove(MY_PREFS_NAME);
edit.commit();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
}
});
alertbox.show();
break;
default:
break;
}}}
public类分数扩展活动实现OnClickListener{
公共静态最终字符串MY\u PREFS\u NAME=“PrefName”;
共享参考优先;
静态字符串[]试探输入法=新字符串[10];
静态字符串[]tempScore=新字符串[10];
私有静态类EfficientAdapter扩展BaseAdapter{
私人停车场;
公共效率适配器(上下文){
mInflater=LayoutInflater.from(上下文);
}
public int getCount(){
返回testime.length;
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.充气(
R.layout.mathmatch_score_格式,空);
holder=新的ViewHolder();
holder.text1=(TextView)convertView
.findviewbyd(R.id.time\u text);
holder.text2=(TextView)convertView
.findviewbyd(R.id.score_text);
/*最终图像视图删除按钮=(图像视图)
convertView.findViewById(R.id.score\U重置);
deleteButton.setOnClickListener(此)*/
convertView.setTag(支架);
//deleteButton.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.text1.setText(试探时间[位置]);
holder.text2.setText(tempScore[位置]);
返回视图;
}
静态类视窗夹{
TextView text1;
TextView text2;
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mathmatch_分数);
setupview();
pref=getSharedReferences(我的首选项名称,0);
strTime=pref.getString(“高分次数”);
intScore=pref.getString(“高分数值”);
TENTIME=strTime.split(“,”);
tempScore=intScore.split(“,”);
Comparator Comparator=新的CustomArrayComparator(tempScore,testime);
数组.sort(testime、comparator);
sort(tempScore、Collections.reverseOrder());
lv.setAdapter(新高效适配器(本));
}
私有void setUpViews(){
lv=(ListView)findViewById(R.id.list);
重置=(ImageView)findViewById(R.id.score\u重置);
reset.setOnClickListener(此);
}
@凌驾
受保护的void onPause(){
super.onPause();
pref=getSharedReferences(我的首选项名称,0);
SharedReferences.Editor edit=pref.edit();
edit.putString(“高分次数”,strTime);
edit.putString(“高分数值”,intScore);
edit.commit();
}
@凌驾
受保护的void onStop(){
super.onStop();
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.分数重置:
AlertDialog.Builder alertbox=新建AlertDialog.Builder(此);
alertbox.setTitle(“重置”);
setMessage(“您确定所有ans分数都重置了吗?”);
setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
公共void onClick(对话框接口arg0,int arg1){
pref=getSharedReferences(我的首选项名称,0);
SharedReferences.Editor edit=pref.edit();
/*编辑。删除(“高分次数”);
编辑。删除(“高分值”)*/
/*编辑。删除(intScore);
编辑。删除(strTime);
*/
//edit.clear();
编辑。删除(我的首选项名称);
edit.commit();
}
});
alertbox.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
公共void onClick(对话框接口arg0,int arg1){
Toast.makeText(getApplicationContext(),“单击了“否”按钮”,Toast.LENGTH\u SHORT.show();
}
});
show();
打破
违约:
打破
}}}
我的重置按钮未包含在列表中。
我在上面代码中的“是”按钮点击事件中尝试过这个,但无法得到任何更新。那怎么办呢?
提前感谢。要清除列表:
将TENTIME和tempScore设置为空数组
tempTime= new String[0];
adapter.notifyDataSetChanged();
要添加/删除数据,请执行以下操作:
相应地更改数据源TENTIME和tempScore,并调用adapter.notifyDataSetChanged()代码>清除列表:
将TENTIME和tempScore设置为空数组
tempTime= new String[0];
adapter.notifyDataSetChanged();
要添加/删除数据,请执行以下操作:
相应地更改数据源TENTIME和tempScore,并调用adapter.notifyDataSetChanged()代码>使用listview实例获取列表适配器,如
urlist.setAdapter("pass your updated adapter with empty string array");
或
您还可以调用notifyDataSetChanged();要使用listview实例告知列表视图其数据集已更改,请获取列表适配器,如
urlist.setAdapter("pass your updated adapter with empty string array");
或
您还可以调用notifyDataSetChanged();首先,要告诉列表视图其数据集已更改,您的适配器使用错误。适配器应该是数据的包装器,而不是用于公开代码中其他地方包含的数据的外观
在您的情况下,您可以使用它访问两个变量(使这些变量成为静态变量的非常糟糕的形式):
在创建时,根据共享首选项填充这些变量
然后在“是”的时候更新您的首选项,但无论您在适配器上按了多少次“更新”按钮,它仍然会查看那些尚未更新的变量
如果希望“是”按钮清除列表,则需要更改