程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-使用shared preferences如何刪除list item?

listview-使用shared preferences如何刪除list item?

編輯:編程綜合問答
使用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按鈕事件,但是沒有任何更新。如何改正這個問題呢?

最佳回答:


EfficientAdapter adapter;//設為全局變量
當list數據改變後,需要調用adapter.notifyDataSetChanged();
這樣顯示才會改變.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved