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>
Related
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 .
My xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="#+id/address" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:layout_below="#+id/address"
android:id="#+id/win_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/win_list"
android:layout_below="#+id/win_title"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/lose_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lose_list"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/cannot_compare_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cannot_compare_list"
android:layout_weight="1" />
</LinearLayout>
And this is my custom Adapter:
public class AnalysisResultAdapter extends ArrayAdapter<ComparisonResult> {
static class ViewHolder {
TextView address;
TextView win_title;
ListView win_list;
TextView lose_title;
ListView lose_list;
TextView cannot_compare_title;
ListView cannot_compare_list;
}
private Hashtable<String, SevenEleven[]> sevenElevenData;
public AnalysisResultAdapter(Context context, int resource, List<ComparisonResult> houses) {
super(context, resource, houses);
}
public void setSevenElevenData(Hashtable<String, SevenEleven[]> sevenElevenData) {
this.sevenElevenData = sevenElevenData;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
view = viewInflater.inflate(R.layout.analysis_result_list_item, null);
holder = new ViewHolder();
holder.address = (TextView) view.findViewById(R.id.address);
holder.win_title = (TextView) view.findViewById(R.id.win_title);
holder.win_list = (ListView) view.findViewById(R.id.win_list);
holder.lose_title = (TextView) view.findViewById(R.id.lose_title);
holder.lose_list = (ListView) view.findViewById(R.id.lose_list);
holder.cannot_compare_title = (TextView) view.findViewById(R.id.cannot_compare_title);
holder.cannot_compare_list = (ListView) view.findViewById(R.id.cannot_compare_list);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (this.sevenElevenData.size() == 0) {
return view;
}
ComparisonResult result = getItem(position);
holder.address.setText(result.house.Address);
if (result.win.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.win) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.win_title.setText("win");
holder.win_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
if (result.lose.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.lose) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.lose_title.setText("lose");
holder.lose_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
if (result.cannotBeCompared.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.cannotBeCompared) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.cannot_compare_title.setText("cannotBeCompared");
holder.cannot_compare_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
return view;
}
}
The problem is: Only the #+id/address TextView shows the text successfully, other things are not shown. But I don't know why this happened.
How can I solve this problem ? Can someone help me ?
Thanks.
Use ScrollView intead of outer(parent) ListView.
And use LisView for the inner ListView.
It gives you the scrolling feature.
1/ layout_below only works in RelativeLayout, you are using a LinearLayout with horizontal orientation so all the elements will be rendered side by side, and all elements have the layout_width:"match_parent" so only the first element will be shown.
2/ when you use a ListView in another scrollView the renderer can't calculate the height of the second one
I use GridView in Fragmnet, Host actvity inlcudes SlidingMenu and My grid Items use simple google now card style. but my gridView is too laggy even i use performance trick for my adapter (recycling and holder pattern).
My Question:
Is there another performance tweak? or Do i follow right way for the performance? (GridView and Adapter)
gridView adapter:
public class CustomGridAdapter extends ArrayAdapter<RbtItem> {
Context context;
int layoutResourceId;
ArrayList<RbtItem> data = new ArrayList<RbtItem>();
public CustomGridAdapter(Context context, int layoutResourceId,
ArrayList<RbtItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
RbtItem item = data.get(position);
holder.txtTitle.setText(item.getText());
holder.imageItem.setImageResource(R.drawable.bg);
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
gridView item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/nowCardStyleRaw"
android:layout_width="100dp"
android:layout_height="140dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/bg" />
<TextView
android:id="#+id/item_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:ellipsize="none"
android:gravity="right"
android:maxLines="1"
android:textSize="18sp" />
<TextView
android:id="#+id/item_singer"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="-20dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="5dp"
android:ellipsize="none"
android:gravity="right"
android:maxLines="1"
android:textSize="11sp"
android:textColor="#FF4444" />
</TextView>
fargment layout:
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_rbt_list_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#e3e3e3"
android:columnWidth="97dp"
android:gravity="center"
android:numColumns="auto_fit"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:stretchMode="columnWidth"
android:verticalSpacing="12dp" >
</GridView>
I am trying to show elements in a list view using Fragments. I created my custom view as follow
graphical representation of list_row.xml
list_row.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="#string/image_desc"/>
</LinearLayout>
<!-- Menu name -->
<TextView
android:id="#+id/menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="#string/menu_name"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Description -->
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_below="#id/menu_name"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="#string/description"
android:textColor="#343434"
android:textSize="10sp"
tools:ignore="SmallSp" />
<!-- Price -->
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/menu_name"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="#string/price"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
</RelativeLayout>
In fragment.xml file, I simply put a list view inside a linear layout
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
I have an array of menu objects, that has the necessary fields to populate list_row.xml like menu name, description, price and image. I couldn't find a way to fill fragment.xml's listView with the list_row elements. Any help or idea would be appreciated.
PS: I think in fragment.xml instead of android:id="#+id/listView1" field, I have to write android:id="#id/list" since I'm using ListFragment
I solved my problem (as system32 suggests on comment) creating a CustomArrayAdapter class, and setting it as the adapter for my listView.
First I changed android:id="#+id/listView1" to android:id="#android:id/list" in fragment.xml.
CustomArrayAdapter.java
public class CustomArrayAdapter extends ArrayAdapter<Menu> {
Context context;
public CustomArrayAdapter(Context context, int textViewResourceId, List<Menu> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtMenuName;
TextView txtMenuDesc;
TextView txtPrice;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Menu rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.txtMenuName = (TextView) convertView.findViewById(R.id.menu_name);
holder.txtMenuDesc = (TextView) convertView.findViewById(R.id.description);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView.findViewById(R.id.list_image);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtMenuDesc.setText(rowItem.getDescription());
holder.txtMenuName.setText(rowItem.getName());
holder.txtPrice.setText(String.valueOf(rowItem.getPrice()) + " TL");
//holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
Then I use it in my Fragment class
public static class Fragment extends ListFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
private ListView listView;
private ArrayList<Menu> menuItems;
private CustomArrayAdapter mAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment,
container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
int num = getArguments().getInt(ARG_SECTION_NUMBER);
// GlobalList is a class that holds global variables, arrays etc
// getMenuCategories returns global arraylist which is initialized in GlobalList class
menuItems = GlobalList.getMenuCategories().get(num).getMenu();
mAdapter = new CustomArrayAdapter(getActivity(), android.R.id.list, menuItems);
listView.setAdapter(mAdapter);
}
}
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..