2016-10-25 79 views
0

我有一個RecycleView我向其中ArrayPost對象。當它第一次加載時,一切正常,但試圖替換數據或再次輸入Fragment,然後RecycleView不顯示任何內容。我也檢查了新數據的大小,它與零不同。這裏是我的代碼:RecycleView僅顯示數據第一次

// Init the list 
list = (RecyclerView) view.findViewById(R.id.list); 
list.setLayoutManager(new LinearLayoutManager(mContext)); 

          ... 


if (adapter != null) { 
    adapter.replaceCurrentPosts(new ArrayList<>(postList)); 
} 
else { 
    adapter = new PostAdapter(mContext, new ArrayList<>(postList), this); 
    list.setAdapter(adapter); 
} 

         ... 

public PostAdapter(Context context, List<Post> posts, onPostElementClickListener listener) {  
    this.context = context; 
    this.posts = posts; 
    this.mListener = listener; 
} 

public void replaceCurrentPosts(List<Post> newPosts) {  
    this.posts.clear(); 
    this.posts.addAll(newPosts); 
    this.notifyDataSetChanged(); 
} 

@Override 
public int getItemCount() { 
    return posts.size(); 
} 
+0

adapter.replaceCurrentPosts(postList); – Ricardo

+0

應該投射,因爲postList是集合 – charbinary

+0

而在投射後,您檢查了列表是否爲空? – Ricardo

回答

-1

你爲什麼不嘗試添加一個元素(或者更多),而不是刪除一切,重新添加的所有項目?

0

package com.example.jiteshmohite.myapplication;

公共類PostAdapter擴展RecyclerView.Adapter {

private Context context; 
private List<Post> posts; 
private onPostElementClickListener mListener; 

// Provide a reference to the views for each data item 
// Complex data items may need more than one view per item, and 
// you provide access to all the views for a data item in a view holder 
public static class ViewHolder extends RecyclerView.ViewHolder { 
    // each data item is just a string in this case 
    public TextView mTextView; 
    public ViewHolder(View v) { 
     super(v); 
     mTextView = (TextView) v.findViewById(R.id.info_text); 
    } 
} 

public PostAdapter(Context context, List<Post> posts, onPostElementClickListener listener) { 
    this.context = context; 
    this.posts = posts; 
    this.mListener = listener; 
} 

public void replaceCurrentPosts(List<Post> newPosts) { 
    posts.clear(); 
    posts = newPosts; 
    this.notifyDataSetChanged(); 
} 

@Override 
public PostAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    // create a new view 
    View v = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.my_text_view, parent, false); 
    // set the view's size, margins, paddings and layout parameters 
    ViewHolder vh = new ViewHolder(v); 
    return vh; 
} 

@Override 
public void onBindViewHolder(PostAdapter.ViewHolder holder, final int position) { 
    // - get element from your dataset at this position 
    // - replace the contents of the view with that element 
    holder.mTextView.setText(posts.get(position).getName()); 
    holder.mTextView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mListener.onClick(position); 
     } 
    }); 

} 

@Override 
public int getItemCount() { 
    return posts.size(); 
} 
interface onPostElementClickListener { 
    public void onClick(int pos); 
} 

}

private RecyclerView list; 
private PostAdapter postAdapter; 
private RecyclerView.LayoutManager layoutManager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_recycler_example); 
    list = (RecyclerView) findViewById(R.id.list); 
    layoutManager = new LinearLayoutManager(getApplicationContext()); 
    list.setLayoutManager(layoutManager); 
    postAdapter = new PostAdapter(getApplicationContext(), getPostData(), listener); 
    list.setAdapter(postAdapter); 
} 


private PostAdapter.onPostElementClickListener listener = new PostAdapter.onPostElementClickListener() { 
    @Override 
    public void onClick(int pos) { 
     Toast.makeText(getApplicationContext(), pos + "" , Toast.LENGTH_SHORT).show(); 
     List<Post> posts = getPostData(); 
     posts.get(pos).setName("abc"); 
     postAdapter.replaceCurrentPosts(posts); 
    } 
}; 

// get post list which contains name 
public List<Post> getPostData() { 
    Post post = new Post(); 
    post.setName("xyz"); 
    List<Post> posts = new ArrayList<Post>(); 
    posts.add(post); 
    posts.add(post); 
    posts.add(post); 
    posts.add(post); 
    posts.add(post); 
    return posts; 
} 

}

我建議而不是notifyDataSetChanged(),你應該參考notifyItemChange其修改特定的位置。

請讓我知道,如果它不適合你。

相關問題