CustomAdapter.class
public class CustomAdapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] titleArray;
String[] descriptionArrayl;
public CustomAdapter(Context c,String[] memeTitles,int[] imgs,String[] descriptionArrayl) {
super(c,R.layout.row,R.id.etTitle,memeTitles);
this.context=c;
this.images = imgs;
this.titleArray = memeTitles;
this.descriptionArrayl=descriptionArrayl;
}
//Sub class of CustomAdapter
class MyViewHolder{
ImageView myImage;
TextView myTitle;
TextView myDescription;
MyViewHolder(View v){
myImage= (ImageView) v.findViewById(R.id.imageView);
myTitle = (TextView) v.findViewById(R.id.etTitle);
myDescription = (TextView) v.findViewById(R.id.etDescriptions);
}
}
//Get View Method of CustomAdapter class
public View getView(int position, View convertView, ViewGroup parent){
View row=convertView;
MyViewHolder myViewHolder =null;
if (row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row, parent, false);
myViewHolder = new MyViewHolder(row);
Log.d("List View","Creating New Row");
row.setTag(myViewHolder);
}else {
myViewHolder = (MyViewHolder) row.getTag();
Log.d("List View","Recycling Stuff");
}
myViewHolder.myImage.setImageResource(images[position]);
myViewHolder.myTitle.setText(titleArray[position]);
myViewHolder.myDescription.setText(descriptionArrayl[position]);
return row;
}
}
MainActivity.class
package com.faisal.listviewtask;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView listView;
String[] memeTitles;
String[] memeDescriptions;
CustomAdapter customAdapter;
int[] image = {R.drawable.meme1, R.drawable.meme2,
R.drawable.meme3, R.drawable.meme4,
R.drawable.meme5, R.drawable.meme6,
R.drawable.meme7, R.drawable.meme8,
R.drawable.meme9, R.drawable.meme10,
R.drawable.meme1, R.drawable.meme2,
R.drawable.meme3, R.drawable.meme4,
R.drawable.meme5, R.drawable.meme6,
R.drawable.meme7, R.drawable.meme8,
R.drawable.meme9, R.drawable.meme10};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
Resources res = getResources();
memeTitles = res.getStringArray(R.array.titles);
memeDescriptions = res.getStringArray(R.array.descriptions);
customAdapter = new CustomAdapter(this, memeTitles, image, memeDescriptions);
listView.setAdapter(customAdapter);
listView.setItemsCanFocus(false);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, "you clicked" + i, Toast.LENGTH_LONG).show();
Log.d("onitemclick Listener","called");
}
});
}
}
main_activity.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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:descendantFocusability="blocksDescendants"
tools:context="com.faisal.listviewtask.MainActivity">
<ListView
android:clickable="true"
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</ListView>
</RelativeLayout>
row layout.xml
<?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"
>
<ImageView
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/imageView"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_margin="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<TextView
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/etTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Title"
android:textStyle="bold"
android:textSize="25sp"
android:layout_alignTop="#+id/imageView"
android:layout_toRightOf="#id/imageView"
android:layout_alignParentRight="true"
/>
<TextView
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/etDescriptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:textSize="15sp"
android:layout_below="#+id/etTitle"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/imageView" />
</RelativeLayout>
This is all my code given. I added all tags like android:focusable="false", android:focusableInTouchMode="false" in row of list view and also I added android:descendantFocusability="blocksDescendants" in root layout of listview and listView.setItemsCanFocus(false); in code before the listView.setOnItemClickListener but after this onitem click listener does not work and no response.
add this in Activity [updated]
customAdapter = new CustomAdapter(this, memeTitles, image, memeDescriptions,new View.OnClickListener() {
#Override
public void onClick(View view) {
int i=(Integer)view.getTag();
Toast.makeText(MainActivity.this, "you clicked" + i, Toast.LENGTH_LONG).show();
Log.d("onitemclick Listener","called");
}
});
listView.setAdapter(customAdapter);
And add id as rootLayout in row xml
public class CustomAdapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] titleArray;
String[] descriptionArrayl;
OnClickListener onClickListener;
public CustomAdapter(Context c,String[] memeTitles,int[] imgs,String[] descriptionArrayl,OnClickListener onClickListener) {
super(c,R.layout.row,R.id.etTitle,memeTitles);
this.context=c;
this.images = imgs;
this.titleArray = memeTitles;
this.descriptionArrayl=descriptionArrayl;
this.onClickListener=onClickListener;
}
//Sub class of CustomAdapter
class MyViewHolder{
ImageView myImage;
TextView myTitle;
TextView myDescription;
RelativeLayout rootLayout;
MyViewHolder(View v){
rootLayout=(RelativeLayout)v.findViewById(R.id.rootLayout);
myImage= (ImageView) v.findViewById(R.id.imageView);
myTitle = (TextView) v.findViewById(R.id.etTitle);
myDescription = (TextView) v.findViewById(R.id.etDescriptions);
}
}
//Get View Method of CustomAdapter class
public View getView(int position, View convertView, ViewGroup parent){
View row=convertView;
MyViewHolder myViewHolder =null;
if (row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row, parent, false);
myViewHolder = new MyViewHolder(row);
Log.d("List View","Creating New Row");
row.setTag(myViewHolder);
}else {
myViewHolder = (MyViewHolder) row.getTag();
Log.d("List View","Recycling Stuff");
}
myViewHolder.myImage.setImageResource(images[position]);
myViewHolder.myTitle.setText(titleArray[position]);
myViewHolder.myDescription.setText(descriptionArrayl[position]);
rootLayout.setTag(position);
rootLayout.setOnClickListener(onClickListener);
return row;
}
}
row layout.xml
<?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"
>
<RelativeLayout
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/imageView"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_margin="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<TextView
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/etTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Title"
android:textStyle="bold"
android:textSize="25sp"
android:layout_alignTop="#+id/imageView"
android:layout_toRightOf="#id/imageView"
android:layout_alignParentRight="true"
/>
<TextView
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/etDescriptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:textSize="15sp"
android:layout_below="#+id/etTitle"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/imageView" />
</RelativeLayout>
</RelativeLayout>
if you your all code is correct and also no Exception is remain in your code But your ListView On ItemClickListener is not working then you need to use
Restart/invalidate caches option available in Android studio then Run the Listview Project then OnItemClick Listener work correctly.
Click on the File Option on top left corner of the android studio the select the invalidate caches/restart option .
Related
I made a ListView with a custom adapter with my own layout, and on this layout there is a text view, a button and three image buttons that are at first with setVisibility.GONE
I am trying to animate this button for when the user clicks on it, it changes its position from right to left to give space for the three image buttons.
The problem is, the only item in which the animation is working is the last item on the list. I want the animation to work in all of the items.
Here's the code for my adapter:
public class ListaAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private ListView lista;
private Button btnAbrir;
private RelativeLayout relativeLayout;
private ObjectAnimator animation;
public ListaAdapter (ArrayList<String> list, Context context, ListView a) {
this.list = list;
this.context = context;
this.lista = a;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list, null);
}
TextView listItemText = (TextView)view.findViewById(R.id.textoItem);
listItemText.setText(list.get(position));
final LinearLayout btnLayout = (LinearLayout)
view.findViewById(R.id.btnLayout);
relativeLayout = (RelativeLayout) view.findViewById(R.id.layout);
btnAbrir = (Button)view.findViewById(R.id.btnAbrir);
ImageButton btnNaoSei = (ImageButton)view.findViewById(R.id.btnNaoSei);
ImageButton btnAceitar =
(ImageButton)view.findViewById(R.id.btnAceitar);
ImageButton btnNegar = (ImageButton)view.findViewById(R.id.btnNegar);
animation = ObjectAnimator.ofFloat(btnAbrir,"x",200);
btnLayout.setVisibility(View.GONE);
btnAbrir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnLayout.setVisibility(View.VISIBLE);
animate();
}
});
return view;
}
private void animate()
{
animation.setDuration(500);
animation.start();
}
}
And this is my custom_list.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/white">
<LinearLayout
android:id="#+id/btnLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_barra">
<ImageButton
android:id="#+id/btnNaoSei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentTop="false"
android:backgroundTint="#color/compras_barra"
android:contextClickable="false"
app:srcCompat="#drawable/a12" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNaoSei"
android:layout_centerHorizontal="true"
android:text="NÃO SEI"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras">
<ImageButton
android:id="#+id/btnAceitar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:background="#color/compras"
app:srcCompat="#drawable/a11" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/btnAceitar"
android:paddingEnd="#dimen/_3sdp"
android:paddingTop="#dimen/_8sdp"
android:text="ACEITAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="#dimen/_32sdp"
android:layout_height="#dimen/_50sdp"
android:background="#color/compras_texto2">
<ImageButton
android:id="#+id/btnNegar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#color/compras_texto2"
app:srcCompat="#drawable/a10" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="12dp"
android:paddingBottom="#dimen/_1sdp"
android:text="NEGAR"
android:textColor="#color/white"
android:textSize="8sp" />
</RelativeLayout>
</LinearLayout>
<Button
android:id="#+id/btnAbrir"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/a09"
android:layout_alignTop="#+id/textoItem"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/textoItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:maxWidth="200dp"
android:text="TextView"
android:textColor="#color/compras_texto2" />
</RelativeLayout>
Any idea on how to accomplish this? Thanks in advance.
The things you need to adopt the following method and create a ViewHolder so that your adapter will know each element of each row in the List
The last UI element of the row only animates because this the only last known id of components for your Adapater when he was creating the Views in your List. So it will animates for the last row elements
You have to add ViewHolder nested class inside your Adapter and declare your UI components. Use setTag and getTag methods inside the getView
Overall you have to create your adapter things like this in getView
public class SListAdapter extends ArrayAdapter<String> {
private Context context;
private String[] seCtnColors;
private List<Subscription> item;
private ViewHolder mainViewHolder = null;
private ViewHolder viewHolder;
private LayoutInflater inflater;
private View row;
public SListAdapter(Context c, List<Subscription> subscriptions)
{
super(c, R.layout.row,R.id.rowNameTV);
this.context=c;
this.item = subscriptions;
}
#Override
public int getCount() {
return this.item.size();
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
row = null;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row,parent,false);
viewHolder = new ViewHolder();
initAdapterUI();
setAdapterDataset(position);
return row;
}
private void initAdapterUI() {
viewHolder.animatedGifViewHol = row.findViewById(R.id.row_animated_gif);
viewHolder.alertBarVerticalViewHol = row.findViewById(R.id.alertBarVerticalView);
viewHolder.firstNameTVHol = (TextView) row.findViewById(R.id.rowNameTV);
viewHolder.phoneNumberTVHol = (TextView) row.findViewById(R.id.rowNumberTV);
viewHolder.switchStateTVHol = (TextView) row.findViewById(R.id.switchStateTV);
row.setTag(viewHolder);
}
private void setAdapterDataset(int position) {
mainViewHolder = (ViewHolder) row.getTag();
mainViewHolder.alertBarVerticalViewHol.setBackgroundColor(Color.RED);
mainViewHolder.switchStateTVHol.setTextColor(Color.RED);
mainViewHolder.firstNameTVHol.setText(item.get(position).getFirstName());
mainViewHolder.phoneNumberTVHol.setText(item.get(position).getNumber());
}
public class ViewHolder{
View animatedGifViewHol;
View alertBarVerticalViewHol;
TextView firstNameTVHol;
TextView switchStateTVHol;
TextView phoneNumberTVHol;
}
}
I want to know why it tells me: "Unfortunately your app has stopped"?
I followed all the steps in this video:
https://www.youtube.com/watch?v=0zQCv0Xb3pk&index=84&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa
my code in main Activity
public class MainActivity extends Activity {
ListView list;
String[] mimititles;
String[] description;
int[] images = {R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m4,
R.drawable.m5,R.drawable.m6,R.drawable.m7,R.drawable.m8,R.drawable.m9,R.drawable.m10};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res= getResources();
mimititles= res.getStringArray(R.array.title);
description = res.getStringArray(R.array.description);
list=(ListView)findViewById(R.id.listView);
ibrahimadapter adapter = new ibrahimadapter(this,mimititles,images,description);
list.setAdapter(adapter);
}
}
class ibrahimadapter extends ArrayAdapter<String>
{
Context context;
int[] images;
String [] tiltearray;
String [] descrip;
ibrahimadapter(Context c,String[]titles,int[]imgs,String[] desc )
{
super(c,R.layout.single_row,R.id.textView1,titles);
this.context=c;
this.images=imgs;
this.tiltearray=titles;
this.descrip=desc;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View raw=inflate.inflate(R.layout.single_row,parent,false);
ImageView myimage= (ImageView) raw.findViewById(R.id.imageView1);
myimage.setImageResource(images[position]);
TextView mytitle = (TextView) raw.findViewById(R.id.textView1);
mytitle.setText(tiltearray[position]);
TextView mydescription = (TextView) raw.findViewById(R.id.textView2);
mydescription.setText(descrip[position]);
return raw;
}
}
my code in activity_main.xml layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
xmlns:android2="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android2:id="#+id/listView"
android2:layout_width="match_parent"
android2:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
my code in single_row.xml
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:src="#drawable/m1" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageView1"
android:layout_toRightOf="#+id/imageView1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
In your adapter:
your constructor should be: public ibrahimadapter(....) {.....}
getView function change to:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final Holder holder;
View raw = converView;
if(raw == null){
raw=inflate.inflate(R.layout.single_row,parent,false);
holder = new Holder();
holder.imgView = (ImageView) raw.findViewById(R.id.imageView1);
holder.myTitle = (TextView) raw.findViewById(R.id.textView1);
holder.myDes = (TextView) raw.findViewById(R.id.textView2);
raw.setTag(holder)
} else {
holder=(Holder)raw.getTag();
}
holder.imgView.setImageResource(images[position]);
holder.myTitle.setText(tiltearray[position]);
holder.myDes.setText(descrip[position]);
return raw;
}
public static class Holder {
ImageView imgView;
TextView myTitle;
TextView myDes;
}
Try this:
Change this
super(c,R.layout.single_row,R.id.textView1,titles)
To this
super(c,R.layout.single_row,titles)
Please check that the number of items for each String[] mimititles, String[] description and int[] images are all of the same length.
E.g if you want to show 5 item in the List, then you must 5 images, 5 mimititles and 5 description.
So check your string-array files
I've Created a List view with ArrayAdapter and when i run it , its showing me an empty activity in the runtime and i don't know the wrong:
here is the MainActivity and the ArrayAdapter Classes
public class MainActivity extends Activity {
String [] title ;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
title = getResources().getStringArray(R.array.titles);
list.setAdapter(new MyAdapter(MainActivity.this, title));
}
}
class MyAdapter extends ArrayAdapter{
String [] title;
Context context;
public MyAdapter(Context context, String[] title) {
super(context,R.layout.single_row,R.id.img);
this.title = title;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
ImageView iv;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_row, parent, false);
}
tv = (TextView) row.findViewById(R.id.title);
tv.setText(title[position]);
iv = (ImageView) row.findViewById(R.id.img);
iv.setImageResource(R.drawable.arrow);
return row;
}
}
activity_main.xml that contain the ListView
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
single_row.xml that contains the elements of the listview
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img"
android:src="#drawable/arrow"
android:layout_margin="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textSize="#dimen/abc_action_bar_stacked_max_height"
android:id="#+id/title"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
Change this line of code in your adapter.
LayoutInflater inflater = LayoutInflater.from(context);
Note : i have refered similar questions and still cannot find any mistakes
NullPointerException at
listView.addHeaderView(header);
AND
listView.setAdapter(adapter);
detailed code :
Veg v = new Veg();
v.setId(1);
v.setName("some");
v.setPrice(15.0);
List<Veg> vegData = new ArrayList<Veg>();
// dummy add for testing
vegData.add(v);
vegData.add(v);
vegData.add(v);
CustomAdapter adapter = new CustomAdapter(this,
R.layout.custom_list_rows, vegData);
listView = (ListView) findViewById(R.id.listView1);
View header = getLayoutInflater().inflate(
R.layout.custom_list_header, null);
listView.addHeaderView(header);
listView.setAdapter(adapter);
getView() part
public class CustomAdapter extends ArrayAdapter<Veg> {
Context context;
int layoutResourceId;
List<Veg> data;
public CustomAdapter(Context context, int resource, List<Veg> data) {
super(context, resource);
this.context = context;
this.layoutResourceId = resource;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CustomHolder();
holder.at = (TextView) row.findViewById(R.id.customheaderbutton1);
holder.bt = (TextView) row.findViewById(R.id.customheaderbutton2);
holder.ct = (TextView) row.findViewById(R.id.customheaderbutton3);
row.setTag(holder);
} else {
holder = (CustomHolder) row.getTag();
}
holder.at.setText(String.valueOf(data.get(position).getId()));
holder.bt.setText(data.get(position).getName());
holder.ct.setText(String.valueOf(data.get(position).getPrice()));
return row;
}
static class CustomHolder {
TextView at;
TextView bt;
TextView ct;
}
}
main activity xml file
<?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"
android:background="#FFFFFF">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
custom_list_header.xml
<?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="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/customheaderbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text="marketname" />
<Button
android:id="#+id/customheaderbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="edit" />
<Button
android:id="#+id/customheaderbutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="+" />
</LinearLayout>
custom_list_rows.xml
<?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="fill_parent" >
<TextView
android:id="#+id/elementIDtext"
android:layout_width="11dp"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/elementName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.30"
android:text="#string/elementName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/elementSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/elementValue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="62dp"
android:layout_height="wrap_content"
android:text="#string/_" />
</LinearLayout>
Sounds like listView is null, which means findViewById() returned null: it couldn't find the control you're looking for. Without seeing your layout there's no way to debug further, but look to be sure R.id.listView1 refers to a valid element.
In getView, change
holder.at = (TextView) row.findViewById(R.id.customheaderbutton1);
holder.bt = (TextView) row.findViewById(R.id.customheaderbutton2);
holder.ct = (TextView) row.findViewById(R.id.customheaderbutton3);
to
holder.at = (TextView) row.findViewById(R.id.elementIDtext);
holder.bt = (TextView) row.findViewById(R.id.elementName);
holder.ct = (TextView) row.findViewById(R.id.elementSummary);
Replace your Code With Below:
package com.example.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class List extends Activity
{
private ListView listview;
Veg Veg = new Veg();
ArrayList<Veg> data = new ArrayList<Veg>();
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listview = (ListView)findViewById(R.id.listView);
for(int i=1;i<101;i++)
{
Veg = new Veg();
Veg.setName("Item "+i);
Veg.setID("ID "+i);
Veg.setPrice("Price "+i);
data.add(Veg);
}
View headerView = getLayoutInflater().inflate(
R.layout.custom_list_header, null);
listview.addHeaderView(headerView);
adapter = new CustomAdapter(this,R.layout.custom_list_rows,data);
listview.setAdapter(adapter);
}
class CustomAdapter extends ArrayAdapter<Veg>
{
Context con;
int Res;
ArrayList<Veg> data;
public CustomAdapter(Context context, int resource, ArrayList<Veg> data) {
super(context, resource,data);
// TODO Auto-generated constructor stub
con = context;
Res = resource;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomHolder holder = null;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(Res, parent, false);
holder = new CustomHolder();
holder.at = (TextView) row.findViewById(R.id.elementIDtext);
holder.bt = (TextView) row.findViewById(R.id.elementName);
holder.ct = (TextView) row.findViewById(R.id.elementSummary);
row.setTag(holder);
} else {
holder = (CustomHolder) row.getTag();
}
holder.at.setText(String.valueOf(data.get(position).getID()));
holder.bt.setText(data.get(position).getName());
holder.ct.setText(String.valueOf(data.get(position).getPrice()));
return row;
}
class CustomHolder {
TextView at;
TextView bt;
TextView ct;
}
}
}
Also You have to Change your XML Something Like below instead giving Static Width like 11 dp.
custom_list_rows.xml
<?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="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/elementIDtext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/elementName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="elementName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/elementSummary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="elementValue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn" />
</LinearLayout>
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..