getView() of a custom adapter giving error - android

I want to have different background color of each item of listview. Following is my code for getView() method of custom adaptor.
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
View v = super.getView(position, convertView, parent);
RatingBar ratingBar = ((RatingBar)convertView.findViewById(R.id.my_choices_row_rating));
ratingBar.setRating(mRatings[position]);
TextView tv = (TextView)convertView.findViewById(R.id.my_choices_row_text);
tv.setText(mStrings[position]);
Log.v(TAG,"getView nalist=" + mNAList[position]);
Log.v(TAG,"getView gotlist=" + mGotList[position]);
if(mNAList[position].equalsIgnoreCase("N/A")){
TextView tv1 = (TextView)convertView.findViewById(R.id.my_choices_row_na_text);
tv1.setText(mNAList[position]);
}
if(mGotList[position].equalsIgnoreCase("Purchased")){
TextView tv2 = (TextView)convertView.findViewById(R.id.my_choices_row_got_text);
tv2.setText(mGotList[position]);
}
convertView.setBackgroundColor(mColors[position]);
return convertView;
}
}
Here is my xml file for the row of the list
<?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:orientation="horizontal">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/RHE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="5dp"
android:layout_span = "3">
<TableRow android:layout_height="wrap_content">
<RatingBar
android:id="#+id/my_choices_row_rating"
android:progressDrawable="#drawable/hearts_rating_bar"
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="25sp"
android:numStars="5"
android:stepSize="1" />
<TextView
android:id="#+id/my_choices_row_text"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:textSize="18sp"
android:gravity="left|center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox
android:id="#+id/my_choices_row_checkBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</TableRow>
<TableRow android:layout_height="wrap_content">
<TextView
android:id="#+id/my_choices_row_na_text"
android:textSize="12sp"
android:gravity="left|center"
android:textColor="#8B0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/my_choices_row_got_text"
android:textSize="12sp"
android:textColor="#8B0000"
android:gravity="left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
I keep on getting following error
06-01 00:20:52.057: E/AndroidRuntime(711):FATAL EXCEPTION:main
06-01 00:20:52.057: E/AndroidRuntime(711):java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
06-01 00:20:52.057: E/AndroidRuntime(711):at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)

Your tutorial
Ask me if there are anything you dont understant
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//your colors list
ArrayList<Integer> colors = new ArrayList<Integer>();
for(int i = 0; i < 10; i++){
Random ran = new Random();
int color = ran.nextInt();
colors.add(color);
}
//get listview and set adapter for it
ListView lv = (ListView) findViewById(R.id.myListView);
lv.setAdapter(new MyColorAdapter(this, colors));
}
public class MyColorAdapter extends ArrayAdapter<Integer>{
ArrayList<Integer> colors;
Context context;
public MyColorAdapter(Context context, ArrayList<Integer> colors) {
super(context, R.layout.row, colors);
this.colors = colors;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, parent, false);
rowView.setBackgroundColor(colors.get(position));
return rowView;
}
}
}

Related

My ListView won't work ...it says: "Unfortunately your app has stopped"

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

ListView is not displayed in fragment

