我试图找出如何在片段中显示我所拥有的Bundle数据并将其显示在RecyclerView中。在我的活动的onClick方法中,我从用户获取输入并将值包装在Bundle中。然后我将它发送到要在RecyclerView中显示的片段。我对如何设置我的RecyclerView适配器以显示Bundle数据感到有点困惑任何帮助将不胜感激。
这是我的活动代码
public class OwesActivity extends AppCompatActivity
{
private Button okButton;
private EditText nameEditText;
private EditText amountEditText;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owes);
spinner = (Spinner)findViewById(R.id.spinner);
nameEditText = (EditText)findViewById(R.id.name_edit_text);
amountEditText = (EditText)findViewById(R.id.amount_edit_text);
okButton = (Button)findViewById(R.id.okButton);
okButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (isInputEmpty())
{
}
else
{
Bundle extras = new Bundle();
extras.putString("nameKey", nameEditText.getText().toString());
extras.putString("amountKey", amountEditText.getText().toString());
Fragment fragment;
switch (spinner.getSelectedItemPosition()) //value of spinner determines which list the extras go to.
{
case 0:// Owes Me
fragment = new OwesMeFragment();
fragment.setArguments(extras);
break;
case 1: // I Owe
fragment = new OweFragment();
fragment.setArguments(extras);
break;
default:
break;
}
startNewActivity();
}
}
});
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_choices, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
public boolean isInputEmpty()
{
if(nameEditText.getText().toString().isEmpty()||
amountEditText.getText().toString().isEmpty())
{
Toast.makeText(this, "Field cannot be empty", Toast.LENGTH_SHORT).show();
return true;
}
else
{
return false;
}
}
public void startNewActivity()
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
这是片段
public class OwesMeFragment extends Fragment
{
public OwesMeFragment()
{
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
//Inflate the view for this fragment
View view = inflater.inflate(R.layout.fragment_owe, container, false);
Bundle extras = this.getArguments();
if(extras != null)
{
String nameExtra = extras.getString("nameKey");
String amountExtra = extras.getString("amountKey");
}
//get a reference to the recyclerView
RecyclerView oweRecyclerView = (RecyclerView)view.findViewById(R.id.owe_recycler_view);
//set the layoutManager
oweRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
}
最后这里是适配器(不完整)
public class MyViewHolder extends RecyclerView.ViewHolder
{
public TextView name;
public TextView amount;
public MyViewHolder(View view)
{
super(view);
name = (TextView)view.findViewById(R.id.name_title);
amount = (TextView)view.findViewById(R.id.amount_title);
}
}
//Inflate listRow layout
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int ViewType)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.payup_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
{
}
@Override
public int getItemCount()
{
return 0;
}
}
答案 0 :(得分:0)
是的,您只需在按钮的点击监听器中重新启动包含数据包的片段即可实现此目的。虽然这不是最好的方法。但这是最简单的方法。
答案 1 :(得分:0)
您可以使用在单击按钮时调用的接口方法,在片段中您可以简单地实现接口方法来重新绑定适配器。
答案 2 :(得分:0)
我认为您通过捆绑来获取活动数据!
现在做一件事就是为你的数据创建一个Model Model,比如,
/**
* Created by Uttam on 7/19/2016.
*/
public class Mydata {
String nameExtra;
String amountExtra;
public Mydata(String nameExtra, String amountExtra) {
this.nameExtra = nameExtra;
this.amountExtra = amountExtra;
}
public Mydata() {
}
public String getAmountExtra() {
return amountExtra;
}
public void setAmountExtra(String amountExtra) {
this.amountExtra = amountExtra;
}
public String getNameExtra() {
return nameExtra;
}
public void setNameExtra(String nameExtra) {
this.nameExtra = nameExtra;
}
}
此后,在您的片段创建列表中..
List<Mydata> mydataList = new ArrayList<>();
现在,您在获取Data
后从Bundle获取数据Bundle extras = this.getArguments();
if(extras != null)
{
String nameExtra = extras.getString("nameKey");
String amountExtra = extras.getString("amountKey");
//Just add your data in list
Mydata mydata = new Mydata(); // object of Model Class
mydata.setAmountExtra(amountExtra );
mydata.setNameExtra(nameExtra);
mydataList.add(mydata);
}
最后现在只需通过在Adapte中创建一个Setter方法将此列表传递给适配器。
myAdapter.setMyDataList(mydataList); // passing list to Adapter
在此之前,不要忘记在你的片段中注册你的适配器!
像这样创建适配器..
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Uttam on 7/19/2016.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<Mydata> myDataList;
public MyAdapter(Context context) {
this.context = context;
myDataList = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Replace with your layout
View view = LayoutInflater.from(context).inflate(R.layout.yourLayout_Name, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Set Your Data here to yout Layout Components..
// to get Amount
myDataList.get(position).getAmountExtra();
}
@Override
public int getItemCount() {
if (myDataList.size() != 0) {
// return Size of List if not empty!
return myDataList.size();
}
return 0;
}
public void setMyDataList(List<Mydata> myDataList) {
// getting list from Fragment.
this.myDataList = myDataList;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// attach you component here using findViewById //
public ViewHolder(View itemView) {
super(itemView);
// itemView.findViewById
}
}
}
所有设置希望这将帮助你很多..!