这是模型类
public class AmountCartModel {
private String testName;
private String testPrice;
public AmountCartModel() {
this.testName = testName;
this.testPrice = testPrice;
}
public String getTestName() {
return testName;
}
public void setTestName(String testName) {
this.testName = testName;
}
public String getTestPrice() {
return testPrice;
}
public void setTestPrice(String testPrice) {
this.testPrice = testPrice;
}
}
这是我的活动
public class AmountCartActivity extends AppCompatActivity
private RecyclerView recyclerView;
List<AmountCartModel> mydataList ;
private MyAdapter madapter;
Bundle extras ;
String testName="";
String testPrice="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_amount_cart);
mydataList = new ArrayList<>();
/*
* Getting Values From BUNDLE
* */
extras = getIntent().getExtras();
if (extras != null) {
testName = extras.getString("test_name");
testPrice = extras.getString("test_price");
//Just add your data in list
AmountCartModel mydata = new AmountCartModel(); // object of Model Class
mydata.setTestName(testName );
mydata.setTestPrice(testPrice);
mydataList.add(mydata);
}
madapter=new MyAdapter(mydataList);
madapter.setMyDataList(mydataList);
recyclerView = (RecyclerView)findViewById(R.id.recyler_amount_cart);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(madapter);
}
这是我的回收站适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<AmountCartModel> 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(parent.getContext()).inflate(R.layout.amount_cart_row, 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
holder.testName.setText(myDataList.get(position).getTestName());
holder.testPrice.setText(myDataList.get(position).getTestPrice());
}
@Override
public int getItemCount() {
return myDataList.size();
}
public void setMyDataList(List<AmountCartModel> myDataList) {
// getting list from Fragment.
this.myDataList = myDataList;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView testName,testPrice;
public ViewHolder(View itemView) {
super(itemView);
// itemView.findViewById
testName=itemView.findViewById(R.id.test_name_one);
testPrice=itemView.findViewById(R.id.price_name);
}
}
}
}
Logcat
E / Android运行时:致命异常:主要 流程:com.snpinfotech.snp.aoplapp,PID:11874 java.lang.NullPointerException 在com.snpinfotech.snp.aoplapp.aopl.amountCartContent.AmountCartActivity $ MyAdapter.onBindViewHolder(AmountCartActivity.java:374) 在com.snpinfotech.snp.aoplapp.aopl.amountCartContent.AmountCartActivity $ MyAdapter.onBindViewHolder(AmountCartActivity.java:343) 在android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6482) 在android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6515) 在android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458) 在android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724) 在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5563) 在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5559) 在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2229) 在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556) 在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516) 在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608) 在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693) 在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410) 在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962) 在android.view.View.layout(View.java:15125) 在android.view.ViewGroup.layout(ViewGroup.java:4862) 在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160) 在android.view.View.layout(View.java:15125) 在android.view.ViewGroup.layout(ViewGroup.java:4862) 在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1160) 在android.view.View.layout(View.java:15125) 在android.view.ViewGroup.layout(ViewGroup.java:4862) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:515) 在android.widget.FrameLayout.onLayout(FrameLayout.java:450) 在android.view.View.layout(View.java:15125) 在android.view.ViewGroup.layout(ViewGroup.java:4862) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1651) 在android.view.View.layout(View.java:15125) 在android.view.ViewGroup.layout(ViewGroup.java:4862) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:515) 在android.widget.FrameLayout.onLayout(FrameLayout.java:450) 在android.view.View.layout(View.java:15125) 在android.view.ViewGroup.layout(ViewGroup.java:4862) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1651) 在android.view.View.layout(View.java:15125) 在android.view.ViewGroup.layout(ViewGroup.java:4862) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:515) 在android.widget.FrameLayout.onLayout(FrameLayout.java:450) 在android.view.View.layout(View.java:15125) 在android.view.ViewGroup.layout(ViewGroup.java:4862) 在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2317) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2023) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6223) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:788) 在android.view.Choreographer.doCallbacks(Choreographer.java:591) 在android.view.Choreographer.doFrame(Choreographer.java:560) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:774) 在android.os.Handler.handleCallback(Handler.java:808) 在android.os.Handler.dispatchMessage(Handler.java:103) 在android.os.Looper.loop(Looper.java:193) 在Android系统上。
在这里,我正在从“上一个活动回收者”视图中获取值,并且我试图通过此活动中的捆绑包显示它。 问题是 当我用这种方法做的时候,活动并没有显示我什么东西,而且我在捆绑包中得到了价值,我没有得到什么问题。任何帮助将不胜感激。谢谢
答案 0 :(得分:0)
您显然忘记了设置适配器。 更改代码
UserForm
到
madapter=new MyAdapter(mydataList);
madapter.setMyDataList(mydataList);
recyclerView = (RecyclerView)findViewById(R.id.recyler_amount_cart);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
编辑2:将onBindViewHolder更改为:
madapter=new MyAdapter(mydataList);
madapter.setMyDataList(mydataList);
recyclerView = (RecyclerView)findViewById(R.id.recyler_amount_cart);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(madapter);
编辑:作为一个附加的想法,下面的代码与getItemCount()函数的作用相同。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.testName.setText(myDataList.get(position).getTestName());
holder.testPrice.setText(myDataList.get(position).getTestPrice());
}