Android:Recyclerview onClick(int position)在刷新时添加到上一个位置

时间:2017-04-24 02:28:31

标签: java android android-intent android-recyclerview

作为noob开发者我正在开发一个flashcards应用程序,我使用recyclerview填充卡片。问题是当我使用FAB添加新卡时,我刷新主要活动。在这里,我发现当我点击任何卡时,它不会显示任何数据。调试显示,recyclerview片段中onClick(int position)的位置增加了以前的卡片大小,因此无法查看数据。如何获得正确的onClick位置?请帮忙..

public class CardFragment extends Fragment {
public static List<String> QArray = new ArrayList<>();
public CardFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final OnSelectFlashListner mCallback = null;

    // Inflate the layout for this fragment
    final RecyclerView flashRecycler = (RecyclerView) inflater.inflate(R.layout.recy_view , container, false);

    try{
        SQLiteOpenHelper flashCardsHelper1 = new FlashCardsHelper(getContext());
        SQLiteDatabase db = flashCardsHelper1.getReadableDatabase();
        Cursor cursor = db.query("Flashdata",new String[]{"question", "answer"},null,null,null,null,null);
        Log.d("Flash data", DatabaseUtils.dumpCursorToString(cursor));

        if (cursor.moveToFirst()){
            do {
                QArray.add(cursor.getString(0));
            }while (cursor.moveToNext());
        }
        cursor.close();
        db.close();

    }catch (Exception e){
        e.printStackTrace();
    }

    flashAdapter flashAdap = new flashAdapter(QArray);
    flashRecycler.setAdapter(flashAdap);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    flashRecycler.setLayoutManager(linearLayoutManager);

    flashAdap.setListner(new flashAdapter.Listner(){

        @Override
        public void onClick(int position) {
            Intent intent = new Intent(getActivity(),FlashCardOpen.class);
            intent.putExtra(FlashCardOpen.EXXTRA_QUESTIONNO,position);
            getActivity().startActivity(intent);
        }
    });
    Button button = (Button)flashRecycler.findViewById(R.id.btn_Add);

    return flashRecycler;
}

我通过FAB刷新主要活动:

private void loadFAB() {
    FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.dialog_box);
            dialog.setTitle("Add Flash Card");

            final EditText textViewQues = (EditText)dialog.findViewById(R.id.et_dialogQuestion);
            final EditText textViewAns = (EditText)dialog.findViewById(R.id.et_dialogAnswer);
            Button btn_cancel = (Button)dialog.findViewById(R.id.btn_cancel);
            btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            Button btn_add = (Button)dialog.findViewById(R.id.btn_Add);
            btn_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    writeToDb(textViewQues.getText().toString(),textViewAns.getText().toString());
                    showNotif();
                    Intent intent = new Intent(context,MainActivity.class);
                    context.startActivity(intent);
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    });

}

1 个答案:

答案 0 :(得分:0)

事实证明,这根本不是问题。我正在填充一个数组列表,该列表继续将整个列表附加到前一个列表之上。我只是在视图onCreateView()的开头添加了array.clear()并解决了问题。 调试规则!