user2121 user2121 - 2 months ago 8
Android Question

items of the listview are not showing

I am creating a cutomized listView. the model of each item in the listView is as shown in the "model_view" layout.
I set the data to the adapter as shown in "set adapter" below.
the cutomized adapter is as shown in "CutomizedAdapter"

when i run the App, there are no items appearing on the screen

why the items of the listview are not showing

model_view:

<?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">

<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="Name"/>

<TextView
android:id="@+id/tvAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="Address: "/>

<TextView
android:id="@+id/tvGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="gender: "/>
</LinearLayout>


set adapter:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.mListView = (ListView) findViewById(R.id.listview);
this.mModelList = new ArrayList<RowModel>();

this.mModelList.add(new RowModel("xx", "Mümchen", "M"));
this.mModelList.add(new RowModel("xx", "Karlsruhe", "M"));
this.mModelList.add(new RowModel("xx", "Mümchen", "F"));
this.mModelList.add(new RowModel("xx", "Karlsruhe", "F"));
this.mModelList.add(new RowModel("xx", "Alexandria", "M"));
this.mModelList.add(new RowModel("xx", "Cairo", "F"));

CustomizedAdapter adapter = new CustomizedAdapter(this, R.layout.model_view, this.mModelList);
this.mListView.setAdapter(adapter);


CutomizedAdapter:

public class CustomizedAdapter extends BaseAdapter {

private Context mCxt = null;
private int mRowModelLayout;
private ArrayList<RowModel> mRowsList = null;
private static LayoutInflater inflater=null;

public CustomizedAdapter(Context cxt, int rowModelLayout, ArrayList<RowModel> rowsList) {
this.mCxt = cxt;
this.mRowModelLayout = rowModelLayout;
this.mRowsList = rowsList;
}

@Override
public int getCount() {
return 0;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

public static class ViewHolder{
public TextView tvName;
public TextView tvAddress;
public TextView tvGender;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

final int pos = position;
ViewHolder holder = new ViewHolder();
View rowView;

convertView = inflater.inflate(R.layout.model_view, null);
holder.tvName=(TextView) convertView.findViewById(R.id.tvName);
holder.tvAddress=(TextView) convertView.findViewById(R.id.tvAddress);
holder.tvGender=(TextView) convertView.findViewById(R.id.tvGender);

convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mCxt, "You Clicked "+mRowsList.get(pos).getName(), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}

Answer

Why you are not filling below methods?

@Override
public int getCount() {
    return mRowsList.size();
}

@Override
public Object getItem(int position) {
    return mRowsList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

Also, you are not initializing your inflater

So, initialize it in your constructor like

public CustomizedAdapter(Context cxt, int rowModelLayout, ArrayList<RowModel> rowsList) {
    this.mCxt = cxt;
    this.mRowModelLayout = rowModelLayout;
    this.mRowsList = rowsList;
    inflater = (LayoutInflater) cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Comments