luwionline
luwionline

Reputation: 186

Items from ListView to SQLite, SQLite to ListView

I'm trying to get all the items on my listview (I've attached some image uri) and put them in the database, then get them again and load them on the listview.

What I did is: String hex = array_list.toString(); and put that hex string in the database (sqlite).

FirstFragment:

ListView lv;    
ArrayList<Uri> array_list = new ArrayList<Uri>();
ArrayAdapter<Uri> array_adapter;

OnCreate..

array_adapter = new ArrayAdapter<Uri>(getActivity(), android.R.layout.simple_list_item_1, array_list);
lv = (ListView) v.findViewById(R.id.list);
lv.setAdapter(array_adapter);


OnClick..

String hex = array_list.toString();

HashMap<String, String> rcfData = new HashMap<String, String>();

rcfData.put("dateTime", hex);

DataHolder dataHolder = new DataHolder(getActivity());
dataHolder.insertData(rcfData);

Here are the items before I put them in sqlite: (Before clicking OnClick)

enter image description here

SecondFragment:

ListView lv2;
ArrayList<Uri> array_list2 = new ArrayList<Uri>();
ArrayAdapter<Uri> array_adapter2;

OnCreate...

array_adapter2 = new ArrayAdapter<Uri>(getActivity(), android.R.layout.simple_list_item_1, array_list2);
lv2 = (ListView) v.findViewById(R.id.list2);
lv2.setAdapter(array_adapter2);

OnClick...

Intent intent = getActivity().getIntent();
String rcfDataId = intent.getStringExtra("unique_id");
DataHolder dataHolder = new DataHolder(getActivity());
HashMap<String, String> rcfData = dataHolder.get_rcfData(rcfDataId);

    if(rcfData.size() !=0) {

        String s = rcfData.get("dateTime");
        Uri hello = Uri.parse(s);

        array_list2.add(hello);
        array_adapter2.notifyDataSetChanged();
    }

The problem is, when I try to load that string from sqlite to listview of the SecondFragment, it became like this: (After clicking OnClick of SecondFragment)

enter image description here

What I want it to be like (after clicking OnClick of SecondFragment), is like this:

enter image description here

How can I load it like that?

Upvotes: 0

Views: 179

Answers (1)

PaF
PaF

Reputation: 3477

I don't know DataHolder, and I don't know whether it's really backed by an sqlite DB, but this feels like a misuse - you'd probably want to insert each of your items from your ListView as a separate row to your table.

However, if you just want a quick workaround to make it work, you could just split the string you're reading, e.g:

if(rcfData.size() !=0) {

    String s = rcfData.get("dateTime");
    // Remove brackets
    s = s.substring(1, s.length() - 1);
    // Split string and insert to ListView
    for(String sPart : s.split(", ")) {
        Uri hello = Uri.parse(sPart);
        array_list2.add(hello);
    }
    array_adapter2.notifyDataSetChanged();
}

Upvotes: 1

Related Questions