构造函数在自定义适配器中未定义

时间:2013-12-19 11:02:01

标签: android

我尝试在我的代码中获得自定义适配器。我按照教程,但有一些错误。

这是我的CustomListAdapter类:

package com.jd.wedstrijdkaart;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
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;
             public Resources res;
             Players tempValues=null;
             int i=0;

             /*************  CustomAdapter Constructor 
             * @return *****************/
             public void CustomAdapter(Activity a, ArrayList d,Resources resLocal) {

                    /********** Take passed values **********/
                     activity = a;
                     data=d;
                     res = resLocal;

                     /***********  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() );


                      /******** Set Item Click Listner for LayoutInflater for each row *******/


                 }
                 return vi;
             }                

         }

我的球员课程:

package com.jd.wedstrijdkaart;

import android.util.Log;

public class Players


     {
     private int shirtNummer;
        private String spelerNaam;
        private int spelerTeam;
        private int wedstrijdSpeler;
        private int idSpeler;


        public Players(){}

        public Players(int shirtNummer, String spelerNaam, int spelerTeam, int wedstrijdSpeler) {
            super();
            this.shirtNummer = shirtNummer;
            this.spelerNaam = spelerNaam;
            this.spelerTeam = spelerTeam;
            this.wedstrijdSpeler = wedstrijdSpeler;
        }
        //getters & setters

        public int getIdSpeler() {
         return idSpeler;
 }
 public void setIdSpeler(int idSpeler) {
         this.idSpeler = idSpeler;
 }
        public int getShirt() {
            return shirtNummer;
      }
      public void setShirt(int shirtNummer) {
            this.shirtNummer = shirtNummer;
      } 
      public String getSpeler() {
            return spelerNaam;
      }
      public void setSpeler(String spelerNaam) {
            this.spelerNaam = spelerNaam;
      } 
      public int getTeam() {
            return spelerTeam;
      }
      public void setTeam(int spelerTeam) {
            this.spelerTeam = spelerTeam;
      }
      public int getSpelerWedstrijd() {
            return wedstrijdSpeler;
      }
      public void setSpelerWedstrijd(int wedstrijdSpeler) {
            this.wedstrijdSpeler = wedstrijdSpeler;
      }

    //strings
      @Override
            public String toString() {
            Log.d("Speler "+spelerNaam,"Team=" + spelerTeam + ", Shirt=" + shirtNummer+", wedstrijd=" + wedstrijdSpeler);
            return "[id=" + idSpeler + ", shirt="+shirtNummer+", naam=" + spelerNaam + ", team=" + spelerTeam+ ", wedstrijd=" + wedstrijdSpeler
                    + "]";
        }
}

在我的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 ) ***********/

         List <Players> CustomListViewValuesArr = new ArrayList<Players>();    

         Resources res =getResources();
         list=  ( ListView )findViewById( R.id.listHome );  

         /**************** Create Custom Adapter *********/
         adapter=new CustomListAdapter( CustomListView, CustomListViewValuesArr,res);
         list.setAdapter( adapter );

    }

但是在eclipse中我一直看到这个警告:

  

构造函数CustomListAdapter未定义

那么我忘了使用这个自定义适配器是什么?

4 个答案:

答案 0 :(得分:2)

请参阅更改,CustomAdapter应为CustomListAdapter

 public void CustomListAdapter(Activity a, ArrayList d,Resources resLocal) {

                        /********** Take passed values **********/
                         activity = a;
                         data=d;
                         res = resLocal;

                         /***********  Layout inflator to call external xml layout () ***********/
                          inflater = ( LayoutInflater )activity.
                                                      getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                 }

答案 1 :(得分:1)

问题在于你的 ArrayList&lt;播放器与GT;

所以改变:

 private ArrayList< Players>data;
 public void CustomAdapter(Activity a, ArrayList < Players> d,Resources resLocal) {


                 activity = a;
                 data=d;
                 res = resLocal;

                 /***********  Layout inflator to call external xml layout () ***********/
                  inflater = ( LayoutInflater )activity.
                                              getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         }

同时将 CustomAdapter 更改为 CustomListAdapter

答案 2 :(得分:1)

嗨而不是使用

CustomListView = this; 

请使用

CustomListView = YoursMainActivity.this;

或Application.getContext(),这些问题基本上是因为没有正确获取当前上下文。

答案 3 :(得分:1)

CustomAdapter更改为CustomListAdapter并删除void因为构造函数永远不会有返回类型声明

 public CustomListAdapterActivity a, ArrayList d,Resources resLocal) {

                    /********** Take passed values **********/
                     activity = a;
                     data=d;
                     res = resLocal;

                     /***********  Layout inflator to call external xml layout () ***********/
                      inflater = ( LayoutInflater )activity.
                                                  getSystemService(Context.LAYOUT_INFLATER_SERVICE);

             }ic void CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
}