I created a custom adapter to fill the item i have depending on how many elements there is but it only fills the first item and i can't figure out why
main.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=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:id="#+id/customList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView" />
</RelativeLayout>
item.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">
<TextView
android:id="#+id/item_text_2"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 2"
android:layout_gravity="right"
android:layout_below="#+id/item_text_1"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/item_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/item_text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
Custom Class
public class CustomClass {
private String txt1, txt2, txt3;
public CustomClass(String txt1, String txt2, String txt3) {
this.txt1 = txt1;
this.txt2 = txt2;
this.txt3 = txt3;
}
public String getTxt1() {
return txt1;
}
public String getTxt2() {
return txt2;
}
public String getTxt3() {
return txt3;
}
}
MainActivity
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<CustomClass> lst_stuff = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateCustomClass();
populateListView();
}
private void populateCustomClass()
{
lst_stuff.add(new CustomClass("First", "I Got", "FIRST!!!!!"));
lst_stuff.add(new CustomClass("Second", "How did you...", "you know what GG"));
lst_stuff.add(new CustomClass("Third", "I Got", "Last?!! ... I am just too good to be first"));
}
private void populateListView()
{
ArrayAdapter<CustomClass> adapter = new CustomAdapter();
ListView lst = (ListView) findViewById(R.id.customList);
lst.setAdapter(adapter);
}
private class CustomAdapter extends ArrayAdapter<CustomClass>
{
public CustomAdapter()
{
super(MainActivity.this, R.layout.item, lst_stuff);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.item, parent, false);
try
{
CustomClass customClass = lst_stuff.get(position);
TextView textView_1 = (TextView) findViewById(R.id.item_text_1);
textView_1.setText(customClass.getTxt1());
TextView textView_2 = (TextView) findViewById(R.id.item_text_2);
textView_2.setText(customClass.getTxt2());
TextView textView_3 = (TextView) findViewById(R.id.item_text_3);
textView_3.setText(customClass.getTxt3());
}
catch (NullPointerException exception)
{
Toast.makeText(MainActivity.this, "Exception", Toast.LENGTH_SHORT).show();
exception.printStackTrace();
}
return itemView;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
is there something missing or something doing wrong?
All seems to be ok, but I would change what is on the method getView() by this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
CustomClass customClass = lst_stuff.get(position);
((TextView) rowView.findViewById(R.id.item_text_1)).
setText(customClass.getTxt1());
((TextView) rowView.findViewById(R.id.item_text_2)).
setText(customClass.getTxt2());
((TextView) rowView.findViewById(R.id.item_text_3)).
setText(customClass.getTxt3());
return rowView;
}
I think that you must obtain first a instance of inflater, and then inflate your view (rowView) with your layout (R.layout.item).
Good luck!
Related
I want my GridView to display only 16 random letters, a 4x4 tile and if it's possible I don't want any space between the letters.
I also want it to display its corresponding text to a TextView to form a word. I put it in a toast for now to show how it should be. But it only shows the corresponding letter of the image. How can I achieve that? I dont have any idea on how should i do it.
Any help will be appreciated.
New to Android Programming
grid_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="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="1dp"
android:layout_marginEnd="1dp"
android:contentDescription="#string/hello_world"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="15sp"
android:visibility="gone" >
</TextView>
</LinearLayout>
activity_main.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: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.grid.gridgame.MainActivity" >
<TextView
android:id="#+id/wordHere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1234567890"/>
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
</RelativeLayout>
MainActivity.java
package com.grid.gridgame;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView gridView;
String word;
int rando;
static final String[] setOne = new String[] {
"A", "M","C", "Qu",
"Z", "B", "D", "A",
"E", "I", "E", "L",
"M", "N", "T", "P" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridViewCustom);
gridView.setAdapter(new GridViewCustomAdapter(this, setOne));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.textView))
.getText(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GridViewCustomAdapter
package com.grid.gridgame;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewCustomAdapter extends BaseAdapter {
private Context context;
private final String[] setOne;
public GridViewCustomAdapter(Context context, String[] setOne) {
this.context = context;
this.setOne = setOne;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
convertView = new ImageView(context);
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.grid_row, null);
// set value into TextView
TextView textView = (TextView) gridView
.findViewById(R.id.textView);
textView.setText(setOne[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
String allletters = setOne[position];
if (allletters.equals("A")) {
imageView.setImageResource(R.drawable.a);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("C")) {
imageView.setImageResource(R.drawable.c);
} else if (allletters.equals("Qu")) {
imageView.setImageResource(R.drawable.qu);
} else if (allletters.equals("Z")) {
imageView.setImageResource(R.drawable.z);
} else if (allletters.equals("B")) {
imageView.setImageResource(R.drawable.b);
} else if (allletters.equals("D")) {
imageView.setImageResource(R.drawable.d);
} else if (allletters.equals("A")) {
imageView.setImageResource(R.drawable.a);
} else if (allletters.equals("E")) {
imageView.setImageResource(R.drawable.e);
} else if (allletters.equals("I")) {
imageView.setImageResource(R.drawable.i);
} else if (allletters.equals("E")) {
imageView.setImageResource(R.drawable.e);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("N")) {
imageView.setImageResource(R.drawable.n);
} else if (allletters.equals("T")) {
imageView.setImageResource(R.drawable.t);
} else if (allletters.equals("P")) {
imageView.setImageResource(R.drawable.p);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return setOne.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
If you want to store the letter you can simply create a string and store the letter push by user.
Example code:
String word; // word that you want to be display
String letter = MOBILE_OS[id]; // get the corresponding letter from array from the user selected
word.add(letter);
I have a ListView but I can´t put images on it.
The error is in Activity_listView at holder.image.setImageDrawable(datos[position].getImage());
The error message is
The method setImageDrawable(Drawable) in the type ImageView is not applicable
for the arguments (ImageView)
Any idea for solve this?
Activity (Activity_listView):
package com.simarro.asteroids;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Activity_listView extends ActionBarActivity {
private ListView lstOpciones;
ImageView asteroid = (ImageView)findViewById(R.id.img_asteroid);
private puntuacion[] datos = new puntuacion[] {
new puntuacion(asteroid,"Pepito Dominguez", "15489"),
new puntuacion(asteroid,"Pedro Martínez", "16598"),
new puntuacion(asteroid,"Paco Perez", "16332"),
new puntuacion(asteroid,"Rosana Fernandez", "18792"),
new puntuacion(asteroid,"Paco Jones", "960") };
class AdaptadorTitulares extends ArrayAdapter<puntuacion> {
Activity context;
AdaptadorTitulares(Activity context) {
super(context, R.layout.puntuacion, datos);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ViewHolder holder;
if (item == null) {
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(R.layout.puntuacion, null);
holder = new ViewHolder();
holder.image=(ImageView)item.findViewById(R.id.img_asteroid);
holder.jugador = (TextView) item.findViewById(R.id.LblJugador);
holder.puntuacion = (TextView) item.findViewById(R.id.LblPuntuacion);
item.setTag(holder);
} else {
holder = (ViewHolder) item.getTag();
}
holder.image.setImageDrawable(datos[position].getImage());
holder.jugador.setText(datos[position].getJugador());
holder.puntuacion.setText(datos[position].getPuntuacion());
return (item);
}
}
static class ViewHolder {
ImageView image;
TextView jugador;
TextView puntuacion;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
AdaptadorTitulares adaptador = new AdaptadorTitulares(this);
lstOpciones = (ListView) findViewById(R.id.LstOpciones);
//¡FUUUUUUUUU-SIÓN!
lstOpciones.setAdapter(adaptador);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_list_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The xml of content for layout with listView (puntuacion.xml):
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal" >
<ImageView
android:id="#+id/img_asteroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/asteroid" />
<TextView
android:id="#+id/LblJugador"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0"
android:textSize="30px"
android:textStyle="bold" />
<TextView
android:id="#+id/LblPuntuacion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:textStyle="normal" />
</GridLayout>
The layout where is the listView (listView.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/LstOpciones"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The class for get values (Puntuacion)
package com.simarro.asteroids;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
public class puntuacion
{
private ImageView image;
private String jugador;
private String puntuacion;
public puntuacion(ImageView img, String tit, String sub){
image=img;
puntuacion = sub;
jugador = tit;
}
public ImageView getImage(){
return image;
}
public String getJugador(){
return jugador;
}
public String getPuntuacion(){
return puntuacion;
}
}
Instead of returning ImageView in getImage(), return a Drawable and change it to
public Drawable getImage() {
return image.getDrawable();
}
getDrawable() Docs
As the error says, the method setImageDrawable() takes a Drawable type but you are passing it an ImageView
Below is my code. When i try to run the app it gives me an error. it says there is a fatal exception in main. Can anybody help me out?
This is my main.java
package com.go.nextgendevelopment.uefaeuro2016;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
public class main extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adapter
setListAdapter(new Myadapter(this,android.R.layout.simple_list_item_1, R.id.textView, getResources().getStringArray(R.array.teams)));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class Myadapter extends ArrayAdapter<String> {
public Myadapter(Context context, int resource, int textViewResourceID, String[] objects) {
super(context,resource,textViewResourceID,objects);
}
#Override
public View getView(int position, View convertView,ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.teams);
ImageView iv = (ImageView) row.findViewById(R.id.imageView);
TextView tv = (TextView) row.findViewById(R.id.textView);
tv.setText(items[position]);
if (items[position].equals("ajax")){
iv.setImageResource(R.drawable.logo_ajax);
}
else if (items[position].equals("feyenoord")){
iv.setImageResource(R.drawable.logo_feyenoord);
}
return super.getView(position,convertView, parent);
}
}
}
activity_main.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_gravity="center_horizontal" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Thanks in advance!
Remove the setContentview in onCreate()
While setting the list adapter pass your listitem layout
I have an android application that has a custom list view with an image and a text and works as it should but I want to add a header section to separate between two items.
I have been searching for several days now without any success can anyone help me?
this is my list :
i need to add header section with value "Group A" before the first item that the second header section will be before the Spain flag "Group B"
so can anyone help me ??
this is the xml FILE for the list view
(activity_group_list.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: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=".GroupList" >
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>**
xml File for the item row
row_list_group
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/algeria_flag" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:ems="10" />
</RelativeLayout>
CustomAdapter.java
package com.devleb.expandablelistdemo3;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
private static ArrayList<ItemDetails> itemDetailsarrayList;
LayoutInflater layoutInflater;
String[] teamName;
int[] teamFlag;
Context context;
public CustomAdapter(ArrayList<ItemDetails> result, Context c) {
itemDetailsarrayList = result;
context = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return itemDetailsarrayList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return itemDetailsarrayList.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.row_list_group, parent,
false);
TextView txt = (TextView) row.findViewById(R.id.textView1);
ImageView imgv = (ImageView) row.findViewById(R.id.imageView1);
txt.setText(itemDetailsarrayList.get(position).getName());
imgv.setImageResource(itemDetailsarrayList.get(position).getImage());
return row;
}
}
// in the GroupList file i need to add String array for the groups as the String array of name
GroupList.java
package com.devleb.expandablelistdemo3;
import java.lang.reflect.Array;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.view.Menu;
import android.widget.ListView;
public class GroupList extends Activity {
int[] img = { R.drawable.brazil_flag, R.drawable.croatian_flag,
R.drawable.mexico_flag, R.drawable.cameroon_flag, R.drawable.spain,
R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
R.drawable.australia, R.drawable.colombia_flag, R.drawable.gress,
R.drawable.cote_divoire_flag, R.drawable.japan,
R.drawable.uruguay_flag, R.drawable.costa_rica_flag,
R.drawable.england_flag, R.drawable.italy_flag,
R.drawable.switzerland, R.drawable.ecuador_flag,
R.drawable.france_flag, R.drawable.honduras_flag,
R.drawable.argentina_flag, R.drawable.bousna, R.drawable.iran_flag,
R.drawable.nigeria_flag, R.drawable.germany_flag,
R.drawable.portugal, R.drawable.ghana_flag,
R.drawable.united_states_flag, R.drawable.belgium_flag,
R.drawable.algeria_flag, R.drawable.russia_flag,
R.drawable.korea_flag };
String[] name = { "BRA", "CRO", "MEX", "CMR", "ESP", "NED", "CHI", "AUS",
"COL", "GRE", "CIV", "JPN", "URU", "CRC", "ENG", "ITA", "SUI",
"ECU", "FRA", "HON", "ARG", "BIH", "IRN", "NGA", "GER", "POR",
"GHA", "USA", "BEL", "ALG", "RUS", "KOR" };
ItemDetails item_details;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_list);
ArrayList<ItemDetails> result = getList();
ListView lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new CustomAdapter(result, getApplicationContext()));
}
private ArrayList<ItemDetails> getList() {
ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
for (int i = 0; i < name.length; i++) {
item_details = new ItemDetails();
item_details.setName(name[i]);
item_details.setImage(img[i]);
results.add(item_details);
}
return results;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.group_list, menu);
return true;
}
}
Using your current code, try modifying your arrays slightly like this:
int[] img = { null, R.drawable.brazil_flag, R.drawable.croatian_flag,
R.drawable.mexico_flag, R.drawable.cameroon_flag, null, R.drawable.spain,
R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
R.drawable.australia, null, ... };
String[] name = { "Group A", "BRA", "CRO", "MEX", "CMR", "Group B","ESP", "NED", "CHI", "AUS", ... };
And then in the getView method in your adapter do this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = null;
/// inflate a header view here
if (itemDetailsarrayList.get(position).getImage()==null) {
row = layoutInflater.inflate(R.layout.row_group_header, parent,
false);
TextView txt = (TextView) row.findViewById(R.id.groupTitle);
txt.setText(itemDetailsarrayList.get(position).getName());
/// Inflate your regular item row here.
} else {
row = layoutInflater.inflate(R.layout.row_list_group, parent,
false);
TextView txt = (TextView) row.findViewById(R.id.textView1);
ImageView imgv = (ImageView) row.findViewById(R.id.imageView1);
txt.setText(itemDetailsarrayList.get(position).getName());
imgv.setImageResource(itemDetailsarrayList.get(position).getImage());
}
return row;
}
EDIT
/// FILE: res/layout/row_group_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#000"
android:padding="5dip" >
<TextView
android:id="#+id/groupTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:textColor="#FFF"
android:ems="10" />
</RelativeLayout>
Notice the Null on the image array and the "Group X" in the names array.
I am new to Android programming. I'm trying Shopping cart tutorial trying to populate the listview of catalog view using the productAdapter. But the list is not getting populated and also getview function is not getting called. I'am attaching my files. Please let me know,where I'am going wrong.
ProductAdapter.java
package com.example.helloshoppingcart;
import java.util.List;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class ProductAdapter extends BaseAdapter {
private class viewItem{
ImageView productImageView;
TextView productTitle;
CheckBox productCheckbox;
};
private List<Product> mproductList;
private LayoutInflater minflater;
private Boolean mShowCheckbox;
ProductAdapter(List<Product> productList, LayoutInflater inflater, Boolean showCheckbox)
{
this.mproductList = productList;
this.minflater = inflater;
this.mShowCheckbox = showCheckbox;
}
#Override
public View getView(int pos, View convertView, ViewGroup parent ){
final viewItem item;
if (convertView == null){
convertView = minflater.inflate(R.layout.item, null);
item = new viewItem();
item.productImageView = (ImageView) convertView.findViewById(R.id.ImageViewItem);
item.productTitle= (TextView) convertView.findViewById(R.id.TextViewItem);
//item.productCheckbox = (CheckBox) convertView.findViewById(R.id.CheckBoxSelected);
convertView.setTag(item);
}else{
item = (viewItem) convertView.getTag();
}
Product prod = mproductList.get(pos);
item.productImageView.setImageDrawable(prod.productImage);
//item.productCheckbox.setChecked(prod.selected);
item.productTitle.setText(prod.title);
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
CatalogActivity.java
package com.example.helloshoppingcart;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class CatalogActivity extends Activity {
private List<Product> mproductList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
//get the catalog and display all the records
mproductList = shoppingCartHelper.getCatalog(getResources());
ListView catalogListView = (ListView) findViewById(R.id.ListViewCatalog);
ProductAdapter catalogListAdapter = new ProductAdapter(mproductList, getLayoutInflater(), false);
catalogListView.setAdapter(catalogListAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_catalog, menu);
return true;
}
}
activity_catalog.xml
<?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">
<!--<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#000000"
android:textSize="24dip" android:layout_margin="5dip" android:text="Product Catalog"></TextView>-->
<ListView android:layout_height="wrap_content"
android:layout_weight="1" android:id="#+id/ListViewCatalog"
android:layout_width="fill_parent" android:background="#ffffff"
android:clickable="true" android:cacheColorHint="#ffffff">
</ListView>
<!-- <Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_margin="5dip"
android:layout_gravity="right" android:id="#+id/ButtonViewCart"
android:text="View Shopping Cart"></Button>-->
</LinearLayout>
item.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="vertical" >
<ImageView android:id="#+id/ImageViewItem"
android:layout_margin="5dip"
android:layout_height="wrap_content"
android:layout_width="100dip">
</ImageView>
<TextView android:id="#+id/TextViewItem"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:textSize="26dip"
android:text="Phone Names"
android:textColor="#000000"
android:minLines="2"
android:maxWidth="150dip">
</TextView>
<TextView android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</TextView>
<!-- <CheckBox android:layout_height="wrap_content"
android:layout_margin="5dip" android:id="#+id/CheckBoxSelected"
android:focusable="false" android:clickable="false"
android:layout_gravity="center" android:layout_width="wrap_content">
</CheckBox>-->
</LinearLayout>
In ProductAdapter, the getCount() method returns the number of items owned by the Adapter associated with this ListView. So, it should return the maximum number of items that the listview will display i.e. the size of the list you are using as a data source : mproductList.size()
The getView() method is not getting called because this function is returning 0 in your code.
Only took a fast glance, but getCount() should return the number of items
in the adapter, and therefore mproductList.size()