How to store an ArrayList<Uri> of in Shared Preferences in Java

53 views Asked by At

I got a listview with custom items containing an image and three text views.

I put the images in an ArrayList<Uri> of Uri.

The other text views are put in an ArrayList<String> of String.

But when I save the items with shared preferences, I save the String ArrayLists directly with the TinyDB library; the ArrayList<Uri> is not supported!

I need help saving the images as ArrayList<Uri>.

I've tried a lot of things, but it didn't work!

This is the code of the Home Page:

package com.wainan.moh;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.View;
import android.content.Intent;
import java.util.ArrayList;
import android.net.Uri;
import com.wainan.moh.TinyDB;
import android.content.SharedPreferences;
import android.widget.Toast;
import android.widget.ImageView;
import android.util.JsonWriter;
import java.util.Set;
import java.util.HashSet;

public class HomePage extends Activity {
    
    TextView tv, tv2;
    ListView lv;
    ImageButton bt;
    
    ArrayList<Uri> imgs = new ArrayList<Uri>();
    
    ArrayList<String> titles = new ArrayList<String>();
    ArrayList<String> years = new ArrayList<String>();
    ArrayList<String> descs = new ArrayList<String>();
    
    int itemAdded = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_home);
        
        getActionBar().hide();
        
        tv = findViewById(R.id.tvLvNull);
        
        tv2 = findViewById(R.id.tvSubHint);
        lv = findViewById(R.id.mainListView);
        bt = findViewById(R.id.btShowAddPage);
        
        itemAdded = getIntent().getIntExtra("itemChecked", 0);
        
        if (itemAdded == 1)
        {
            imgs.add(getIntent().getData());
            titles.add(getIntent().getStringExtra("Title"));
            years.add(getIntent().getStringExtra("Year"));
            descs.add(getIntent().getStringExtra("Desc"));
        }
        
        if (titles.isEmpty())
        {
            tv.setText("Nothing To Show");
            tv2.setText("Hit Add Button To Add");
        }
        else
        {
            tv.setText("");
            tv2.setText("");
        }
        
        TinyDB db = new TinyDB(this);
        
        db.putListString("Titles", titles);
        db.putListString("Years", years);
        db.putListString("Descs", descs);
        
        MainListAdapter ad = new MainListAdapter(this, imgs, titles, years, descs);
        lv.setAdapter(ad);
        
        bt.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    
                   startActivity(new Intent(HomePage.this, AddActivity.class));
                    
                }
            });
        
    }
    
}

0

There are 0 answers