1

I want to remove an item from ListView, but I don't know how can I do it.

This is my code...

public void FLYNOTE_refresh() {
    int i;
    SharedPreferences Pref = getSharedPreferences("nnote", MODE_WORLD_READABLE); //созаётся файл настроек "nnote" и туда барыжиться вся хня
    key = Pref.getInt("number", 0); 
    key++;
    String lv_arr[] = new String[key];

    for (i = 0; i < key; i++) {
         lv_arr[i] = Pref.getString("n" + i, "none");           
    }

    textView1.setText(String.valueOf(key));

    listView1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
    listView1.setTextFilterEnabled(true);
}

I want to remove an item with 0 id,guide me to do that.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
user1468102
  • 391
  • 5
  • 16

2 Answers2

2

You can delete an item from ListView as:

 ArrayAdapter<String> adapterlist = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, lv_arr);
     listView1.setAdapter(adapter1);
     adapter1.notifyDataSetChanged();
     listView1.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    adapter1.remove(position);
    adapter1.notifyDataSetChanged();
    });

and you can see these posts for removing an item from ListView:

Remove ListView items in Android

Android: how to remove an item from a listView and arrayAdapter

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    adapter1.remove() is "String object" – user1468102 Jun 21 '12 at 07:59
  • remove() take an integer not string – ρяσѕρєя K Jun 21 '12 at 08:01
  • I must agree with user1468102, [ArrayAdapter.remove(T object)](http://developer.android.com/reference/android/widget/ArrayAdapter.html#remove%28T%29) means remove() only takes an object of the ArrayAdapter's referenced class, also ArrayAdapter.remove() is essentially a shortcut for `lv_arr.remove()` which doesn't exist for `String[]`. – Sam Jun 23 '12 at 18:20
2

If you do not want to display item 0, simply adjust the size of lv_arr and change your for-loop:

public void FLYNOTE_refresh() {
    int i;
    SharedPreferences Pref = getSharedPreferences("nnote", MODE_WORLD_READABLE); //созаётся файл настроек "nnote" и туда барыжиться вся хня
    key = Pref.getInt("number", 0); 
    String lv_arr[] = new String[key]; // One item smaller without "n0" 

    key++;
    for (i = 1; i < key; i++) {
        // Skip "n0" like this
        lv_arr[i - 1] = Pref.getString("n" + i, "none");           
    }

    textView1.setText(String.valueOf(key));

    listView1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv_arr));
    listView1.setTextFilterEnabled(true);
}

If you want to remove items while the app is running I suggest using a List<String> rather than a String[]:

public void FLYNOTE_refresh() 
{
    int i;
    SharedPreferences Pref = getSharedPreferences("nnote", MODE_WORLD_READABLE); //созаётся файл настроек "nnote" и туда барыжиться вся хня
    key = Pref.getInt("number", 0); 
    key++;
    lv_arr = new ArrayList<String>(); 

    for (i = 1; i < key; i++) {
        lv_arr.add(Pref.getString("n" + i, "none"));           
    }

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv_arr);
    listView1.setAdapter(adapter);

    listView1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            lv_arr.remove(position);
            adapter.notifyDataSetChanged();
        }
    });
}
Sam
  • 86,580
  • 20
  • 181
  • 179