如何从SharedPreferences向Listview显示数据

时间:2017-12-12 23:40:08

标签: java android listview arraylist sharedpreferences

我有两个活动(DetailProduct和Wishlist),我想显示从DetailProduct到Wishlist的数据,在这里我使用SharedPreferences将数据从DetailProduct添加到Wishlist。

这是我的活动DetailProduct

wishlist.setOnClickListener(new View.OnClickListener() {
        String imgpicaso = getIntent().getStringExtra("aaa");
        String vardetailed = getIntent().getStringExtra("acb");
        String thisname = getIntent().getStringExtra("name");
        String text = getIntent().getStringExtra("abb");
        @Override
        public void onClick(View view) {
            Intent tambah = new Intent(DetailProduct.this, Wishlist.class);
            SharedPreferences sharedPreferences = DetailProduct.this.getSharedPreferences("baba", MODE_PRIVATE);
            SharedPreferences.Editor shreditor = sharedPreferences.edit();
            shreditor.putString("aaa", imgpicaso);
            shreditor.putString("name", thisname);
            shreditor.putString("abb", text);
            shreditor.commit();
            startActivity(tambah);
        }
    });

This is img DetailProduct

This is img Wishlist, please open this img

这是文件Wishlist.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wishlist);
    setTitle("Your Wishlist");

    ImageView resultgmb = (ImageView) findViewById(R.id.resultgmb);
    TextView detailtok = (TextView) findViewById(R.id.detailtok);
    TextView resulttext = (TextView) findViewById(R.id.resulttext);
    Button bayarwish = (Button) findViewById(R.id.check);
    Button hapus = (Button) findViewById(R.id.delete);


    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("baba", MODE_PRIVATE);
    String imgpicaso = sharedPreferences.getString("aaa", "");
    Picasso.with(getBaseContext()).load(imgpicaso).into(resultgmb);
    String detailtext = sharedPreferences.getString("name", "");
    detailtok.setText(detailtext);
    String text = sharedPreferences.getString("ccc", "");
    resulttext.setText("$: " +text);

    listproduct = (ListView) findViewById(R.id.listproduct);
    ArrayList<String> arrayList = new ArrayList<>();
    ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    listproduct.setAdapter(arr);

    arr.notifyDataSetChanged();

}

这是xml Wishlist

<SearchView
    android:id="@+id/search"
    android:background="@color/cardview_light_background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</SearchView>

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/relone"
    android:layout_below="@id/search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout

        android:id="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ImageView
        android:src="@drawable/noimage"
        android:id="@+id/resultgmb"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_alignParentRight="true"/>
    <TextView
        android:layout_marginTop="10dp"
        android:text="setext"
        android:textStyle="bold"
        android:textColor="#000"
        android:textSize="17dp"
        android:id="@+id/detailtok"
        android:layout_width="wrap_content"
        android:layout_height="30dp" />
    <TextView
        android:text="setdescription"
        android:layout_below="@id/detailtok"
        android:textColor="#f00"
        android:id="@+id/resulttext"
        android:layout_width="wrap_content"
        android:layout_height="30dp" />

        <LinearLayout
            android:layout_below="@id/resulttext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/check"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="CHECK"/>
            <Button
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="DELETE"/>
        </LinearLayout>


    <ListView
        android:id="@+id/listproduct"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
    </RelativeLayout>
</LinearLayout>

请帮帮我,谢谢

2 个答案:

答案 0 :(得分:1)

正如@ sark9012指出的那样,您可能希望为此目的使用Intent Data而不是共享首选项。 Here是一个教程。

修改

虽然我真的建议不要将共享首选项数据用于您正在寻找的目的,但似乎您从共享首选项中检索数据的代码是正确的,您可能会错过的是填充ListView 。这是您发给我们的代码:

listproduct = (ListView) findViewById(R.id.listproduct);
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listproduct.setAdapter(arr);

arr.notifyDataSetChanged();

我真的没有看到你使用检索到的数据填充适配器,所以你可能想看看它。从CodePath检查this example如何使用ArrayAdapter设置ListView。

答案 1 :(得分:0)

生成以逗号分隔的字符串并将其存储在共享首选项

中 存储到SP时

喜欢string1 = a1 +&#34;,&#34; a2 +&#34;,&#34; a3;

从SP检索时

like array [] = string1.split(&#34;,&#34;);

ArrayAdapter adapter = new ArrayAdapter(this,                     android.R.layout.simple_list_item_1,array);