Java 如何从SharedReferences向Listview显示数据
我有两个活动(DetailProduct和Wishlist),我想在DetailProduct和Wishlist之间显示数据,在这里我使用SharedReferences将数据从DetailProduct放到Wishlist 这是我的产品Java 如何从SharedReferences向Listview显示数据,java,android,listview,arraylist,sharedpreferences,Java,Android,Listview,Arraylist,Sharedpreferences,我有两个活动(DetailProduct和Wishlist),我想在DetailProduct和Wishlist之间显示数据,在这里我使用SharedReferences将数据从DetailProduct放到Wishlist 这是我的产品 wishlist.setOnClickListener(new View.OnClickListener() { String imgpicaso = getIntent().getStringExtra("aaa"); Str
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);
}
});
这是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();
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wishlist);
setTitle(“您的愿望列表”);
ImageView resultgmb=(ImageView)findViewById(R.id.resultgmb);
TextView detailtok=(TextView)findViewById(R.id.detailtok);
TextView resulttext=(TextView)findViewById(R.id.resulttext);
按钮bayarwish=(按钮)findviewbyd(R.id.check);
按钮hapus=(按钮)findViewById(R.id.delete);
SharedReferences SharedReferences=getApplicationContext().GetSharedReferences(“baba”,模式_PRIVATE);
字符串imgpicaso=SharedReferences.getString(“aaa”,即“”);
Picasso.with(getBaseContext()).load(imgpicaso).into(resultgmb);
String detailtext=SharedReferences.getString(“名称”,“名称”);
detailtok.setText(detailtext);
String text=SharedReferences.getString(“ccc”和“”);
resulttext.setText(“$:”+文本);
listproduct=(ListView)findViewById(R.id.listproduct);
ArrayList ArrayList=新的ArrayList();
ArrayAdapter arr=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1);
setAdapter(arr);
arr.notifyDataSetChanged();
}
这是xml愿望列表
<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>
请帮帮我,谢谢你。正如@sark9012所指出的,你可能希望使用意图数据,而不是共享首选项。这是一个教程 编辑 尽管我确实建议不要使用共享首选项数据来实现您所寻找的目的,但似乎您获得了从共享首选项检索数据的正确代码,您可能遗漏的是填充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();
listproduct=(ListView)findViewById(R.id.listproduct);
ArrayList ArrayList=新的ArrayList();
ArrayAdapter arr=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1);
setAdapter(arr);
arr.notifyDataSetChanged();
我并没有看到您用检索到的数据填充适配器,所以您可能希望对此进行研究。从CodePath检查如何使用ArrayAdapter设置ListView。生成逗号分隔字符串并将其存储在共享首选项中 在存储到SP时 类似于string1=a1+“,“a2+”,“a3 从SP检索时 like数组[]=string1.split(“,”) ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,数组) 当您启动第二个活动时,您是否知道您可以在Intent中传递数据?您可以给我一个示例教程吗?我想从SharedReferences向listview显示数据当按钮单击Tutorial not working时,我想从SharedReferences向listview显示数据。你有主意吗?