Horrorgoogle
Horrorgoogle

Reputation: 7868

how to remove list item or clearing listview using shared preferences?

I have following base adapter custom class, creating listview and items. but i want to remove all items from the list when I click reset button. My code :

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

My reset button is not including on a list. I have tried this in yes button click event in above code but could not get any update. So what to do? Thanks in advance.

Upvotes: 0

Views: 1928

Answers (3)

Graeme
Graeme

Reputation: 25864

For a start, your using your adapter wrong. Your adapter should be a wrapper around your data, not a facade used to expose data contained elsewhere in your code.

In your case your using it to access the two variables (very bad form to make these static):

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

On create your filling these variables from your shared preferences.

Then on your "Yes" your updating your preferences but no matter how much you press the "update" button on your adapter, it's still looking at those variables which haven't been updated.

If you want your "Yes" button to clear your list then you need to change the data which backs the adapter then tell the adapter that it has changed and to redraw itself.

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

Upvotes: 1

Arun
Arun

Reputation: 1668

using your listview instance get the list adapter like

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

OR

you can also call notifyDataSetChanged(); to tell the list view that its data set is changed

Upvotes: 2

jeet
jeet

Reputation: 29199

To clear List:

set tempTime and tempScore to empty array

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

To Add/Remove Data :

Alter data source tempTime and tempScore accordingly and call adapter.notifyDataSetChanged();

Upvotes: 1

Related Questions