Main (Activity)
public class Main extends Activity
{
private CustomAdapter adapter;
public ListView mListView = null;
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);
mListView = (ListView) findViewById(R.id.list);
setListData();
adapter = new CustomAdapter(this, CustomListViewValuesArr);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
/****** Function to set data in ArrayList *************/
public void setListData()
{
CustomListViewValuesArr.add(new ListModel("No1"));
CustomListViewValuesArr.add(new ListModel("No2"));//same
CustomListViewValuesArr.add(new ListModel("No3"));
CustomListViewValuesArr.add(new ListModel("No4"));
CustomListViewValuesArr.add(new ListModel("No2"));//same
}
}
listview_layout.xml (Activity xml)
<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" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
CustomAdapter (Adapter)
public class CustomAdapter extends BaseAdapter
{
/*********** Declare Used Variables *********/
private Activity mActivity;
private ArrayList<ListModel> mList;
private static LayoutInflater mInflater = null;
private ListModel tempValues = null;
private int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d) {
/********** Take passed values **********/
mActivity = a;
mList = d;
/*********** Layout inflator to call external xml layout () ***********/
mInflater = ( LayoutInflater ) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(mList.size()<=0)
return 1;
return mList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder
{
public TextView text;
}
/****** Depends upon mList size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = mInflater.inflate(R.layout.tabitem, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.textView);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(mList.size() <= 0)
{
holder.text.setText("No Data");
}
else
{
tempValues = null;
tempValues = ( ListModel ) mList.get( position );
holder.text.setText( tempValues.getName() );
}
if(isDuplicate(tempValues.getName()))// this Method to check duplicate.
{
// Set a background color for ListView regular row/item
vi.setBackgroundColor(Color.parseColor("#FFB6B546"));
}
else
{
// Set the background color for alternate row/item
vi.setBackgroundColor(Color.parseColor("#FFCCCB4C"));
}
return vi;
}
private boolean isDuplicate(String item)
{
int count = 0;
for (int i = 0; i < mList.size(); i++)
{
ListModel listModel = mList.get(i);
if(item.equals(listModel.getName()))
count++;
}
if(count > 1)
return true;
else
return false;
}
}
ListModel (Model Class)
public class ListModel {
private String name ="";
/*********** Set Methods ******************/
public ListModel(String name)
{
this.name = name;
}
public void setName(String CompanyName)
{
this.name = CompanyName;
}
/*********** Get Methods ****************/
public String getName()
{
return this.name;
}
}
tabitem (ListView Adapter 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:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/textView" />
</LinearLayout>