Custom ListView with images contains Custom Adapter that i have explained it in below examples:----
1:- CustomListView.java
package com.example.demo;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomListView extends ListActivity {
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
// ArrayAdapter<String> adapter;
MyPerformanceArrayAdapter adapter;
//RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter=0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new MyPerformanceArrayAdapter(this,listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
// adapter.add(listItems.get(0));
adapter.notifyDataSetChanged();
}
public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> names;
public MyPerformanceArrayAdapter(Activity context, ArrayList<String> listItems) {
super(context, R.layout.list_layout, listItems);
this.context = context;
this.names = listItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.textview);
viewHolder.image = (CheckBox) rowView
.findViewById(R.id.checkbox);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names.get(position);
holder.text.setText(s);
return rowView;
}
}
class ViewHolder {
public TextView text;
public CheckBox image;
}
}
1:- CustomListView.java
package com.example.demo;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomListView extends ListActivity {
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
// ArrayAdapter<String> adapter;
MyPerformanceArrayAdapter adapter;
//RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter=0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new MyPerformanceArrayAdapter(this,listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
// adapter.add(listItems.get(0));
adapter.notifyDataSetChanged();
}
public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> names;
public MyPerformanceArrayAdapter(Activity context, ArrayList<String> listItems) {
super(context, R.layout.list_layout, listItems);
this.context = context;
this.names = listItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.textview);
viewHolder.image = (CheckBox) rowView
.findViewById(R.id.checkbox);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names.get(position);
holder.text.setText(s);
return rowView;
}
}
class ViewHolder {
public TextView text;
public CheckBox image;
}
}
------------------------------------------------------------------------------------------------------------
2:- main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
----------------------------------------------------------------------------------------------------------
3:- list_layout.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="match_parent"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:id="@+id/textview"/>
</LinearLayout>
No comments:
Post a Comment