[英]passing string from an activity to a fragment
您好我一直在研究問題並找到了將字符串從活動(不是片段活動)傳遞到片段的解決方案。 我想通過點擊從 RestaurantsList.class 到 Mainsfragment.class 的文檔 ID,在我的 onclick 中。
我得到的錯誤信息如下:
嘗試在 null object 參考上調用虛擬方法 'android.content.Intent android.app.Activity.getIntent()'
這是我的活動代碼:
package com.example.hostapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RelativeLayout;
import com.example.hostapp.Adapters.RestaurantAdapter;
import com.example.hostapp.Adapters.categoryCardAdapter;
import com.example.hostapp.MenuFragments.MainsFragment;
import com.example.hostapp.Models.Restaurant;
import com.example.hostapp.Models.categoryCard;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class RestaurantList extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference restaurantref = db.collection("restaurants");
private RestaurantAdapter adapter;
String categoryid = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
setUpRecyclerView();
}
private void setUpRecyclerView() {
//Get intent
if (getIntent() != null)
categoryid = getIntent().getStringExtra("categoryid");
if (!categoryid.isEmpty() && categoryid != null) {
Query query = restaurantref.whereEqualTo("categoryid", categoryid).orderBy("name");
final FirestoreRecyclerOptions<Restaurant> options = new FirestoreRecyclerOptions.Builder<Restaurant>().setQuery(query, Restaurant.class).build();
adapter = new RestaurantAdapter(options);
RecyclerView recyclerView = findViewById(R.id.restaurant_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListerner(new RestaurantAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Restaurant restaurant = documentSnapshot.toObject(Restaurant.class);
Intent foodlist = new Intent(RestaurantList.this, Foodlist.class);
Bundle bundle = new Bundle();
bundle.putString("restaurantid", documentSnapshot.getId());
MainsFragment m4 = new MainsFragment();
m4.setArguments(bundle);
//foodlist.putExtra("restaurantid", documentSnapshot.getId());
startActivity(foodlist);
}
});
}
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
這是我的片段代碼:
package com.example.hostapp.MenuFragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.hostapp.Adapters.FoodAdapter;
import com.example.hostapp.Models.FoodModel;
import com.example.hostapp.R;
import com.example.hostapp.RestaurantList;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class MainsFragment extends Fragment {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private FoodAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mains, container, false);
String restaurantNumber = getArguments().getString("restaurantid");
Query menuref = db.collectionGroup("Foods").whereEqualTo("menuid", restaurantNumber);
FirestoreRecyclerOptions<FoodModel> options = new FirestoreRecyclerOptions.Builder<FoodModel>().
setQuery(menuref, FoodModel.class)
.build();
adapter = new FoodAdapter(options);
RecyclerView recyclerView = view.findViewById(R.id.recycler_mains);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
private void setUpRecyclerView() {
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
我也有過這樣的經歷。 以下是我的處理方式:
1,通過intent.putExtra()將你的數據從RestaurantList活動傳遞到你的foodlist活動;
Intent foodlist = new Intent(RestaurantList.this, Foodlist.class);
foodlist.putExtra("restaurantid", documentSnapshot.getId());
startActivity(foodlist);
2、通過getIntent()從Foodlist活動中獲取值;
String id = getIntent().getStringExtra("restaurantid");
3,將您的數據傳遞給您正在初始化適配器的適配器。 (像這樣的東西:)
YourAdapter adapter = new YourAdapter(getSupportFragmentManager(), tabLayout.getTabCount(),id);
4,在適配器的構造函數中獲取數據(您必須創建一個全局變量並將其填充到構造函數中)並在getItem()方法中創建一個捆綁並將數據傳遞給它並將arguments設置為片段(如下面的代碼)
public class YourAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
String id;
public AdapterPagerShift(FragmentManager fm, int NumOfTabs, String id) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.id = id;
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("restaurantid", documentSnapshot.getId());
switch (position) {
case 0:
MainsFragment main = new MainsFragment();
main.setArguments(bundle);
return main;
case 1:
//...
case 2:
//...
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
5,在您的 MainsFragment class 中使用以下代碼獲取數據:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String id = getArguments().getString("restaurantid");
}
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.