Reputation: 1258
After several tries to get a custom adapter working I am almost there. Now I have one problem left: get data in the adapter. This is what I do.
I have a CustomListAdapter
class for the adapter, a sqLiteHelper
class to CRUD data and a mainActivity
class. In the mainActivity i load the customlist like this:
ListView list;
CustomListAdapter adapter;
public MainActivity CustomListView = null;
public ArrayList<Players> CustomListViewValuesArr = new ArrayList<Players>();
//button handlers newmatch_players_home
public void addSpelerNu(View button) {
Log.d("WedstrijdID buiten de create", ""+wedstrijdId);
Log.d("id return team thuis", ""+thuisTeamId);
//thuisteam
final EditText shirtNummer = (EditText) findViewById(R.id.shirtThuis);
String nummerShirt = shirtNummer.getText().toString();
int shirtSpeler = Integer.parseInt(nummerShirt);
final EditText spelerNaam = (EditText) findViewById(R.id.naamThuis);
String naamSpeler = spelerNaam.getText().toString();
Integer wedstrijdSpelerId = (int) (long) wedstrijdId;
SqLiteHelper db = new SqLiteHelper(this);
db.addSpeler(new Players(shirtSpeler, naamSpeler, thuisTeamId, wedstrijdSpelerId));
Log.d("Toevoegen speler THUIS", ">> BEGIN");
Log.d("Toevoegen speler", "Shirt = "+nummerShirt+" Naam = "+naamSpeler +" Team ="+thuisTeamId+" Wedstrijd ="+wedstrijdSpelerId);
Log.d("Toevoegen speler", ">> EIND");
shirtNummer.setText(null);
spelerNaam.setText(null);
CustomListView = this;
/******** Take some data in Arraylist ( CustomListViewValuesArr ) ***********/
ArrayList <Players> CustomListViewValuesArr = new ArrayList<Players>();
Resources res =getResources();
list= ( ListView )findViewById( R.id.listHome );
/**************** Create Custom Adapter *********/
adapter=new CustomListAdapter( CustomListView, CustomListViewValuesArr);
list.setAdapter( adapter );
}
This works except for the data. The data I want to use is in my sqlitehelper
public ArrayList<Players> getPlayersForTeam(int teamNumber,int matchNumber) {
ArrayList<Players> speler = new ArrayList<Players>();
String query = "SELECT * FROM " + TABLE_SPELERS + " WHERE team=? AND wedstrijd =?";
SQLiteDatabase db = this.getWritableDatabase();
String[] selectionArgs = new String[] { Integer.toString(teamNumber), Integer.toString(matchNumber) };
Cursor cursor = db.rawQuery(query, selectionArgs);
Players spelers = null;
if (cursor.moveToFirst()) {
do {
spelers = new Players();
spelers.setIdSpeler(Integer.parseInt(cursor.getString(0)));
spelers.setShirt(Integer.parseInt(cursor.getString(1)));
spelers.setSpeler(cursor.getString(2));
spelers.setTeam(Integer.parseInt(cursor.getString(3)));
spelers.setSpelerWedstrijd(Integer.parseInt(cursor.getString(4)));
speler.add(spelers);
} while (cursor.moveToNext());
}
Log.d("Alle spelers in DB for team:" + teamNumber, speler.toString());
// return wedstrijden
return speler;
}
If I load this data like this
List <Players> list = new ArrayList<Players>();
list=db.getPlayersForTeam(thuisTeamId,wedstrijdSpelerId);
I will see the log.d in logcat. But I just can't get that data into my CustomListViewValuesArr
.
What should I do?
This is the customListAdapter
class
package com.jd.wedstrijdkaart;
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.BaseAdapter;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
Players tempValues=null;
int i=0;
/************* CustomAdapter Constructor
* @return *****************/
public CustomListAdapter(Activity a, ArrayList d) {
/********** Take passed values **********/
activity = a;
data=d;
/*********** Layout inflator to call external xml layout () ***********/
inflater = ( LayoutInflater )activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
public TextView shirt;
public TextView name;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.adapter_players, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.shirt = (TextView) vi.findViewById(R.id.shirt);
holder.name=(TextView)vi.findViewById(R.id.name);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.name.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues = null;
tempValues = ( Players ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.shirt.setText( tempValues.getShirt() );
holder.name.setText( tempValues.getSpeler() );
}
return vi;
}
}
Upvotes: 0
Views: 3562
Reputation: 13932
Typically, when building custom adapters you want to pass your ArrayList into the constructor for your CustomListAdapter. It is relatively straight forward.
// Create a reference to your custom adapter as a Class Member and arraylist
CustomListAdapter mAdapter = null;
ArrayList<MyObjects> mArrayList = null;
// Initialize the adapter somewhere in onCreate
// After you have retrieved the data out of sql and created your custom object
mArrayList = new ArrayList<MyObjects>();
// Fill your array list with data
// ******* THIS IS WHERE YOU WANT TO RETRIEVE THE ITEM FROM SQLITE
// Then Each time your get a new cursor object
// You should create a new MyObject object and add it to the ArrayList
// Intialize the custom list adapter using the context, and your array list
mAdpater = new CustomListAdapter(getBaseContext(), mArrayList);
// Set ListView adapter
listView.setAdapter(mAdapter);
Then Create the Custom Adapter And use the constructor to pass the data
// Custom Adapter Class
public class CustomListAdapter extends BaseAdapter {
private ArrayList<MyObjects> mObjects = null;
private LayoutInflater mInflater = null;
// Private class to hold information about the row content UI Items
private class RowContent{
// Reference the UI Widgets for each row
TextView mTextView;
// etc.
}
// Constructor
public CustomListAdapter(Context context, ArrayList<MyObject> objects){
this.mInflater = LayoutInflater.from(context);
this.mObjects = objects;
}
// --------------------------------------------------
// BaseAdapter Overrides
// --------------------------------------------------
// Returns the count of your arrayList
@Override
public int getCount() {
int count = 0;
if(mObjects!=null && mObjects.length() >= 1){
count = mObjects.length();
}
return count;
}
// Returns object at position
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mOjbects[position];
}
// returns position
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
// This is where all the magic happens
// Creates a new row for each item in your arraylist
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowContent theRow;
// If ConvertVIew is null
if(convertView == null){
// Create a reference to the class holding the UI Elements
theRow= new RowContent ();
// set the view using your XML layout for a custom list item
convertView = mInflater.inflate(R.layout.custom_list_item, null);
// reference the UI widgets in the XML by their ID's
theRow.mTextView = (TextView) convertView.findViewById(R.id.textView);
// set the Tag for the View
convertView.setTag(theRow);
}else{
// If there is already an item, recycle it
theRow= (RowContent) convertView.getTag();
}
// Set the Text or arrays For UI Widgets
theRow.mtextView.setText(mObjects.get(position).text);
// return the view
return convertView;
}
}
Upvotes: 0
Reputation: 1789
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
is totally wrong. Here is where you should return the objects at the specified position in your ArrayList.
Upvotes: 1
Reputation: 95578
In onCreate()
you do this:
ArrayList <Players> CustomListViewValuesArr = new ArrayList<Players>();
...
adapter=new CustomListAdapter( CustomListView, CustomListViewValuesArr);
You are initializing your adapter with an empty list of items (CustomListViewValuesArr
is empty).
You have 2 choices:
You can get the data from your database and pass it to the adapter when you initialize it.
You can initialize the adapter with an empty array and then later pass the data to the adapter. To do that you'll need to provide a setter method in the adapter, something like this:
public void setData(ArrayList d) { data = d; // Tell the views that the data has changed so they can refresh notifyDataSetChanged(); }
Upvotes: 1