为什么SharedPreference在RecyclerView中不起作用



下面给出的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这里是查看活动的代码

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);
}
}

这里没有任何运行时错误。但不共享工作偏好。我的意思是价格的价值不在于节省。当提交所有信息时,回收查看工作。但普莱斯并没有节省开支。

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

相关内容

  • 没有找到相关文章

最新更新