Android 自定义适配器中未定义构造函数
我尝试在代码中获取自定义适配器。我遵循了一个教程,但有点不对劲 这是我的CustomListAdapter类:Android 自定义适配器中未定义构造函数,android,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
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;
}
}
在我的主要活动中,我会这样做:
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 );
}
ListView列表;
自定义列表适配器;
public main活动CustomListView=null;
public ArrayList CustomListViewValuesArrayList=新ArrayList();
//按钮处理程序newmatch\u players\u home
公共void addSpelerNu(查看按钮){
Log.d(“WedstrijdID buiten de create”,“”+WedstrijdID);
Log.d(“id返回团队thuis”,“”+thuisTeamId);
//图伊斯坦
最终EditText-shirtNummer=(EditText)findViewById(R.id.shirtThuis);
字符串numershirt=shirtnumer.getText().toString();
int shirtSpeler=Integer.parseInt(numershirt);
最终编辑文本spelerNaam=(编辑文本)findViewById(R.id.naamThuis);
字符串naamSpeler=spelerNaam.getText().toString();
整数wedstrijdSpelerId=(int)(long)wedstrijdId;
SqLiteHelper db=newsqlitehelper(this);
db.addSpeler(新玩家(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”);
shirtnumer.setText(null);
spelerNaam.setText(null);
CustomListView=这个;
/********在Arraylist(CustomListViewValuesArrayList)中获取一些数据***********/
List CustomListViewValuesArrayList=新建ArrayList();
Resources res=getResources();
列表=(ListView)findViewById(R.id.listHome);
/****************创建自定义适配器*********/
适配器=新的CustomListAdapter(CustomListView、CustomListViewValuesR、res);
list.setAdapter(适配器);
}
但在eclipse中,我一直看到以下警告:
构造函数CustomListAdapter未定义
那么,我忘记为这个自定义适配器使用什么呢?问题是因为您的阵列列表
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);
}
私有数组列表数据;
public void CustomAdapter(活动a、ArrayListd、资源resLocal){
活动=a;
数据=d;
res=resLocal;
/***********布局充气器调用外部xml布局()***********/
充气器=(充气器)活动。
getSystemService(上下文布局\充气机\服务);
}
同时将CustomAdapter更改为CustomListAdapter问题是因为您的阵列列表
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);
}
私有数组列表数据;
public void CustomAdapter(活动a、ArrayListd、资源resLocal){
活动=a;
数据=d;
res=resLocal;
/***********布局充气器调用外部xml布局()***********/
充气器=(充气器)活动。
getSystemService(上下文布局\充气机\服务);
}
同时将CustomAdapter更改为CustomListAdapter请参见更改,
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);
}
请参见更改,
CustomAdapter
应为customlistapter
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);
}
hi而不是使用
CustomListView = this;
请使用
CustomListView = YoursMainActivity.this;
或者Application.getContext(),这些问题基本上是因为没有正确获取当前上下文。hi而不是使用
CustomListView = this;
请使用
CustomListView = YoursMainActivity.this;
或者Application.getContext(),这些问题基本上是因为没有正确获取当前上下文。将
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) {
}
将
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) {
}
CustomListAdapter
而不是CustomAdapter
,并删除void
。thnx!我在自定义视图中获得“无数据”。因此,自定义视图正在工作:)CustomListAdapter
而不是CustomAdapter
,并删除void
。thnx!我在自定义视图中获得“无数据”。因此,自定义视图正在工作:)