Thursday, February 21, 2019

Delete row Listview (2)

We want to delete a row of ListView and when user reopen activity the row deleted not appear again.
In folder layout, create a file name li3.xml, put a checked textView in it.
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="46dp"
    android:checkMark="@android:drawable/btn_radio"
    android:gravity="center_vertical"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#000080"
    android:textSize="17sp" />

Declare an arraylist string.
ArrayList<String> fru = new ArrayList<String>();
fru.add("Pear");
          fru.add("Banana");
          fru.add("Cashew");
          fru.add("Orange");
          fru.add("Water melon");
          fru.add("Peach");
          fru.add("Grape");
          fru.add("Mango");
          fru.add("Plum");

First we read file list.txt to an array.
Declare an ArrayList name check.
ArrayList<String> check = new ArrayList<String>();
Read file list.txt.
String line = "";
          try {
FileInputStream In = openFileInput("list.txt");
InputStreamReader inputReader = new InputStreamReader(In);
BufferedReader BR = new BufferedReader(inputReader);
          while ((line = BR.readLine()) != null) {
          check.add(line);
          }
          BR.close();
          } catch (IOException e) {
               e.printStackTrace();
          }
Check size of array, if size =0 mean there 's no file list.txt, we write data from array fru to file txt.
if(check.size()==0){
         try {
FileOutputStream fos = openFileOutput(
                          "list.txt", Context.MODE_APPEND);
OutputStreamWriter osw = new OutputStreamWriter(
                          fos);
          for (int i = 0; i < fru.length; i++) {
          osw.append(fru[i] + "\n");
               }
               osw.flush();
               osw.close();
          } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          }
Import libraries.
Now declare data array fru2 use for ListView.
Command to read file list.txt:
String line = "";
try {
FileInputStream In = openFileInput("list.txt");
InputStreamReader inputReader = new InputStreamReader(In);
BufferedReader BR = new BufferedReader(inputReader);
          while ((line = BR.readLine()) != null) {
          fru2.add(line);
          }
          BR.close();
          } catch (IOException e) {
          e.printStackTrace();
          }   
Create file li3.xml in folder layout, trong đó có má»™t checked textView.
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="46dp"
    android:checkMark="@android:drawable/btn_radio"
    android:gravity="center_vertical"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#000080"
    android:textSize="17sp" />

Set adapter for ListView .
adapter=new ArrayAdapter<String>(this,R.layout.li3,R.id.text, fru2);
In delete button, we delete and rewrite file list.txt.
b1.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
     if (count == 0) {
Toast.makeText(forth.this, "Please choose a row!",
                                    Toast.LENGTH_SHORT).show();
} else {
SparseBooleanArray checked = lv.getCheckedItemPositions();
          for (int i = 0; i < lv.getCount(); i++) {
               if (checked.get(i) == true) {
                    fu2.remove(i);
                     }
               adapter.notifyDataSetChanged();
          }
          lv.clearChoices();
          deleteFile("list.txt");
     try {
FileOutputStream fos = openFileOutput("list.txt",
                                         Context.MODE_APPEND);
OutputStreamWriter osw = new OutputStreamWriter(fos);
     for (int i = 0; i < fru2.size(); i++) {
          osw.append(fru2.get(i) + "\n");
          }
          osw.flush();
          osw.close();
          } catch (FileNotFoundException e) {
               // TODO Auto-generated catch block
          e.printStackTrace();
     } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }
     count = 0;
Toast.makeText(forth.this, "Data deleted!",
                                    Toast.LENGTH_SHORT).show();
     }
}
});
Run to see result.



No comments:

Post a Comment