I implemented my own navigation drawer that comes from the left screen in my android application. The problem is that the ListView it is not visible and I can't figure out why.
This is the method onCreateView of the fragment where the list is (first I initialize the header of the layout and then the list with my custom adapter):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View firstAccessView = inflater.inflate(R.layout.fragment_navigation_drawer, null);
this.profileImageDrawer = (ImageView) firstAccessView.findViewById(R.id.imageViewProfileDrawer);
RoundImage roundedImage = new RoundImage(BitmapFactory.decodeResource(getResources(), R.mipmap.iconuseranonymous));
//this.profileImageDrawer.setImageDrawable(roundedImage);
Picasso.with((Activity) getActivity()).load("https://graph.facebook.com/" + Profile.getCurrentProfile().getId() + "/picture?height=105&width=105")
.placeholder(roundedImage)
.transform(new CircleTransform()).fit().centerCrop().into(this.profileImageDrawer);
this.nameSurnameDrawer = (TextView) firstAccessView.findViewById(R.id.textViewDrawner);
this.nameSurnameDrawer.setText(Profile.getCurrentProfile().getFirstName() + " " + Profile.getCurrentProfile().getLastName());
List<String> example = new ArrayList<String>();
example.add("Profile");
example.add("Ask");
example.add("Logout");
this.adapter = new DrawerAdapter(getActivity(), example);
list = (ListView) firstAccessView.findViewById(R.id.listDrawer);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
list.setItemChecked(mCurrentSelectedPosition,true);
return firstAccessView;
}
This is the code of my custom adapter
private Activity context;
private List<String> list;
private View view;
private TextView textView;
public DrawerAdapter(Activity context, List<String> list) {
super(context, R.layout.drawerlist_layout);
this.context=context;
this.list = list;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.drawerlist_layout, null);
textView = (TextView) view.findViewById(R.id.sectionDrawer);
textView.setText(this.list.get(position));
textView.setTextColor(Color.BLACK);
return view;
}
and finally this is the layout of my fragment: at the top I have an header with an image and a TextView and then I have my custom ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_weight="0.40"
android:background="#ffb200">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/imageViewProfileDrawer"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="20px" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Medium Text"
android:id="#+id/textViewDrawner"
android:layout_gravity="right|bottom"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:textIsSelectable="true" />
</FrameLayout>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="262dp" android:choiceMode="singleChoice"
android:divider="#cecece" android:dividerHeight="1dp"
android:background="#FFFFFF"
tools:context="com.bellantoni.chetta.lieme.NavigationDrawerFragment"
android:id="#+id/listDrawer"
android:drawSelectorOnTop="false"
android:smoothScrollbar="true"
android:longClickable="false"
android:nestedScrollingEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="true"
android:paddingLeft="10dp"
android:stackFromBottom="false"
android:scrollingCache="false"
android:paddingRight="10dp"
android:textFilterEnabled="false"
android:theme="#style/Base.Widget.AppCompat.ActionMode"
android:textAlignment="center"
android:transitionGroup="true"
android:layout_weight="0.60" />
I found my error in this line of code:
super(context, R.layout.drawerlist_layout);
where I missed the third parameter which in this case is list so it should be
super(context, R.layout.drawerlist_layout,list);
Set both FameLayout and ListView layout_width to 0dp instead of match_parent if you want to use layout_weight.

ListView from ArrayAdapter

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);

Custom Listview is not showing up the string

Listview is not showing the string list rather showing only the custom list view text
My string declaration n the main
String[] Example = new String[]
{ "Android Introduction","Android Setup/Installation","Android Hello World",
"Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android",
"Cz"};
ArrayAdapter<String> ExampleArrayAdapter = new CustomAdapter(this, Example);
ListView ExampleListView = (ListView)findViewById(R.id.listView);
ExampleListView.setAdapter(ExampleArrayAdapter);
here is my customAdapter.java
public class CustomAdapter extends ArrayAdapter {
private LayoutInflater inflater;
public CustomAdapter (Activity activity, String[] items){
super(activity, R.layout.custom_layout, items);
inflater = activity.getWindow().getLayoutInflater();
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(257), rnd.nextInt(258));
View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
rowView.setBackgroundColor(color);
return rowView;
}
}
here is the custom layout of my app, i have updated its...........
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="70dp"
android:layout_height="70dp"
android:background="#drawable/blue_bg">
<LinearLayout
android:layout_marginLeft="20dp"
android:layout_width="345dp"
android:layout_height="70dp"
android:background="#drawable/white_bg">
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="60dp"
android:src="#drawable/ic_launcher"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:gravity="left" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="19sp"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="15dp"
android:text="Tweet body text here"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:lines="3"
android:textColor="#000000"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Listview is not showing the string list rather showing only the custom
list view text
Becuase you are not setting text for TextView in each row of ListView just setting color of row.
so customize getView method and set TextView text from items String Array. see following example for more help:
Using an ArrayAdapter with ListView
You have to declare your CustomAdapter object in Activity As:
CustomAdapter ExampleArrayAdapter = new CustomAdapter(this, Example);
ListView ExampleListView = (ListView)findViewById(R.id.listView);
ExampleListView.setAdapter(ExampleArrayAdapter);
And Your Adapter Code should be:
public class CustomAdapter extends ArrayAdapter{
private LayoutInflater inflater;
private String[] items;
public CustomAdapter (Activity activity, String[] items){
super(activity, 0, items);
inflater = activity.getWindow().getLayoutInflater();
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(257), rnd.nextInt(258));
View rowView = inflater.inflate(R.layout.custom_layout, parent, false);
rowView.setBackgroundColor(color);
TextView tv = (TextView) rowView .findViewById(R.id.textView1);
tv.setText(items[position]);
return rowView;
}
}

Populate list of custom view using ListFragment

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);
}
}

Categories

Resources