Issue
In my MainActivity I use a RecyclerView to fill an activity with images and names, but when I run the app the activity is empty. An adapter is called that is filled with and array of data objects that contain an "id". The adapter inflates the item_view and uses a ViewHolder that holds a textview and acts as an onClickListener.
Any ideas as to why it remains empty and how I may fill it?
MainActivity:
package com.com.com;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mDataAdapter;
private RecyclerView.LayoutManager mDataLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(true);
mMatchesLayoutManager = new LinearLayoutManager(MainActivity.this);
mRecyclerView.setLayoutManager(mDataLayoutManager);
mDataAdapter = new DataAdapter(getDataSetData(), MainActivity.this);
mRecyclerView.setAdapter(mDataAdapter);
for (int i = 0; i < 10; i++) {
DataObject obj = new DataObject(Integer.toString(i));
resultsData.add(obj);
}
mDataAdapter.notifyDataSetChanged();
}
private ArrayList<DataObject> resultsData = new ArrayList<DataObject>();
private List<DataObject> getDataSetData() {
return resultsData;
}
}
DataAdapter:
package com.com.com;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class DataAdapter extends RecyclerView.Adapter<DataViewHolders>{
private List<DataObject> dataList;
private Context context;
public DataAdapter(List<DataObject> dataList, Context context) {
this.dataList = dataList;
this.context = context;
}
@Override
public DataViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_data, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutView.setLayoutParams(lp);
DataViewHolders rcv = new DataViewHolders((layoutView));
return rcv;
}
@Override
public void onBindViewHolder(DataViewHolders holder, int position) {
holder.mDataId.setText(dataList.get(position).getId());
}
@Override
public int getItemCount() {
return 0;
}
}
DataObject:
package com.com.com;
public class DataObject {
private String Id;
public DataObject (String Id) {
this.Id= Id;
}
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id= Id;
}
}
DataViewHolders:
package com.com.com;
import android.view.View;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
public class DatasViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mDataId;
public DataViewHolders(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mDataId = (TextView) itemView.findViewById(R.id.Dataid);
}
@Override
public void onClick(View v) {
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclerView"
android:scrollbars="vertical">
</androidx.recyclerview.widget.RecyclerView>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
item_data:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="20sp">
<ImageView
android:layout_width="75sp"
android:layout_height="75sp"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Data ID"
android:layout_gravity="center"
android:id="@+id/Dataid" />
</LinearLayout>
Solution
@Override
public int getItemCount() {
return 0;
}
You return wrong value here, it must be the size of list.
Answered By - dinhlam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.