Android listview is view we can use for to set your data into vertically fashion one by one item element with customizable xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.androidsimplifycodes.example.MainActivity">
<ListView
android:id="@+id/lvList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
This xml file bind with adapter and adapter dynamically set data into this view for example in our case TextView. In sort number of copy will be generated into run time memory when you call adapter.
list_item.xml
<?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="wrap_content">
<TextView
android:id="@+id/tvText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAppearance="?android:textAppearanceLarge" />
</LinearLayout>
ListModel is class use for the storage it means using new operator generate new object and using this object set data into this object but question arise how can we stored this objects? For the storing this object so may technique but here i want to use array list.
ListModel.java
package com.androidsimplifycodes.example.ListView;
public class ListModel {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
ListViewAdapter.java
package com.androidsimplifycodes.example.ListView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.androidsimplifycodes.example.R;
import java.util.ArrayList;
public class ListViewAdapter extends BaseAdapter {
private Context context;
private ArrayList list;
// passing the objects array list
public ListViewAdapter(Context context, ArrayList list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
// this condition only first time true because when adapter call that
// time your View is null so fist we bind xml file then after using ViewHolder
// bind views.
if (convertView == null) {
// bind your list_item.xml here
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);
holder = new ViewHolder(convertView);
// if you not set holder into View then each and every time fist bind xml
// then after make ViewHolder calls
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvText.setText(list.get(i).getText());
return convertView;
}
// this class use for the memory utilization for example one time your
// xml view bind with java using findViewById then stored into ViewHolder
// using setTag method. Here question arise but why required this procedure
// because at run time each and every time not bind your xml view with adapter
// it will be provide memory utilization and scrolling speed to the View.
private static class ViewHolder {
TextView tvText;
ViewHolder(View rootView) {
initView(rootView);
}
private void initView(View rootView) {
tvText = (TextView) rootView.findViewById(R.id.tvText);
}
}
}
MainActivity.java
package com.androidsimplifycodes.example;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.androidsimplifycodes.example.ListView.ListModel;
import com.androidsimplifycodes.example.ListView.ListViewAdapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
protected Context context;
protected ListView lvList;
protected ArrayList list;
protected ListModel model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
context = MainActivity.this;
initView();
list = new ArrayList<>();
model = new ListModel();
model.setText("ABC");
list.add(model);
model = new ListModel();
model.setText("CDE");
list.add(model);
model = new ListModel();
model.setText("EFG");
list.add(model);
model = new ListModel();
model.setText("GHI");
list.add(model);
model = new ListModel();
model.setText("IJK");
list.add(model);
model = new ListModel();
model.setText("KLM");
list.add(model);
lvList.setAdapter(new ListViewAdapter(this, list));
}
private void initView() {
lvList = (ListView) findViewById(R.id.lvList);
}
}
Output :