简体   繁体   English

单击项目时将数据从 RecyclerView 传递到 Fragment

[英]Passing Data from RecyclerView to Fragment when Item is Clicked

I am currently implementing a Food Diary which doesn't seem to be functioning properly at the moment, upon opening the RecyclerView when the user clicks on an Item the Toast message is displayed appropriately and they navigated back to the fragment page.我目前正在实施一个食物日记,目前似乎无法正常运行,当用户单击一个项目时打开 RecyclerView 时,Toast 消息会正确显示,并且他们导航回片段页面。 However I would like to pass the data which they clicked on to the previous fragment and set a TextView appropriately with the item they selected, please see code below:但是我想将他们点击的数据传递给上一个片段,并使用他们选择的项目适当地设置 TextView,请参见下面的代码:

public class InputFoodToDiary extends AppCompatActivity {
RecyclerView recyclerView;
UserDatabase userDatabase;
ArrayList<String> FoodID, FoodName, Calories;

CustomAdapter customAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inputfoodtodiary);

    recyclerView = findViewById(R.id.recyclerView_InputFood);

    userDatabase = new UserDatabase(InputFoodToDiary.this, "CALTOR", null, 1);

    FoodID = new ArrayList<>();
    FoodName = new ArrayList<>();
    Calories = new ArrayList<>();

    storeDataInArrays();

    customAdapter = new CustomAdapter(InputFoodToDiary.this, FoodID, FoodName, Calories, new CustomAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(getApplicationContext(), Calories.get(position), Toast.LENGTH_SHORT).show();
            onBackPressed();
        }
    });
    recyclerView.setAdapter(customAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(InputFoodToDiary.this));
}

void storeDataInArrays(){
    Cursor cursor = userDatabase.readAllData();
    if(cursor.getCount() == 0){
        Toast.makeText(this, "No Data.", Toast.LENGTH_SHORT).show();
    } else {
        while (cursor.moveToNext()){
            FoodID.add(cursor.getString(0));
            FoodName.add(cursor.getString(1));
            Calories.add(cursor.getString(2));
        }
    }
}

public void onBackPressed(){
    super.onBackPressed();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.food_search, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView)searchItem.getActionView();

    searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            customAdapter.getFilter().filter(newText);
            return false;
        }
    });
    return true;
}

} }

From what I see you don't use fragments, you are using activity.从我看到你不使用片段,你正在使用活动。 When back press is called from an activity it closes itself.当从活动中调用后按时,它会自行关闭。 The solution would be to have the recycler view in a fragment under your activity and when you click the item in the recycler view, save the data in a member of the parent activity, than when the fragment disapear, the data will be saved in the parent activity and you can do whatever you want with it.解决方案是将回收器视图放在您的活动下的片段中,当您单击回收器视图中的项目时,将数据保存在父活动的成员中,而不是当片段消失时,数据将保存在父活动,你可以用它做任何你想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从 RecyclerView 传递到 Fragment? - Passing data from RecyclerView to Fragment? 将数据从 RecyclerView 传递到 Fragment - Passing data from a RecyclerView to Fragment 将RecyclerView CardView单击项目数据传递给活动 - Passing RecyclerView CardView Clicked Item Data To Activity 单击第一个 RecyclerView 中的项目时,如何将数据从一个 RecyclerView 流向另一个 RecyclerView? - How to flow Data from One RecyclerView to Another RecyclerView when an item in first RecyclerView is clicked? 使用导航组件单击 RecyclerView 项目时如何从 RecyclerView OnClick 侦听器更改片段 - How to change fragment from the Recycler View OnClick Listener when RecyclerView item is clicked using Navigation Components 当我点击一个recyclerview项目时,如何从一个片段移动到一个新的活动? - how to move from a fragment to a new activity when i clicked an item of recyclerview? 在recyclerview中单击项目时获取数据 - get the data when an Item is clicked in recyclerview 单击recyclerview上的项目时的多个数据 - Multiple data when clicked item on recyclerview 当我在 RecyclerView 项目中单击时,片段加载数据错误数据 - Fragment loading data wrong data when I click in RecyclerView item 单击RecyclerView项时发生NullPointerException - NullPointerException when RecyclerView item is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM