This question already has an answer here:
ListView not showing at all
(1 answer)
Closed 4 years ago.
I need to add data to ListView by adapter. Adapter has some data.
As you can see here, the size of ArrayList is 417.But when I open the activity where the ListView is, I don't see anything here. Here's the code of adapter:
package asus.example.com.player;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class SongAdapter extends BaseAdapter {
private ArrayList<Song> songs;
private LayoutInflater layoutInflater;
SongAdapter(Context context, ArrayList<Song> songs){
this.songs = songs;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
#SuppressLint("ViewHolder")
LinearLayout songLay = (LinearLayout) layoutInflater.inflate(R.layout.song, parent, false);
TextView songTitle = songLay.findViewById(R.id.songTitle);
TextView songArtist = songLay.findViewById(R.id.songArtist);
Song curSong = songs.get(position);
songTitle.setText(curSong.getTitle());
songArtist.setText(curSong.getArtist());
songLay.setTag(position);
return songLay;
}
Code of Song class:
package asus.example.com.player;
public class Song {
private long id;
private String title;
private String artist;
public long getId() {
return id;
}
public String getTitle() {
return title;
}
String getArtist() {
return artist;
}
Song(long id, String title, String artist){
this.id = id;
this.title = title;
this.artist = artist;
}
}
Song.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">
<TextView
android:id="#+id/songTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#1111ff"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/songArtist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#2a7800"
android:textSize="18sp" />
</LinearLayout>
Layout of ativity, where there is a ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".ListOfSongsActivity">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="UselessParent">
<ListView
android:id="#+id/songList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="NestedScrolling" />
</ScrollView>
</LinearLayout>
See this you are wrong here
#Override
public int getCount() {
return 0;
}
Make it
#Override
public int getCount() {
return songs.size();
}
Remove these two methods also:
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
Explanation Adapter uses getCount() method to identify the size of List and recyclerview you are returning 0 here means you have 0 items in listview. What actually we need is return the total items size which is songs.size().
Hope this will give you a proper guidence.
Make following changes in your adapter class and check once
#Override
public int getCount() {
//return 0;
return songs.size();
}
Related
I am creating a custom list view with:
activity_main.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_display_list_view"
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.anandgupta.login.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/myListView" />
</RelativeLayout>
and list_row.xml containing only a TextView and a button.
<?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"
android:background="#drawable/list_row_selector"
android:padding="8dp">
<!-- Movie Title -->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/title"
android:textStyle="bold" />
<!-- Buy Ticket -->
<Button
android:id="#+id/buyTicket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Buy"
android:onClick="buyTicket" //Button calling buyTicket...Not working
/>
and MainActivity.java:
package com.anandgupta.login;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
private List<Movie> movieList = new ArrayList<Movie>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assume that MovieAdapter and Movie class is already created and its working fine.
MovieAdapter movieAdapter = new MovieAdapter(this,movieList);
listView = (ListView)findViewById(R.id.myListView);
listView.setAdapter(movieAdapter);
}
public void buyTicket() //Not working
{
Toast.makeText(MainActivity.this,"Ticket Booked",Toast.LENGTH_LONG).show();
}
}
and MovieAdapter.java:
package com.anandgupta.login;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MovieAdapter extends BaseAdapter {
private List<Movie> movieList;
private Activity activity;
private LayoutInflater layoutInflater;
public MovieAdapter(Activity activity, List<Movie> movieList) {
this.movieList = movieList;
this.activity = activity;
}
#Override
public Object getItem(int position) {
return movieList.get(position);
}
#Override
public int getCount() {
return movieList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(layoutInflater==null)
layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
convertView = layoutInflater.inflate(R.layout.list_row,null);
TextView title = (TextView)convertView.findViewById(R.id.title);
Movie m = movieList.get(position);
title.setText(m.getMovieTitle());
return convertView;
}
}
and Movie.java:
package com.anandgupta.login;
public class Movie {
private String title;
Movie()
{
}
public void setMovieTitle(String title)
{
this.title = title;
}
public void getMovieTitle(String title)
{
return title;
}
}
I am able to see the custom list but while trying to call the function buyTicket with the click of button my app get closed.
Any help will be appreciated.
Edit: The problem was that the method is not passed a view:
public void buyTicket(View view) { ....... }
This solve the above problem but now how to uniquely know which button from the list is clicked.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(layoutInflater==null)
layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
convertView = layoutInflater.inflate(R.layout.list_row,null);
TextView title = (TextView)convertView.findViewById(R.id.title);
Button buyTicketButton = (Button)convertView.findViewById(R.id.buyTicket);
Movie m = movieList.get(position);
title.setText(m.getMovieTitle());
buyTicketButton.setOnClickListener(new OnClickListener(){
//..... your code goes here
});
return convertView;
}
You can craete an Button Click event in the Adapter's getView() method.
Button btnBookTicket = (Button)convertView.findViewById(R.id.buyTicket);
btnBookTicket.setOnClickListener(new View.OnClickListener()
{
((MainACtivity)activity).buyMovieTicket();
});
Solved: I have created an adapter based on #JJV 's suggestion. I am aware that there is plenty of room for improvement, but it works for now.
I have updated this simplified version of my program, with the working code; I hope it will be useful to others:
MainActivity.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.Map;
import java.util.TreeMap;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Map<Integer, Object> m = new TreeMap<Integer, Object>();
int key = 123;
Item obj1 = new Item("abc", "xyz", 888);
m.put(key, obj1);
key = 456;
Item obj2 = new Item("def", "zyx", 999);
m.put(key, obj2);
ListAdapter adapter = new TreeMapAdapter(this, (TreeMap<Integer, Object>) m);
ListView itemListView = (ListView) findViewById(R.id.itemListView);
itemListView.setAdapter(adapter);
}
public class Item {
private String name;
private String thing;
private int number;
Item(String name, String thing, int number) {
this.name = name;
this.thing = thing;
this.number = number;
}
public String getName() {
return this.name;
}
public String getThing() {
return this.thing;
}
public int getNumber() {
return this.number;
}
}
}
main_activity.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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemListView" />
</LinearLayout>
listview_row.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_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/thing" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/number" />
</LinearLayout>
TreeMapAdapter.java:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.TreeMap;
public class TreeMapAdapter extends ArrayAdapter<String> {
private Context context;
private TreeMap<Integer, Object> treeMap;
private Integer[] mapKeys;
public TreeMapAdapter(Context context, TreeMap<Integer, Object> data) {
super(context, R.layout.listview_row);
this.context = context;
this.treeMap = data;
mapKeys = treeMap.keySet().toArray(new Integer[getCount()]);
}
public int getCount() {
return treeMap.size();
}
public String getItem(int position) {
return String.valueOf(treeMap.get(mapKeys[position]));
}
public long getItemId(int position) {
return mapKeys[position];
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater deviceInflater = LayoutInflater.from(getContext());
View listViewRow = deviceInflater.inflate(R.layout.listview_row, parent, false);
MainActivity.Item test = (MainActivity.Item) treeMap.get(mapKeys[position]);
String nameString = test.getName() + ", ";
String thingString = test.getThing() + ", ";
String numberInt = String.valueOf(test.getNumber());
TextView name = (TextView) listViewRow.findViewById(R.id.name);
TextView thing = (TextView) listViewRow.findViewById(R.id.thing);
TextView number = (TextView) listViewRow.findViewById(R.id.number);
name.setText(nameString);
thing.setText(thingString);
number.setText(numberInt);
return listViewRow;
}
}
Result:
I cannot embed a screenshot of the result in this post, because I do not have 10 reputation, but you can see the result of running the code here: http://i.stack.imgur.com/QoXX1.png
do something like this:
public class TreeMapAdapter extends ArrayAdapter<String> {
private Context context
private TreeMap<Integer, Object> treeMap;
private int mapKeys[];
public TreeMapAdapter(Context context,TreeMap<Integer, Object> treeMap)
this.context=context;
this.treeMao=treeMap;
mapKeys=treemap.keySet().toArray();
}
public int getCount() {
return treeMap.size();
}
public String getItem(int position) {
return treeMap.get(mapKeys[position]);
}
public long getItemId(int position) {
return mapKeys[position];
}
//your getView method....
}
So I have the following view
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutSessionItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/lblGroupDate"
style="#style/CustomText.GrayTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/lblCourseCode"
style="#style/CustomText.GrayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/lblSessionCode"
style="#style/CustomText.GrayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
So basically I get the following data from the database:
COURSE_CODE
SESSION_CODE
SESSION_DATE
I need to group the ListView items by SESSION_DATE, what I do is I keep a variable called "PreviousDate" and I compare it to the current SESSION_DATE, if it's different then I enable the header with the ID: "lblGroupDate" if the dates are the same I Hide "lblGroupDate".
Here's my Adapter:
public class SessionListAdapter extends BaseAdapter {
private Date PreviousDate = new Date();
static class ViewHolder {
TextView lblGroupDate;
TextView lblCourseCode;
TextView lblSessionCode;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.list_view_item, null);
holder = new ViewHolder();
holder.lblGroupDate = (TextView) convertView.findViewById(R.id.lblGroupDate);
holder.lblCourseCode = (TextView) convertView.findViewById(R.id.lblCourseCode);
holder.lblSessionCode = (TextView) convertView.findViewById(R.id.lblSessionCode);
} else {
holder = (ViewHolder)convertView.getTag();
}
SessionData session = (SessionData) getItem(position);
if(session != null) {
Date sessionDate = session.SESSION_DATE;
if (!mPrevDate.equals(sessionDate)) {
PreviousDate = sessionDate;
// HIDE GROUP HEADER
holder.lblGroupDate.setVisibility(View.GONE);
}
else {
// SHOW GROUP HEADER
holder.lblGroupDate.setVisibility(View.VISIBLE);
}
holder.lblCourseCode.setText(session.COURSE_CODE);
holder.lblSessionCode.setText(session.SESSION_CODE);
}
}
}
Here's the PROBLEM:
Lets say I have 20 records, when I scroll down and then scroll up again the rows that had the Group Header (lblGroupDate) enabled get shifted up without a header, it's the header was shifted to the next 3 rows. Why is this happening?
It's happening because getView() can be called at anytime and out-of-order from your data-set. You need to create a data-set that the adapter can be backed by. Just sorting your adapter out. Will update answer with code in a moment.
SessionListAdapter.java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class SessionListAdapter extends BaseAdapter
{
private static final int TYPE_GROUP = 0;
private static final int TYPE_SESSION = 1;
private static final int MAX_TYPES = 2;
private final LayoutInflater mLayoutInflater;
private ArrayList<SessionViewData> mData;
public SessionListAdapter(Context context, List<SessionData> sessionData)
{
mLayoutInflater = LayoutInflater.from(context);
updateSessionViewData(sessionData);
}
public void updateSessionViewData(List<SessionData> sessionData)
{
Date previousDate = new Date();
ArrayList<SessionViewData> data = new ArrayList<SessionViewData>();
for(SessionData session: sessionData){
if(!previousDate.equals(session.SESSION_DATE)){
data.add(new SessionViewData(TYPE_GROUP, session));
previousDate = session.SESSION_DATE;
}
data.add(new SessionViewData(TYPE_SESSION, session));
}
mData = data;
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
SessionViewData data = mData.get(position);
if(convertView == null){
final int layoutId;
switch(data.type){
case TYPE_GROUP: layoutId = R.layout.list_session_group; break;
case TYPE_SESSION: layoutId = R.layout.list_session_item; break;
default:
throw new IllegalArgumentException("Bad type for: " + data.session);
}
convertView = mLayoutInflater.inflate(layoutId, parent, false);
}
switch(data.type){
case TYPE_GROUP:
TextView lblGroupDate = ((TextView)convertView.findViewById(R.id.lblGroupDate));
lblGroupDate.setText(DateUtils.formatDateTime(convertView.getContext(),
data.session.SESSION_DATE.getTime(), DateUtils.FORMAT_SHOW_DATE));
break;
case TYPE_SESSION:
TextView lblCourseCode = (TextView)convertView.findViewById(R.id.lblCourseCode);
lblCourseCode.setText(data.session.COURSE_CODE);
TextView lblSessionCode = (TextView)convertView.findViewById(R.id.lblSessionCode);
lblSessionCode.setText(data.session.SESSION_CODE);
break;
}
return convertView;
}
#Override
public Object getItem(int position)
{
return mData.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public int getItemViewType(int position)
{
return mData.get(position).type;
}
#Override
public int getViewTypeCount()
{
return MAX_TYPES;
}
#Override
public int getCount()
{
return mData.size();
}
static class SessionViewData
{
int type;
SessionData session;
public SessionViewData(int type, SessionData session)
{
this.type = type;
this.session = session;
}
}
}
list_session_group.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lblGroupDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/CustomText.GrayTitle"
/>
list_session_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutSessionItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/lblCourseCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/CustomText.GrayText"
/>
<TextView
android:id="#+id/lblSessionCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/CustomText.GrayText"
/>
</LinearLayout>
I'm having a weird problem with my viewholders. I'm trying to create an activity that allows the user to choose multiple dates from datepicker dialog (this works) and then make a list of the chosen dates and add a timepicker for each of them. E.g 22-02-2014 14:00 (the 14:00 depicting a timepicker for that date). So if 5 dates were chosen, 5 rows would appear with each containing a chosen date and its own timepicker.
Currently creating the list from chosen dates works but timepicker won't appear no matter what I attempt. Only the textview containing the date is shown in the holder.
I've been searching for solution for several hours but I can't seem to find similar issues which indicates something is seriously wrong on my end. Just can't fathom what that might be.
The relevant code (omitting the activity itself since it works as supposed with list and all):
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.TimePicker;
public class RandomListAdapter extends ArrayAdapter<String> {
static Context mContext;
private static class ViewHolder {
private View row;
private TextView date;
private TimePicker tp;
public ViewHolder(View row) {
this.row = row;
}
public void setDate(String date) {
this.date.setText(date);
}
public void initTp(TimePicker tp){
this.tp = tp;
}
public void setHour(int h) {
tp.setCurrentHour(h);
}
public void setMin(int m){
tp.setCurrentMinute(m);
}
public TextView getDate() {
if(date==null) {
date = (TextView) row.findViewById(R.id.rr_date_item);
}
return date;
}
public TimePicker getTp() {
if(tp==null) {
tp = (TimePicker) row.findViewById(R.id.rr_tp);
}
return tp;
}
}
private static RandomListAdapter instance = null;
private ArrayList<String> items;
public RandomListAdapter(Context context) {
super(context, 0);
mContext = context;
items = new ArrayList<String>();
}
public void updateList(ArrayList<String> l){
items = l;
super.notifyDataSetChanged();
}
#Override
public int getCount() {
return items.size();
}
#Override
public String getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String s = (String)items.get(position);
ViewHolder vH = null;
if(convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.rr_row_layout, null);
vH = new ViewHolder(convertView);
vH.date = vH.getDate();
vH.tp = vH.getTp();
convertView.setTag(vH);
}
else {
vH = (ViewHolder) convertView.getTag();
}
if(vH != null && s != null) {
vH.setDate(s);
}
return convertView;
}
public static RandomListAdapter getInstance(Context mContext) {
if(instance == null) {
instance = new RandomListAdapter(mContext);
}
return instance;
}
}
The xml file:
<?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="?android:attr/listPreferredItemHeight"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:showDividers="middle"
android:divider="?android:attr/dividerHorizontal" >
<TextView
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:id="#+id/rr_date_item"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical|left"
android:ellipsize="end"
android:singleLine="true"
android:layout_weight="0"
android:textAlignment="gravity"
android:textSize="18sp" />
<TimePicker
android:id="#+id/rr_tp"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
Thanks in advance and my apologies if I'm missing something obvious.
This could be a problem
android:layout_height="0dip"
on your TimePicker. Change the height to wrap_content. Only the width needs to be 0dp when using weight.
I am trying to show some data in a ListView.
I created my own Adapter but it doesn't seem to be working.
I put a break point inside the method "getView(..)" but it never reached it.
I am probably missing something simple but can't figure it out.
package mpg.scoreControl;
import java.util.ArrayList;
import mpg.playerControl.MPGPlayer;
import mpg.playerControl.MPGPlayerControl;
import multiplayerGameControl.pkg.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SlidingDrawer;
import android.widget.ListView;
import android.widget.TextView;
public class MPGGameScore {
ArrayList<MPGGameScoreEntry> scores;
protected class MPGGameScoreEntry{
public String playerName;
public int playerScore;
public MPGGameScoreEntry(String playerName, int playerScore) {
this.playerName = playerName;
this.playerScore = playerScore;
}
}
private class GameScoreAdaptor extends BaseAdapter{
private LayoutInflater mInflater;
public GameScoreAdaptor(Context context) {
// searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.playerscoresrow, null);
holder = new ViewHolder();
holder.tvwPlayerName = (TextView) convertView.findViewById(R.id.tvwPlayerName);
holder.tvwPlayerScore = (TextView) convertView.findViewById(R.id.tvwPlayerScore);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvwPlayerName.setText(scores.get(position).playerName);
holder.tvwPlayerScore.setText(scores.get(position).playerScore);
return convertView;
}
}
static class ViewHolder {
TextView tvwPlayerName;
TextView tvwPlayerScore;
}
public void showCurrentScores(final Activity context, SlidingDrawer sd){
ListView lvwScores = (ListView) sd.findViewById(R.id.lvwScores);
// Build arraylist with scores.
scores = new ArrayList<MPGGameScoreEntry>();
// Now fill it up with rows
for (MPGPlayer player: MPGPlayerControl.getInstance().players)
scores.add(new MPGGameScoreEntry(player.playerName,player.playerDetails.playerScore.getScoreInt()));
lvwScores.setAdapter(new GameScoreAdaptor(context));
}
}
score_drawer:
<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer android:id="#+id/slidingDrawer" android:handle="#+id/drawerHandle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:content="#+id/contentLayout" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:visibility="visible">
<ImageView android:id="#+id/drawerHandle"
android:src="#drawable/blue_arrow_up_flat"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ImageView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contentLayout" android:gravity="center"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ListView
android:id="#+id/lvwScores"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:divider="#FFFFFF" android:dividerHeight="1dip"
android:layout_weight="1" android:layout_marginBottom="60dip"
android:footerDividersEnabled="true" android:headerDividersEnabled ="true"/>
</LinearLayout>
</SlidingDrawer>
playerscorerow:
<?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">
<TextView android:id="#+id/tvwPlayerName" android:layout_weight="1" android:gravity="left"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView android:id="#+id/tvwPlayerScore" android:layout_weight="1" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="12sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
You can't return 0 in the getCount() method because this will tell your adapter that you don't have any elements in the adapter:
#Override
public int getCount() {
return 24 ; // I just put a number here,if you plan to use the scores ArrayList as the data
// of the adapter you should return here scores.size();
}
Your adapter seems to be based on your scores list..so you do something like this:
#Override
public int getCount() {
return scores.size();
}
#Override
public Object getItem(int position) {
scores.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
BTW: The member variables (playerName & playerScore) of the Adapter class are probably not neccessay?!