Tuesday, November 22, 2016

Listview example using BaseAdapter in Android

Listview Exaple

ListView only displays a list of scrollable items and the list items are automatically inserted to the list using an Adapter. 

To make custom android ListView you need to create two XML layout files.One layout for ListView and another layout to hold the raw items of ListView, it contains one ImageView and one TextView inside LinearLayout. You can add more views according to your need.

Below is my activity_main.xml.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lvFirst"
/>
</RelativeLayout>



And for custom listview layout we need to create its layout file. Below is my_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 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"/>

</LinearLayout>


Here is my MainActivity.java file.




public class MainActivity extends Activity {

private ArrayList<MyModel> alName;
private ListView lvFirst;
private MyAdapter adapter;

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

getId();
loadArraylist();
adapter = new MyAdapter(MainActivity.this,alName);
lvFirst.setAdapter(adapter);
adapter.notifyDataSetChanged();

}

private void loadArraylist()
{
alName = new ArrayList<MyModel>();

alName.add(new MyModel("Testing1"));
alName.add(new MyModel("Testing2"));
alName.add(new MyModel("Testing3"));
alName.add(new MyModel("Testing4"));
alName.add(new MyModel("Testing5"));

}

private void getId()
{
lvFirst = (ListView)findViewById(R.id.lvFirst);
}
}

Here is my Model class.




public class MyModel implements Serializable {

private String strName;

public MyModel(String strName)
{
this.strName = strName;
}

public String getStrName()
{
return strName;
}

public void setStrName(String strName)
{
this.strName = strName;
}
}

Here is my listview adapter file

public class MyAdapter extends BaseAdapter {

private Context context;
private ArrayList<MyModel> alName;
private LayoutInflater inflater;

public MyAdapter(MainActivity mainActivity, ArrayList<MyModel> alName)
{
context = mainActivity;
this.alName = alName;
}

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

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

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

// View holder is used to avoid findView by id and increase 15 % faster performance.

public class Holder {
TextView tvName;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View myView = null;
try
{
Holder holder;
myView = convertView;

if (myView == null)
{
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.my_layout, null);

//               if pass null in attach to root then app won't be crash and inflate custom layout if we use true
//               then parent view will be use so we can not inflate our custom laoyout.


//                http://stackoverflow.com/questions/21212372/exception-in-adapter-whe  n-trying-to-inflate-layout (for more information.)

holder = new Holder();
holder.tvName = (TextView) myView.findViewById(R.id.tvName);
myView.setTag(holder);

}
else
{
holder = (Holder) myView.getTag();
}

holder.tvName.setText(alName.get(position).getStrName());

} catch (Exception e)
{
e.printStackTrace();
}

return myView;
}
}

No comments:

Post a Comment