我正在使用Firebase用戶界面,並遇到一個獨特的問題。當我加載RecyclerView時,沒有問題。我單擊RecyclerView上的一個項目,並獲取被點擊的項目的.push()值。然後,它會從存儲在Firebase數據庫中的該Firebase .push()值中獲取數據,並將其發送到其他活動。見圖像下方:Firebase RecylcerView適配器不準確查看位置
第二圖像是經由意圖接收數據的活性。當我點擊回到RecyclerView時,我再次點擊「8」,然後我加載一個不同的數字(現在看起來是1)。
最終,我擔心我沒有正確設置RecyclerView。這裏是我的代碼:
創建民意調查對象(POJO)和寫入火力地堡:
mSubmitPollCreation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO: Need to check if poll requirements are added, i.e. Question, Answer, ......
//check if image has been loaded first
if (resultImageURL == null) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.no_image_selected), Toast.LENGTH_LONG).show();
return;
}
//capture answers
if (mNumberOfPollAnswersCreatedByUser > 5) {
Toast.makeText(getApplicationContext(), getResources().getText(R.string.poll_answers_greater_than_five), Toast.LENGTH_LONG).show();
mNumberOfPollAnswersCreatedByUser = 5;
}
for (int i = 0; i < mNumberOfPollAnswersCreatedByUser; i++) {
EditText editText = (EditText) mEditTextAnswerLayout.findViewWithTag(getResources().getString(R.string.created_answer_editText_id) + String.valueOf(i + 1));
String editTextInputForAnswer = String.valueOf(editText.getText());
mPollAnswers.add(0, editTextInputForAnswer); Toast.makeText(getApplicationContext(),editTextInputForAnswer,Toast.LENGTH_SHORT).show();
}
Poll poll = new Poll(mCreatePollQuestion.getText().toString(), resultImageURL, mPollAnswers);
Map<String, Object> pollMap = poll.toMap();
final String key = mBaseRef.child("Polls").push().getKey();
Map<String, Object> childUpdates = new HashMap<String, Object>();
childUpdates.put("/Polls/" + key, pollMap);
mBaseRef.updateChildren(childUpdates);
Collections.reverse(mPollAnswers);
for (int i = 0; i < mPollAnswers.size(); i++) {
mBaseRef.child("Polls").child(key).child("answers").child(String.valueOf(i + 1)).updateChildren(poll.answerConvert(mPollAnswers, i));
}
Intent toHomeActivity = new Intent(CreateActivity.this, HomeActivity.class);
toHomeActivity.putExtra("viewpager_position", 2);
startActivity(toHomeActivity);
}
});
在哪裏,我從火力地堡閱讀並填充RecyclerView:
public class LiveFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private RecyclerView mRecyclerview;
private DatabaseReference mBaseRef;
private DatabaseReference mPollsRef;
private LinearLayoutManager mLayoutManager;
private FloatingActionButton mFloatingActionAdd;
private FirebaseRecyclerAdapter<Poll, PollHolder> mFireAdapter;
private RecyclerView.ItemAnimator mItemAnimator;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public LiveFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment LiveFragment.
*/
// TODO: Rename and change types and number of parameters
public static LiveFragment newInstance(String param1, String param2) {
LiveFragment fragment = new LiveFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBaseRef = FirebaseDatabase.getInstance().getReference();
mPollsRef = mBaseRef.child("Polls");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.fragment_new, container, false);
Log.v("TAG", "ON CREATE CALLED FROM NEW");
mRecyclerview = (RecyclerView) v.findViewById(R.id.list);
mRecyclerview.setItemAnimator(new SlideInLeftAnimator());
mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mFloatingActionAdd = (FloatingActionButton) getActivity().findViewById(R.id.myFAB);
mFloatingActionAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent I = new Intent(getActivity().getApplicationContext(), CreateActivity.class);
startActivity(I);
}
});
mRecyclerview.setLayoutManager(mLayoutManager);
return v;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
scrollToPosition();
}
@Override
public void onStart() {
super.onStart();
mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mBaseRef.child("Polls")) {
@Override
protected void populateViewHolder(PollHolder viewHolder, final Poll model, final int position) {
viewHolder.mPollQuestion.setText(model.getQuestion());
Picasso.with(getActivity().getApplicationContext())
.load(model.getImage_URL())
.fit()
.into(viewHolder.mPollImage);
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.v("TAG", model.toString());
Log.v("TAG","ONCLICKED");
Intent toPoll = new Intent(getActivity(), PollHostActivity.class);
toPoll.putExtra("POLL_ID", mFireAdapter.getRef(position).getKey());
startActivity(toPoll);
}
});
Log.v("TAG", mFireAdapter.getRef(position).getKey());
Log.v("TAG", String.valueOf(position));
Log.v("TAG", model.getQuestion());
Log.v("TAG", model.getImage_URL());
}
};
mRecyclerview.setAdapter(mFireAdapter);
scrollToPosition();
}
@Override
public void onStop() {
super.onStop();
if (mFireAdapter != null) {
mFireAdapter.cleanup();
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public static class PollHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView mPollQuestion;
ImageView mPollImage;
View mView;
public PollHolder(View itemView) {
super(itemView);
mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question);
mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage);
this.mView = itemView;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Log.v("TAG", "CLICKED");
}
}
private void scrollToPosition(){
mFireAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int pollCount = mFireAdapter.getItemCount();
int lastVisiblePosition = mLayoutManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the user is at the bottom of the list, scroll
// to the bottom of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (pollCount - 1) && lastVisiblePosition == (positionStart - 1))) {
mRecyclerview.scrollToPosition(positionStart);
}
}
});
}
}
我的想法:
我在正確的位置打電話給mFireAdapter.cleanup()?
單擊「8」並看到「1」的問題沒有出現在RecyclerView的第一次加載中,它只發生在從活動(第二個圖像)返回並回到RecyclerView時。
我調用.setStackFromEnd(true)和.setReverseLayout(true)來反轉存儲在FirebaseDatabase中的數據,這可能會導致問題。
我設立在.populateViewHolder的.setOnClickListener()()方法,並使用以下中的意圖傳遞.push()鍵到下一個活動:
toPoll.putExtra("POLL_ID", mFireAdapter.getRef(position).getKey());
此方法可能會得到不正確的密鑰。
最後,火力地堡數據庫:
任何幫助,將不勝感激!
嘗試用'holder.getAdapterPosition()替換'position'()' – koceeng
想作爲答案嗎?似乎現在工作... – tccpg288
也發佈在Github上:https://github.com/firebase/FirebaseUI-Android/issues/480 –