gpt4 book ai didi

java - 为什么 SharedPreference 在 RecyclerView 中不起作用

转载 作者:行者123 更新时间:2023-12-01 18:04:04 24 4
gpt4 key购买 nike

下面给出了 mainactivity 和 viewactivity 的代码。

MainActivity.java这里是mainActivity的代码

package com.example.calcul;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

SharedPreferences sharedPreferences;

EditText nameET,itemET,priceET;
String name,item,price;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

nameET=findViewById(R.id.NameET);
itemET=findViewById(R.id.ItemET);
priceET=findViewById(R.id.PriceET);

}

public void SubmitBT(View view) {

name=nameET.getText().toString();
item=itemET.getText().toString();
price=priceET.getText().toString();

class SaveData extends AsyncTask<Void, Void, Void>{

@Override
protected Void doInBackground(Void... voids) {
Calculator calculator=new Calculator();

calculator.setName(name);
calculator.setItem(item);
calculator.setPrice(price);

DatabaseClient.getInstance(getApplicationContext()).appdatabase.calculatorDao().insertData(calculator);

return null;

}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
}
if (name.equals("") && item.equals("") && price.equals("") ){

Toast.makeText(this, "Please Enter your name and Item and Price ", Toast.LENGTH_SHORT).show();
}

else if (name.equals("") ){

Toast.makeText(this, "Please Enter your name ", Toast.LENGTH_SHORT).show();
}
else if (item.equals("")){

Toast.makeText(this, "Please Enter your Item", Toast.LENGTH_SHORT).show();
}
else if (price.equals("")){

Toast.makeText(this, "Please Enter your Price", Toast.LENGTH_SHORT).show();

}
else{
Intent intent=new Intent(this, ViewActivity.class);

intent.putExtra("PriceKey",price);

startActivity(intent);

SaveData saveData =new SaveData();
saveData.execute();
}
}

public void PreviousViewBT(View view) {
Intent intent=new Intent(this, ViewActivity.class);
startActivity(intent);

}
}

ViewActivity.java这里是查看activiy的代码

package com.example.calcul;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ViewActivity extends AppCompatActivity {

RecyclerView recyclerView;
List<Calculator>calculators;

String price;
TextView priceTV;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);

priceTV=findViewById(R.id.PriceTV);

if (loadPrice() !=null){
priceTV.setText("Price : "+loadPrice());

}
calculators =new ArrayList<>();

recyclerView=findViewById(R.id.RecyclerViewID);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

price=getIntent().getStringExtra("PriceKey");

//priceTV.setText("Total : "+price);

savePrice (price);

List<Calculator>calculators=DatabaseClient.getInstance(getApplicationContext()).appdatabase.calculatorDao().getData();
CustomAdapter customAdapter =new CustomAdapter(this, calculators);
recyclerView.setAdapter(customAdapter);

}


private void savePrice(String price){

SharedPreferences sharedPreferences=getSharedPreferences("Price",0);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("priceKey",price);
editor.commit();

}

private String loadPrice(){

SharedPreferences sharedPreferences=getSharedPreferences("Price",0);
String price=sharedPreferences.getString("priceKey",null);
return price;

}

public void NewBT(View view) {

Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);

}
}

这里没有任何运行时错误。但不工作共享Prefernce。我的意思是价格的值没有保存。当提交所有信息时,recyclerview 正在工作。但价格并不能节省。

最佳答案

如果我理解正确,那么您可以滚动 recyclerView 从而更改价格,但我没有看到处理元素滚动的代码。请提供更多信息。

关于java - 为什么 SharedPreference 在 RecyclerView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60588773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com