java如何从SharedReferences向Listview显示数据
我有两个活动(DetailProduct和Wishlist),我想在DetailProduct和Wishlist之间显示数据,在这里我使用SharedReferences将数据从DetailProduct放到Wishlist
这是我的产品
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 Wishlist, please open this img
这是文件愿望清单。爪哇
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, 安卓.R.layout.simple_list_item_1);
listproduct.setAdapter(arr);
arr.notifyDataSetChanged();
}
这是xml愿望列表
<SearchView
安卓:id="@+id/search"
安卓:background="@color/cardview_light_background"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
</SearchView>
<LinearLayout
安卓:orientation="vertical"
安卓:id="@+id/relone"
安卓:layout_below="@id/search"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<RelativeLayout
安卓:id="@+id/rel"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<ImageView
安卓:src="@drawable/noimage"
安卓:id="@+id/resultgmb"
安卓:layout_width="130dp"
安卓:layout_height="130dp"
安卓:layout_alignParentRight="true"/>
<TextView
安卓:layout_marginTop="10dp"
安卓:text="setext"
安卓:textStyle="bold"
安卓:textColor="#000"
安卓:textSize="17dp"
安卓:id="@+id/detailtok"
安卓:layout_width="wrap_content"
安卓:layout_height="30dp" />
<TextView
安卓:text="setdescription"
安卓:layout_below="@id/detailtok"
安卓:textColor="#f00"
安卓:id="@+id/resulttext"
安卓:layout_width="wrap_content"
安卓:layout_height="30dp" />
<LinearLayout
安卓:layout_below="@id/resulttext"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content">
<Button
安卓:id="@+id/check"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="CHECK"/>
<Button
安卓:id="@+id/delete"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="DELETE"/>
</LinearLayout>
<ListView
安卓:id="@+id/listproduct"
安卓:layout_marginTop="20dp"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
</ListView>
</RelativeLayout>
</LinearLayout>
请帮帮我,谢谢
# 1 楼答案
正如@sark9012所指出的,您可能希望使用意图数据,而不是共享首选项Here是关于这方面的教程
编辑
尽管我确实建议不要使用共享首选项数据来实现您所寻找的目的,但似乎您获得了从共享首选项检索数据的正确代码,您可能遗漏的是填充ListView。这是您发送给我们的代码:
我并没有看到您用检索到的数据填充适配器,所以您可能希望对此进行研究。检查CodePath中的this example,了解如何使用ArrayAdapter设置ListView
# 2 楼答案
生成逗号分隔的字符串并将其存储在共享首选项中
在存储到SP时
类似于string1=a1+“,“a2+”,“a3
从SP检索时
like数组[]=string1。拆分(“,”)
ArrayAdapter=新的ArrayAdapter(此, 安卓R.布局。简单_列表_项_1,数组)