[英]Recycler view doesn't show first item until refresh
I've got an app which saves a number and the date it was recorded in a recycler View, however when the array is empty and then I try to save my number, the RecyclerView does not show the item. 我有一个应用程序,该应用程序可以保存一个数字及其在Recycler View中的记录日期,但是当数组为空然后尝试保存我的数字时,RecyclerView不会显示该项目。 The item did save, but it isn't displayed in the view. 该项目已保存,但未在视图中显示。 I know it save because when I go back to the view the item is there. 我知道它可以保存,因为当我返回视图时,该项目在那里。 I have looked ages for answer online and have seen nobody with the same issue. 我在网上寻找答案的年龄已久,却没有人遇到同样的问题。 Thank you in advance for your help! 预先感谢您的帮助!
Activity 2: 活动2:
package com.pinet.count;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class Activity2 extends AppCompatActivity {
private ExampleAdapter mAdapter;
private static final Date currentTime = Calendar.getInstance().getTime();
private ArrayList<ExampleItem> mexampleList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent = getIntent();
String dateString= DateFormat.format("dd/MM/yyyy",currentTime).toString();
int count = intent.getIntExtra(MainActivity.EXTRA_NUMBER,0);
loadData();
buildRecyclerView();
if (count!=0){
if (mexampleList == null){
mexampleList = new ArrayList<ExampleItem>();
}
mexampleList.add(0, new ExampleItem(String.valueOf(count), dateString));
mAdapter.notifyItemInserted(0);
mAdapter.notifyItemRangeChanged(0,mexampleList.size());
}
saveList();
}
private void buildRecyclerView(){
RecyclerView mRecyclerView = findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mAdapter= new ExampleAdapter(mexampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
@Override
public void onDeleteClick(int position) {
removeItem(position);
}
});
}
private void removeItem(int position){
mexampleList.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, mexampleList.size());
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences",0);
sharedPreferences.edit().remove("count list").apply();
}package com.pinet.count;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateFormat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class Activity2 extends AppCompatActivity {
private ExampleAdapter mAdapter;
private static final Date currentTime = Calendar.getInstance().getTime();
private ArrayList<ExampleItem> mexampleList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent = getIntent();
String dateString= DateFormat.format("dd/MM/yyyy",currentTime).toString();
int count = intent.getIntExtra(MainActivity.EXTRA_NUMBER,0);
loadData();
buildRecyclerView();
if (count!=0){
if (mexampleList == null){
mexampleList = new ArrayList<ExampleItem>();
}
mexampleList.add(0, new ExampleItem(String.valueOf(count), dateString));
mAdapter.notifyItemInserted(0);
mAdapter.notifyItemRangeChanged(0,mexampleList.size());
}
saveList();
}
private void buildRecyclerView(){
RecyclerView mRecyclerView = findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
mAdapter= new ExampleAdapter(mexampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
@Override
public void onDeleteClick(int position) {
removeItem(position);
}
});
}
private void removeItem(int position){
mexampleList.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, mexampleList.size());
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences",0);
sharedPreferences.edit().remove("count list").apply();
}
private void saveList(){
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(mexampleList);
editor.putString("count list", json);
editor.apply();
}
private void loadData(){
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("count list", null);
Type type = new TypeToken<ArrayList<ExampleItem>>() {}.getType();
mexampleList = gson.fromJson(json, type);
}
}
`
private void saveList(){
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(mexampleList);
editor.putString("count list", json);
editor.apply();
}
private void loadData(){
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("count list", null);
Type type = new TypeToken<ArrayList<ExampleItem>>() {}.getType();
mexampleList = gson.fromJson(json, type);
}
}
Main Activity: 主要活动:
package com.pinet.count;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int count = 0;
public static final String EXTRA_NUMBER ="com.pinet.count.EXTRA_NUMBER";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView= findViewById(R.id.textView);
Button buttonPos = findViewById(R.id.button);
Button buttonNeg = findViewById(R.id.button2);
Button buttonReset = findViewById(R.id.button3);
Button buttonSave = findViewById(R.id.button4);
Button butonHistory = findViewById(R.id.button5);
buttonPos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count +=1;
textView.setText(String.valueOf(count));
}
});
buttonNeg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count-=1;
textView.setText(String.valueOf(count));
}
});
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count = 0;
textView.setText(String.valueOf(count));
}
});
butonHistory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
OpenActivity2();
}
});
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivity2();
}
});
}
private void openActivity2(){
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_NUMBER, count);
startActivity(intent);
}
private void OpenActivity2(){
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
ExampleAdapter: ExampleAdapter:
package com.pinet.count;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private final ArrayList<ExampleItem> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onDeleteClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
final TextView mTextView1;
final TextView mTextView2;
final ImageView mDeleteImage;
ExampleViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mTextView1=itemView.findViewById(R.id.textView);
mTextView2=itemView.findViewById(R.id.textView2);
mDeleteImage=itemView.findViewById(R.id.image_delete);
mDeleteImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener !=null){
int position = getAdapterPosition();
if (position!=RecyclerView.NO_POSITION){
listener.onDeleteClick(position);
}
}
}
});
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList){
mExampleList = exampleList;
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
return new ExampleViewHolder(v, mListener);
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mTextView1.setText(currentItem.getmText1());
holder.mTextView2.setText(currentItem.getmText2());
}
@Override
public int getItemCount() {
if (mExampleList !=null) {
return mExampleList.size();
}
else {
return 0;
}
}
}
ExampleItem : ExampleItem:
package com.pinet.count;
class ExampleItem {
private final String mText1;
private final String mText2;
public ExampleItem(String text1, String text2){
mText1= text1;
mText2= text2;
}
public String getmText1(){
return mText1;
}
public String getmText2(){
return mText2;
}
}
If you could also help me figure out animating the recycler view that would be appreciated ! 如果您还可以帮助我弄清楚动画制作回收者的观点,将不胜感激!
PiNet PINET
Change your onCreate()
method inside the "Activity2" Activity
to look like this: 在“ Activity2” Activity
更改您的onCreate()
方法,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
String dateString = DateFormat.format("dd/MM/yyyy",currentTime).toString();
Intent intent = getIntent();
int count = intent.getIntExtra(MainActivity.EXTRA_NUMBER,0);
loadData();
if (count!=0){
if (mexampleList == null){
mexampleList = new ArrayList<ExampleItem>();
}
String value = String.valueOf(count);
ExampleItem exampleItem = new ExampleItem(value, dateString);
mexampleList.add(0, exampleItem);
}
buildRecyclerView();
saveList();
}
I've changed the order of adding the new data to the ArrayList
. 我更改了将新数据添加到ArrayList
的顺序。 This way the "new item" will be added to the list before you set the adapter. 这样,在设置适配器之前,“新项目”将被添加到列表中。 I have also extrapolated some if the code into distinct code lines. 如果代码分成不同的代码行,我也将其推断出来。 I recommend this because it will be easier to debug. 我建议这样做,因为它更容易调试。
A few additional comments: 其他一些评论:
The method removeItem(int position)
appears to remove the entire list from your SharedPreferences
instead of what I assume you only want to remove the one item. 方法removeItem(int position)
似乎是从SharedPreferences
删除整个列表,而不是我假设您只想删除一个项目的列表。
Also in your "MainActivity" you have two methods: 同样在“ MainActivity”中,您有两种方法:
openActivity2()
and OpenActivity2()
openActivity2()
和OpenActivity2()
Firstly, Java naming convention dictates that method names begin with lower case. 首先,Java命名约定规定方法名称以小写字母开头。 Secondly, this is very poor programing style to give methods indistinguishable method names. 其次,这是非常差的编程风格,无法给方法名称赋予难以区分的方法名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.