SLaks 2013-08-16 07:43 采纳率: 0%
浏览 3049
已采纳

使用shared preferences如何删除list item?

我有下面的adapter自定义类,创建了listview和items,但是我想在点击reset按钮时,从list中删除所有的items。

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;
}}}

Reset按钮不在list中。上面的代码是点击yes按钮事件,但是没有任何更新。如何改正这个问题呢?

  • 写回答

1条回答

  • longhai326 2013-08-19 07:10
    关注

    EfficientAdapter adapter;//设为全局变量
    当list数据改变后,需要调用adapter.notifyDataSetChanged();
    这样显示才会改变.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)