ListView from ArrayAdapter - android

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

Related

listview setonitemclicklistener doesn't work?

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

How to create custom list with checkboxes and text

**Adapter class**
public class checklist extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> name;
public checklist(Activity context,ArrayList<String> name) {
super(context, R.layout.activity_custm_list, name);
this.context = context;
this.name = name;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.activity_custm_list, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.check);
txtTitle.setText(name.get(position));
return rowView;
}
}
adapter_xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<CheckBox
android:id="#+id/check"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
listview_actitvty
public class Delete extends ActionBarActivity {
ListView lv ;
ArrayList<String> name = new ArrayList<>();
ArrayList<Integer> ids = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
lv = (ListView) findViewById(R.id.list);
Intent intent = getIntent();
name = intent.getStringArrayListExtra("note name");
ids = intent.getIntegerArrayListExtra("id");
System.out.println("-------*********----------------"+name);
// checklist dapter = new checklist(Delete.this,name);
checklist adapter = new checklist(Delete.this, name);
lv.setAdapter(adapter);
}
activity_xml
<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="com.example.mohamedreda.note.Delete">
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
</RelativeLayout>
this output of system.out.print
05-11 05:19:03.072 5464-5464/com.example.mohamedreda.note I/System.outīš• -------*********----------------[alissaas, mes, hhhhhhhhh, meshoo, zeko, mostafaaaa, ahmed, hi, ahmedddd, kattttb, xxxxxxxxxxx, sssssssssssssss, -----------------------------------, a44444, n, no, mes2, zeko5, ahmed7]
error
*
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setTag(java.lang.Object)' on a null object reference
at com.example.mohamedreda.note.checklist.getView(checklist.java:32)*
you can : ""+name.get(position);
became : txtTitle.setText(""+name.get(position));

getCheckedItemPositions() has a size of 0

I am trying to get the positions of the views from my listview but the size of the SparseBooleanArray is always 0.
This is the code from my activity.
IngredientIndivAdapter adapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_screen);
Intent intent = getIntent();
String prev_but = intent.getStringExtra("hi");
ArrayList<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("d");
list1.add("e");
adapter = new IngredientIndivAdapter(this,list1);
lv = (ListView)findViewById(R.id.listview1);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
public void remove_ingredient_button(View v){
SparseBooleanArray remove_list = lv.getCheckedItemPositions();
Log.d("poo",""+remove_list.size());
if(remove_list!=null)
for(int i = 0; i < remove_list.size(); i++){
if(remove_list.valueAt(i)){
View remove_view = (View)findViewById(remove_list.keyAt(i));
((LinearLayout)remove_view.getParent()).removeView(remove_view);
}
}
adapter.notifyDataSetChanged();
}
This is the XML for that activity
<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: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="com.JTA.recipeshoppinglist.ListScreen">
<LinearLayout
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">
<ListView
android:id="#+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<Button
android:id="#+id/add_b"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FFFF9900"
android:text="+"
android:onClick="add_button" />
<Button
android:id="#+id/remove_item"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="-"
android:onClick="remove_ingredient_button" />
This is my custom row XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/ingredient_check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkable="true"
android:background="#FF0000" />
</LinearLayout>
And this is my custom array adapter.
public class IngredientIndivAdapter extends ArrayAdapter<String> {
static class ViewHolder{
CheckBox text;
}
ArrayList<String> list = new ArrayList<String>();
Context context;
ViewHolder viewHolder;
public IngredientIndivAdapter(Context context, ArrayList<String> iList){
super(context,R.layout.ingredient_row,iList);
this.list=iList;
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.ingredient_row, parent, false);
viewHolder = new ViewHolder();
viewHolder.text = (CheckBox) convertView.findViewById(R.id.ingredient_check);
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.text.setText(list.get(position));
return convertView;
}
public boolean hasStableIds(){
return true;
}
}
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < listview.getCount(); i++){
if(checked.get(i)==true) {
// do something
}
}
I hope it will work.

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