2013-12-19 89 views
1

經過多次嘗試獲得自定義適配器的工作我幾乎在那裏。現在我還有一個問題:在適配器中獲取數據。這就是我所做的。如何將數據發送到我的自定義適配器

我有一個CustomListAdapter類的適配器,sqLiteHelper類CRUD數據和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

你可以發佈整個addSpelerNu方法或更好的整個活動類 – Gooziec

+0

完成。我使用addSpelerNu作爲按鈕 –

+0

的onClick事件,因爲我可以看到你只是在適配器的構造函數 – Gooziec

回答

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

}

1

onCreate()你這樣做:

ArrayList <Players> CustomListViewValuesArr = new ArrayList<Players>(); 
... 
adapter=new CustomListAdapter(CustomListView, CustomListViewValuesArr); 

要初始化您的適配器與一個空的項目列表(CustomListViewValuesArr爲空)。

你有兩個選擇:

  1. 你可以從你的數據庫中的數據,並把它傳遞給適配器時初始化它。

  2. 您可以使用空數組初始化適配器,然後再將數據傳遞給適配器。爲此,您需要在適配器中提供setter方法,如下所示:

    public void setData(ArrayList d){ data = d; //告訴數據已更改的意見,以便刷新 notifyDataSetChanged(); }

1
public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

是完全錯誤的。這裏是你應該返回ArrayList中指定位置的對象的地方。

相關問題