Populate Data into RecyclerView

2019-08-09 09:51发布

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

    }
}

2条回答
疯言疯语
2楼-- · 2019-08-09 10:17

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();
}
查看更多
疯言疯语
3楼-- · 2019-08-09 10:30

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);
查看更多
登录 后发表回答