【问题标题】:Displaying Bundle values to recycler view在回收站视图中显示 Bundle 值
【发布时间】:2019-02-24 20:27:12
【问题描述】:

这是模型类

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/AndroidRuntime: 致命异常: main 进程: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) 在安卓。

在这里,我正在从以前的 Activity Recycler 视图中获取值,并且我正在尝试通过 Bundle 在此 Activity 中显示它。
问题是 当我通过这种方法执行此操作时,活动显示我什么都没有,我在我的捆绑包中获得了价值,我没有得到它的问题是什么。任何帮助将不胜感激。谢谢

【问题讨论】:

  • 忘记设置适配器
  • 如果您在 SO 上花费几分钟,您就会找到解决方案。
  • 已经花了几个小时仍然无法解决..

标签: android android-recyclerview bundle


【解决方案1】:

显然你忘了设置适配器。 更改您的代码

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);

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);

编辑 2:将您的 onBindViewHolder 更改为:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.testName.setText(myDataList.get(position).getTestName());
    holder.testPrice.setText(myDataList.get(position).getTestPrice());
}

编辑:作为一个额外的想法,下面的代码与您的 getItemCount() 函数的作用相同。

@Override
public int getItemCount() {
    return myDataList.size();
}

【讨论】:

  • 已经用你的代码编辑了我的代码,现在它只显示我的 test_list_row xml 文件 @gml
  • 你也应该编辑你的 onBindViewHolder。等待编辑...@Sujal
  • holder.testName.setText(myDataList.get(position).getTestName()); holder.testPrice.setText(myDataList.get(position).getTestPrice());在这两行中给我 java.lang.NullPointerException
  • 您确定 R.id.test_name_one 和 R.id.price_name 包含在 R.layout.amount_cart_row 中吗?
  • 很抱歉您说 test_list_row.xml 在您的第一条评论中显示。你检查了 amount_cart_row 或 test_list_row 吗?
猜你喜欢
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2021-10-01
相关资源
最近更新 更多