经过多次尝试让自定义适配器工作后,我几乎就在那里。现在我还有一个问题:在适配器中获取数据。这就是我的工作。
我有适配器的CustomListAdapter
类,CRUD数据的sqLiteHelper
类和mainActivity
类。在mainActivity中我加载自定义列表,如下所示:
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 );
}
除数据外,此方法有效。我想要使用的数据位于我的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;
}
如果我像这样加载这些数据
List <Players> list = new ArrayList<Players>();
list=db.getPlayersForTeam(thuisTeamId,wedstrijdSpelerId);
我将在logcat中看到log.d。但我无法将这些数据存入我的CustomListViewValuesArr
。
我该怎么办?
这是customListAdapter
类
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;
}
}
答案 0 :(得分:1)
在onCreate()
中执行此操作:
ArrayList <Players> CustomListViewValuesArr = new ArrayList<Players>();
...
adapter=new CustomListAdapter( CustomListView, CustomListViewValuesArr);
您正在使用空的项目列表初始化您的适配器(CustomListViewValuesArr
为空)。
您有两个选择:
您可以从数据库中获取数据,并在初始化时将其传递给适配器。
您可以使用空数组初始化适配器,然后将数据传递给适配器。要做到这一点,你需要在适配器中提供一个setter方法,如下所示:
public void setData(ArrayList d){ data = d; //告诉视图数据已更改,以便他们可以刷新 notifyDataSetChanged(); }
答案 1 :(得分:1)
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
完全错了。这里应该返回ArrayList中指定位置的对象。
答案 2 :(得分:0)
通常,在构建自定义适配器时,您希望将ArrayList传递给CustomListAdapter的构造函数。这是相对直接的。
// 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);
然后创建自定义适配器并使用构造函数传递数据
// 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;
}
}