2012-01-09 48 views
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按鈕單擊事件,但無法獲得任何更新。那麼該怎麼辦? 在此先感謝。

回答

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();    
    }); 
+0

如何清除列表視圖?但我把這個代碼>>>> tempTime = new String [0]; \t \t \t \t \t \t \t tempScore = new String [0]; \t \t \t \t \t \t \t \t \t \t \t \t \t \t LV。setAdapter(NULL); \t \t \t \t \t \t \t edit.commit();這是工作。謝謝你的親切信息。 – 2012-01-09 11:20:44

1

要清除列表:

集tempTime和tempScore到空數組

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

要添加/刪除數據:相應

更改數據源tempTime和tempScore和呼叫adapter.notifyDataSetChanged();

+0

我沒有使用arrayadapter來顯示listitem。我的問題是如何使用baseadapter自定義類清除listview,仔細查看我的代碼,然後回覆我。該怎麼辦? – 2012-01-09 09:31:55

2

使用你的列表視圖實例獲取列表適配器,如

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

OR

您也可以撥打notifyDataSetChanged();告訴列表視圖,其數據集已更改