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

リセットボタンがリストに含まれていません。上記のコードのyesボタンクリックイベントでこれを試しましたが、更新を取得できませんでした。じゃあ何をすればいいの?前もって感謝します。

4

3 に答える 3

2

listviewインスタンスを使用して、次のようなリストアダプタを取得します

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

また

notifyDataSetChanged();を呼び出すこともできます。データセットが変更されたことをリストビューに通知します

于 2012-01-09T09:05:16.550 に答える
1

リストをクリアするには:

tempTimeとtempScoreを空の配列に設定します

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

データを追加/削除するには:

それに応じてデータソースのtempTimeとtempScoreを変更し、adapter.notifyDataSetChanged();

于 2012-01-09T08:13:07.840 に答える
1

まず、アダプターの使い方を間違えました。アダプターは、コードの他の場所に含まれるデータを公開するために使用されるファサードではなく、データのラッパーである必要があります。

あなたの場合、それを使用して2つの変数にアクセスします(これらを静的にするための非常に悪い形式):

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 に答える