[英]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.