简体   繁体   中英

Android how to append custom listView using SharedPreferences

i want to update my ListView using SharedPreferences get from API response... I did something like this:

  1. First i inintial the Strings[]:
String First[] = {"aa"};
String Second[] = {"aa"};
String Third[] = {"aa"};
String Four[] = {"aa"};
Integer Five[] = {1};
  1. I want to update this using the Shared Preferences
SharedPreferences settings;
settings = getSharedPreferences("Number_List", Context.MODE_PRIVATE);

String uFirst = settings.getString("uFirst", "NONE");
String uSecond = settings.getString("uSecond", "NONE");
String uThird = settings.getString("uThird", "NONE");
String uFour = settings.getString("uFour", "NONE");
int uFive= settings.getInt("uFive", 0);

First = Arrays.copyOf(First, First.length + 1);
Second= Arrays.copyOf(Second, Second.length + 1);
Third= Arrays.copyOf(Third, Third.length + 1);
Four= Arrays.copyOf(Four, Four.length + 1);
Five= Arrays.copyOf(Five, Five.length + 1);

First[First.length -1] = uFirst;
Second[Second.length -1] = uSecond;
Third[Third.length -1] = uThird;
Four[Four.length -1] = uFour;
Five[Five.length -1] = uFive;

adapter.notifyDataSetChanged();

And this code never refreshing the listview... Im totally without any ideas how to make it more automatically.. Every time when im adding another things in list eg. First[] = {"aaa","bbbb"}; it works properly...

Can someone take the time to help?

Adapter:

class listDataAdapter extends ArrayAdapter<String> {

            Context context;
            String rFirst[];
            String rSecond[];
            String rThird[];
            String rFour[];
            Integer Five[];

            listDataAdapter(Context c, String rFirst[], String rSecond[], String rThird[], String rFour[], Integer Five[]) {
                super(c, R.layout.listview_row_orders, R.id.data_zal_text, rFirst);
                this.context = c;
                this.rFirst = rFirst;
                this.rSecond = rSecond;
                this.rThird = rThird;
                this.rFour = rFour;
                this.Five = Five;
            }

            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View row = layoutInflater.inflate(R.layout.listview_row_orders, parent, false);
                TextView myFirst = row.findViewById(R.id.data_zal_text_data);
                TextView mySecond = row.findViewById(R.id.data_rozl_text_data);
                TextView myThird = row.findViewById(R.id.place_zal_text_data);
                TextView myFour = row.findViewById(R.id.place_rozl_text_data);
                ProgressBar myOrderProgress = row.findViewById(R.id.progress_order_status);


                myFirst.setText(rFirst[position]);
                mySecond.setText(rSecond[position]);
                myThird.setText(rThird[position]);
                myFour.setText(rFour[position]);
                myOrderProgress.setProgress(Five[position]);

                return row;
            }
    ```

The problem is that you have updated the values in you activity, but in the adapter you still have the old values so notifyDataSetChanged will just work with the same values, you need to replace the old array values on the adapter with the new array values, you can create a simple helper method to do that in the adapter class for example

public void updateData(String[] firstArray) {
    this.first = firstArray;
    notifyDataSetChanged();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM