Populate Data into RecyclerView

一个人想着一个人 提交于 2019-12-11 11:16:27

问题


I would like to show JSON Object's data into RecyclerView for that I have tried this but did not get any data.

May, I know where I am doing mistake and What I have missed ?

How could I pass learning objects data from MainAdapter to LearningActivity

MainAdapter class:

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        final Value value = values.get(i);

        ........

        List<Learning> learning = value.getLearning();

        viewHolder.btnCallNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, LearningActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });

    }

LearningActivity class:

public class LearningActivity extends AppCompatActivity {

       ......

        mAdapter = new LearningAdapter(valueList, R.layout.card_learning, getApplicationContext());
        mRecyclerView.setAdapter(mAdapter);

    }
}

回答1:


Finally, I have resolved my own issue.

I found a very simple and straight solution here

Glad! worked for me

Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", learning);
intent.putExtras(bundle);



回答2:


about setting your data

private List<Learning> valueList = new ArrayList<Learning>();

This is new, empty ArrayList. You should fill it if you didn't already do it. To test you implementation, you could add 2 empty objects:

valueList.add(new Learning());
valueList.add(new Learning());

At least, the recyclerview now should contain 2 empty items.

Your data looks a bit strange, too:

You set a List named value, a List of Learning objects. In your adapter's onBindViewHolder function you have the following statement:

final Value value = values.get(i);
String stringLearning = "";
List<Learning> learning = value.getLearning();

This doesn't match your data. Value value = values.get(i); should return a > Learning object, not a list of Learning objects.

//Edit: Gnarf, you posted a whole new adapter while editing your question. Please ignore my above quoted text and next time think about what code you post before you ask ...

your adapter class

In your Adapter class, you need to implement getItemCount(). Because this method is abstract I assume you've already done it: http://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemCount%28%29

public abstract getItemCount () {
    return values.size();
}


来源:https://stackoverflow.com/questions/35355559/populate-data-into-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!