I have a list view and I am trying to set header for it so that it looks like a table header. Here is the XML file of my list view row:
PS: I have removed some codes to shorten the question
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
And my XML file of my header list view:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Category" />
And the class where I populate the list view:
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
LinearLayout listHeaderView = (LinearLayout)inflater.inflate(
R.layout.trans_header_layout, null);
listview.addHeaderView(listHeaderView);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _translist.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.trans_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_dcat.setText(_translist.get(position)
.getCategory().trim());
return convertView;
}
}
However, by using these codes, here is the output that I've gotten:
The header does not match with the column below it. I wonder is there any alternate way to add header to list view so that it looks like a table header.
Thanks in advance.
EDIT
My transaction_rec.XML where the listview is located:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date" />
<TextView
android:id="#+id/txtDisplayDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category" />
<TextView
android:id="#+id/txtDisplayCat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
And my transactionRec.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction_rec);
context = this;
listview = (ListView) findViewById(R.id.listview);
//Code to get data from database
}
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _translist.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.transaction_rec,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_dcat.setText(_translist.get(position)
.getCategory().trim());
return convertView;
}
}
}
And the output I've gotten now:
try this...
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
with all text views.
Related
I'm trying to make a grid view to dynamically load images and data from a web server, but if i try to use Glide to load the image it messes up my layout, im a newbie on android programming so please be patient
the correct layout is this:
but if i enable Glide it shows like this:
As you can see there is a wide gap at the bottom that shouldn't be there
Here is my Grid Adapter:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
ArrayList<HashMap<String, String>> hotelsList;
TextView hotelNameTv;
TextView hotelDistanceTv;
ImageView hotelImage;
public GridViewAdapter(Context c, ArrayList<HashMap<String, String>> hotelsList ) {
mContext = c;
this.hotelsList = hotelsList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return hotelsList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return hotelsList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.hotel_item, null);
hotelNameTv = (TextView) grid.findViewById(R.id.hotelName);
hotelDistanceTv = (TextView) grid.findViewById(R.id.hotelDistance);
hotelImage = (ImageView) grid.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelNameTv.setText(hotel.get("name"));
hotelDistanceTv.setText(hotel.get("distancemsg"));
/*
Glide.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
*/
} else {
grid = (View) convertView;
}
return grid;
}
}
Here is my Activity Main resource File:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#color/windowBackground">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<GridView
android:id="#+id/hotelsGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="#dimen/small_margin"
android:horizontalSpacing="#dimen/small_margin"
android:stretchMode="columnWidth"
android:numColumns="2"
android:padding="10dp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
And my item resource file
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:elevation="30dp"
>
<TextView
android:text="Hotel Name"
android:layout_width="match_parent"
android:layout_height="56sp"
android:gravity="center_vertical"
android:id="#+id/hotelName"
android:textAppearance="#android:style/TextAppearance.Material.Large"
android:textColor="#color/colorPrimary"
android:paddingLeft="5sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/hotel"
android:id="#+id/hotelImage"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:cropToPadding="false"
android:layout_below="#+id/hotelName"
android:layout_alignParentStart="true" />
<TextView
android:text="Hotel Distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/hotelDistance"
android:background="#222222"
android:textAppearance="#android:style/TextAppearance.Material.Inverse"
android:layout_below="#+id/hotelImage"
android:layout_alignParentStart="true"
android:padding="5sp"
android:textColor="#color/gold" />
</RelativeLayout>
</RelativeLayout>
Thanks for your help!!!
Not sure what i did... first i changed my get view to this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.hotel_item, null);
}
final TextView hotelName = (TextView)convertView.findViewById(R.id.hotelName);
final TextView hotelDistance = (TextView)convertView.findViewById(R.id.hotelDistance);
final ImageView hotelImage = (ImageView)convertView.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelName.setText(hotel.get("name"));
hotelDistance.setText(hotel.get("distancemsg"));
Glide.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
return convertView;
}
And it didn't worked...
But then i changed from glide to picasso, it worked perfectly...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.hotel_item, null);
}
final TextView hotelName = (TextView)convertView.findViewById(R.id.hotelName);
final TextView hotelDistance = (TextView)convertView.findViewById(R.id.hotelDistance);
final ImageView hotelImage = (ImageView)convertView.findViewById(R.id.hotelImage);
HashMap<String, String> hotel=hotelsList.get(position);
hotelName.setText(hotel.get("name"));
hotelDistance.setText(hotel.get("distancemsg"));
Picasso.with(mContext)
.load("http://192.168.0.6/hoteles360ws/img/kron02.jpg")
.into(hotelImage);
return convertView;
}
I'll be very grateful if someone explains me what happened...
You can check this link below. Gridview in Android is quite studpid. (I suggest you use Recyclerview with GridLayoutManager)
How to have a GridView that adapts its height when items are added
I request you just see this video you will know what is the issue
https://www.youtube.com/watch?v=gsZSMRPEZSI&feature=youtu.be
Gridview scrolls and Gone when I scroll it's item
This issue is not occurring when there is limited no. of items in GridView or when we scroll slowly
I have taken Custom GridView, in that I am loading images and text from server
below is the code
GridView's XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background" >
<TextView
android:id="#+id/edtTxtProfileQuotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/small_margin"
android:background="#drawable/blue_notification_shape"
android:drawableLeft="#drawable/blue_notification_icon"
android:drawablePadding="#dimen/small_margin"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/light_violet" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="#drawable/teambg" />
<GridView
android:id="#+id/grdGrupOfUnite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomBar"
android:layout_below="#id/edtTxtProfileQuotes"
android:numColumns="3" >
</GridView>
</RelativeLayout>
Adapter's xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical" >
<com.example.achessapp.widget.CircularImageView
android:id="#+id/ivUniteGrpMemIcon"
android:layout_width="#dimen/icon_size"
android:layout_height="#dimen/icon_size"
android:layout_centerHorizontal="true"
android:src="#android:color/white" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ivUniteGrpMemIcon"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/extra_small_margin"
android:layout_marginBottom="#dimen/small_margin"
android:gravity="center_horizontal"
android:textColor="#android:color/black" />
</RelativeLayout>
in Activity File
adapter = new BondMemberAdapter(mActivity, list_members_bean);
mGridMembers.setAdapter(adapter);
In Adapter file
public class BondMemberAdapter extends BaseAdapter {
LayoutInflater mInflator;
Activity mActivity;
private ArrayList<MembersBean> list_members_bean;
Typeface tf = null;
public BondMemberAdapter(Activity activity, ArrayList<MembersBean> list) {
mActivity = activity;
mInflator = LayoutInflater.from(mActivity);
list_members_bean = list;
tf = Typeface.createFromAsset(
mActivity.getAssets(), Constants.FONT_TYPE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list_members_bean.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
MembersBean bean = list_members_bean.get(position);
if (convertView == null) {
convertView = mInflator.inflate(R.layout.unite_grup_member_item,
null);
viewHolder = new ViewHolder();
viewHolder.ivUser = (CircularImageView) convertView
.findViewById(R.id.ivUniteGrpMemIcon);
viewHolder.tvName = (TextView) convertView
.findViewById(R.id.tvName);
viewHolder.tvName.setTypeface(tf);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String text = "";
text = bean.getScreen_name();
//viewHolder.ivUser.setImageResource(R.drawable.unite_item);
if (bean.getThumbnail_url() != null) {
if (bean.getThumbnail_url().equals("null")) {
ImageLoader.getInstance().displayImage(
"drawable://" + R.drawable.profile_placeholder,
viewHolder.ivUser);
} else {
ImageLoader.getInstance().displayImage(bean.getThumbnail_url(),
viewHolder.ivUser);
}
}
else {
ImageLoader.getInstance().displayImage(
"drawable://" + R.drawable.profile_placeholder,
viewHolder.ivUser);
}
viewHolder.tvName.setText(text);
return convertView;
}
static class ViewHolder {
CircularImageView ivUser;
TextView tvName;
int position;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list_members_bean.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
I have a fragment that contains a GridView
<?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" >
<GridView
android:padding="20dp"
android:id="#+id/sub_category_gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="20dp"
android:numColumns="4"
android:verticalSpacing="10dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:fastScrollEnabled="true"
tools:listitem="#layout/sub_category_view" />
</LinearLayout>
Every child of this GridView contains one TextView and one ImageView
<?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="vertical"
android:gravity="center"
android:id="#+id/sub_category_view">
<ir.motorel.carbuyinstruduction.SquareLinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/button">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/picture1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_margin="10dp"
android:id="#+id/sub_category_img"/>
</ir.motorel.carbuyinstruduction.SquareLinearLayout>
<TextView
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center"
android:textColor="#C36518"
android:id="#+id/sub_category_name"/>
</LinearLayout>
When I fill GridView with Texts and Images, it's become very Laggy and slow when I scrolling. This is My adapter Class:
public class CustomGrid extends BaseAdapter{
private Context mContext;
public CustomGrid(Context c) {
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return subCatTitle.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.sub_category_view, parent, false);
viewHolder = new ViewHolder();
viewHolder.subCatView = (LinearLayout) convertView.findViewById(R.id.sub_category_view);
viewHolder.subCatImg = (ImageView) convertView.findViewById(R.id.sub_category_img);
viewHolder.subCatTitle = (TextView) convertView.findViewById(R.id.sub_category_name);
//subCatImg.setImageResource(imgIDs.getResourceId(position, -1));
convertView.setTag(viewHolder);
}
else{
//imgIDs.recycle();
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.subCatTitle.setText(subCatTitle[position]);
return convertView;
}
}
public class ViewHolder{
public TextView subCatTitle;
public ImageView subCatImg;
public LinearLayout subCatView;
}
How can i optimize it to scroll more smoothly?
PS. My images are in drawable folder and their format is PNG.
I had a ImageView in my Activity's Header that I set a really big image to it. It was killing performance. There is no problem in GridView.
im just trying to create a listview using a simple adapter. the setadapter is not working. this is my code. my setcontentview is before this block of code and this is in oncreate.
ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("CType", "Alarm");
map.put("RType", "Once");
map.put("hour", "1");
map.put("minute", "1");
map.put("second", "30");
arraylist.add(map);
String[] stringarray= new String[] {"CType", "RType", "hour","minute","second"};
int[] intarray = new int[] {R.id.clocktextviewalarmtimer,R.id.clocktextviewrepeatonce,
R.id.clocktextviewhours,R.id.clocktextviewminutes,R.id.clocktextviewseconds};
ListView clocklistview = (ListView)findViewById(R.id.clocklistview);
SimpleAdapter adapter = new SimpleAdapter(this,arraylist,R.layout.list_clock,stringarray,intarray);
clocklistview.setAdapter(adapter);
this is the xml that contains my listview
<?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"
>
<ListView
android:id="#+id/clocklistview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView></LinearLayout>
this is for my list_clock.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"
android:weightSum="3">
<TextView
android:id="#+id/clocktextviewalarmtimer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/clocktextviewrepeatonce"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/clocktextviewhours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/clocktextviewminutes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/clocktextviewseconds"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Here's an example of a custom adapter. Try this ...
public class ClockListAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> _arrayList;
ClockListAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) {
this._arrayList = arrayList;
}
#Override
public int getCount() {
return (_arrayList != null) ? _arrayList.size() : 0;
}
#Override
public Object getItem(int position) {
return (_arrayList != null) ? _arrayList.get(position) : null;
}
#Override
public long getItemId(int position) {
return (_arrayList != null) ? _arrayList.indexOf(_arrayList.get(position)) : 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
// REPLACE WITH YOUR LAYOUT FILE vvvvvvvvvv
convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder._textView.setText("set some text here");
return convertView;
}
public class ViewHolder {
// ADD YOUR VIEW(S) vvvvvvvvv
TextView _textView;
ViewHolder(View v) {
// REPLACE WITH YOUR TEXT vvvvvvvvv
_textView = (TextView) v.findViewById(R.id.textView1);
}
}
}
I have to apply pagination concept on ListView my list view contains data parsed from web service. below is code given that how I have displayed data in list view as below.
try {
ArrayList<HashMap<String, String>> arl (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.."+arl);
lv1 = (ListView) findViewById(R.id.lstlodgingresult);
adapter = new SimpleAdapter(this, arl, R.layout.custom_row_view,
new String[] { "Srno", "Names", "URL", "Address1", "Address2", "Telephone", "Category", "PetH",
"PetInfo" }, new int[] { R.id.txtSrno,R.id.txtname, R.id.txturl, R.id.txtaddress1, R.id.txtaddress2, R.id.txtphone, R.id.txtcategory,
R.id.txtpetpolicyH, R.id.txtpetpolicyC }
);
lv1.setScrollbarFadingEnabled(false);
lv1.refreshDrawableState();
lv1.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
you just need to add a Footer View in the Listyou created. Then for the footer view (might be button/image/text) set a ClickListener for that and in Listener add the items into your list and again refresh the activity. I am adding a little tutorial that will help you in this.
I used the following Method for Pagination:
The List Class:
public class customListView extends Activity implements OnClickListener{
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
Context context;
public EfficientAdapter(Context context) {
this.context = context;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return add_Names.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listcontent, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.txt1);
holder.text2 = (TextView) convertView
.findViewById(R.id.txt2);
holder.text3 = (TextView) convertView
.findViewById(R.id.txt3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(add_Names.get(position).toString());
holder.text2.setText(location.get(position).toString());
holder.text3.setText(details.get(position).toString());
return convertView;
}
static class ViewHolder {
TextView text;
TextView text2;
TextView text3;
}
}//end of efficient Adapter Class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
adapter = new EfficientAdapter(this);
l1 = (ListView) findViewById(R.id.ListView01);
View footerView =
((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_footer, null, false);
l1.addFooterView(footerView);
l1.setAdapter(adapter);
mLayout = (LinearLayout) footerView.findViewById(R.id.footer_layout);
more = (Button) footerView.findViewById(R.id.moreButton);
more.setOnClickListener(this);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(), "You clciked "+add_Names.get(arg2).toString(), Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.moreButton:
//Your code to add some more data into list and then call the following to refresh your lits
adapter.notifyDataSetChanged();
break;
}//end of switch
}//end of onClick
}//end of Custom List view class
layout_footerview.xml:(you can add whatever you link in the footer for the list. I used button you can use Text or image or whatever you want)
<?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:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<Button
android:text="Get more.."
android:id="#+id/moreButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold">
</Button>
</LinearLayout>
</LinearLayout>
listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
</RelativeLayout>
list-content.xml:(modify as u like to be your list row)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt1" android:layout_toRightOf="#+id/image1"
android:text="Test Description" android:textSize="15dip" android:textStyle="bold">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt2" android:layout_below="#+id/txt1" android:layout_toRightOf="#+id/image1"
android:text="Address" android:textSize="10dip"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt3" android:layout_below="#+id/txt2" android:layout_toRightOf="#+id/image1"
android:text="Details" android:textSize="10dip" ></TextView>
</RelativeLayout>
I Hop this will definetly help you.!
Mark this as true and UpVote; if this helps you.
Thanks
sHaH..