Reputation:
I am beginner in android and I want to learn working with ListView. I have searched for that and tried to make a List View.
But unfortunately It has force close at the beginning.
here is activity_main.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/List"/>
here is list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/groupTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dp" />
</LinearLayout>
here is CustomListView.java:
public class CustomListView extends BaseAdapter{
private ArrayList<ItemClass> arr = new ArrayList<ItemClass>();
public CustomListView( ArrayList<ItemClass> arr) {
this.arr = arr;
}
@Override
public int getCount() {
return arr.size();
}
@Override
public Object getItem(int index) {
return getItem(index);
}
@Override
public long getItemId(int index) {
return index;
}
@Override
public View getView(int index, View view, ViewGroup parent) {
if(view == null)
{
LayoutInflater inflator = LayoutInflater.from(parent.getContext());
view = inflator.inflate(R.layout.list_row, parent, false);
}
ItemClass child = arr.get(index);
TextView Text = (TextView) view.findViewById(R.id.groupTitle);
Text.setText(child.getName());
return null;
}
}
here is main class:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ItemClass> arr = new ArrayList<ItemClass>();
arr.add(new ItemClass("Sh " , 1));
CustomListView adaptor;
adaptor = new CustomListView(arr);
ListView listView = (ListView) findViewById(R.id.List);
listView.setAdapter(adaptor);
}
}
and finally here is ItemClass.java:
public class ItemClass {
//private Context context;
String name;
private int rate;
public ItemClass(String name , int rate ) {
this.setName(name);
this.setRate(rate);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setRate(int rate) {
this.rate = rate;
}
public int getRate() {
return this.rate;
}
}
I have commented listView.setAdapter(adaptor);
then it had no force close.
can anyone help me??
Thanks in advance for your attention
Upvotes: 0
Views: 87
Reputation: 23638
You need to change your getItem
method in your adapter class as below :
@Override
public Object getItem(int index) {
return arr.get(index);
}
Also change your getView
method to return the custom view besides null as below:
@Override
public View getView(int index, View view, ViewGroup parent) {
if(view == null)
{
LayoutInflater inflator = LayoutInflater.from(parent.getContext());
view = inflator.inflate(R.layout.list_row, parent, false);
}
//.............................
return view;
}
Upvotes: 0
Reputation: 47817
First you correct this you should return a view
return view;
instead of
return null;
in getView(....)
method in your Adapter
Upvotes: 3
Reputation: 9023
public class ListViewAdapter extends BaseAdapter{
private ArrayList<String> listname;
private ArrayList<Integer> listimg;
private ArrayList<String> listtype;
private Activity activity;
public ListViewAdapter(Activity act,ArrayList<String> liststr,ArrayList<Integer> listint,ArrayList<String> list_type)
{
super();
this.listname=new ArrayList<String>();
this.listname=liststr;
this.listimg=listint;
this.listtype=list_type;
this.activity=act;
System.out.println("this is contry name "+this.listname);
System.out.println("this is img name "+this.listimg);
System.out.println("this is type "+this.listtype);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println("len " + listname.size());
return listname.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listname.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder {
public ImageView imgViewFlag;
public TextView txtViewTitle;
public TextView txtViewType;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.notes_listlayout, null);
view.txtViewTitle = (TextView) convertView
.findViewById(R.id.note_layout_name);
//view.txtViewTitle.setText("NO Notes Available.");
view.txtViewType= (TextView) convertView
.findViewById(R.id.note_layout_type);
view.txtViewType.setTypeface(Typeface.createFromAsset(parent
.getContext().getAssets(), "fonts/DroidSerif.ttf"));
view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
.getContext().getAssets(), "fonts/DroidSerif.ttf"));
// view.txtViewTitle.setTextColor(Color.BLACK);
//view.txtViewType.setTextColor(Color.GRAY);
/*TextView emptyView = (TextView)findViewById(R.id.emptyTv);
view.setEmptyView(yourEmptyView);*/
view.imgViewFlag = (ImageView) convertView
.findViewById(R.id.note_layout_img);
// view.imgViewFlag.setBackgroundResource(R.drawable.view_default);
convertView.setTag(view);
}
else {
view = (ViewHolder) convertView.getTag();
}
System.gc();
try {
view.txtViewTitle.setText(listname.get(position));
view.txtViewType.setText(listtype.get(position));
view.imgViewFlag.setImageResource(listimg.get(position));
} catch (Exception e) {
//System.out.println("this is error " + e.getMessage());
}
return convertView;
}
}
to implement it
ListViewAdapter mAdapter;
mAdapter = new ListViewAdapter(this, listCountry, listFlag,
listfoodtype);
lv_note = (ListView) findViewById(R.id.lv_note);
// gridView.setBackgroundColor(Color.WHITE);
lv_note.setAdapter(mAdapter);
Upvotes: 0