0

我有以下基本适配器自定义类,创建列表视图和项目。但是当我单击重置按钮时,我想从列表中删除所有项目。我的代码:

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

我的重置按钮不包括在列表中。我已经在上面代码中的“是”按钮单击事件中尝试过此操作,但无法获得任何更新。那么该怎么办?提前致谢。

4

3 回答 3

2

使用您的 listview 实例获取列表适配器,例如

urlist.setAdapter("pass your updated adapter with empty string array");

或者

你也可以调用 notifyDataSetChanged(); 告诉列表视图其数据集已更改

于 2012-01-09T09:05:16.550 回答
1

首先,您使用适配器错误。您的适配器应该是数据的包装器,而不是用于暴露代码中其他地方包含的数据的外观。

在您的情况下,您使用它来访问两个变量(使这些变量成为静态的非常糟糕的形式):

static String[] tempTime = new String[10];
static String[] tempScore = new String[10];

根据您的共享偏好创建填充这些变量。

然后在您的“是”上更新您的首选项,但无论您按适配器上的“更新”按钮多少次,它仍在查看那些尚未更新的变量。

如果您希望您的“是”按钮清除您的列表,那么您需要更改支持适配器的数据,然后告诉适配器它已更改并重绘自身。

   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(MY_PREFS_NAME);
            edit.commit();

            strTime = pref.getString("high_score_times", "");
            intScore = pref.getString("high_score_values", "");
            tempTime = strTime.split(",");
            tempScore = intScore.split(",");

            EfficientAdapter adapter = (EfficientAdapter)lv.getAdapter();
            adapter.notifyDataSetChanged();               
    });
于 2012-01-09T10:47:54.083 回答
1

清除列表:

将 tempTime 和 tempScore 设置为空数组

tempTime= new String[0]; 
adapter.notifyDataSetChanged();

添加/删除数据:

相应地更改数据源 tempTime 和 tempScore 并调用adapter.notifyDataSetChanged();

于 2012-01-09T08:13:07.840 回答