我正在尝试扩展BaseAdapter
以进行自定义listview
。但它说:
在getCount()方法中方法调用expected`。
我知道它是一个数组,所以如何获取它?我基本上是这个领域的一个人。这是我尝试申请的代码:
public class customAdapter extends BaseAdapter implements View.OnClickListener {
private Context context;
private String[] restaurant;
private String[] address;
private String[] time;
private static LayoutInflater layoutInflater = null;
public Resources res;
int image[];
ListModel tempvalues = null;
int i=0;
public customAdapter(MainActivity mainActivity, String[] rest, String[] add,String[] tim,int[] img){
restaurant = rest;
context=mainActivity;
address= add;
time = tim;
image = img;
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return restaurant.length(); // Method call expected.
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder{
TextView resname;
TextView resadd;
TextView restim;
ImageView imageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowview;
rowview = layoutInflater.inflate(R.layout.custom_list_view_for_restaurant_names,null);
holder.resname = (TextView)rowview.findViewById(R.id.resnameTextView);
holder.resadd = (TextView)rowview.findViewById(R.id.address_textview);
holder.restim = (TextView)rowview.findViewById(R.id.time_textview);
holder.imageView = (ImageView)rowview.findViewById(R.id.imageView);
holder.resname.setText(restaurant[position]);
holder.resadd.setText(address[position]);
holder.restim.setText(time[position]);
holder.imageView.setImageResource(image[position]);
rowview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return rowview;
}
@Override
public void onClick(View v) {
}
}
答案 0 :(得分:2)
更改
return restaurant.length();
到
return restaurant.length;
餐厅是Array
而不是List
还有一件事我想建议你,
不要单独传递这些数组,因为如果某些数组大小小于restaurant数组,那么逻辑上会破坏代码。
我建议你使用模型,
例如:
public class Event {
private String restaurant;
private String address;
private String time;
private int image;
//getters and setters
}
并传递Event
s
如下所示,
private List<Event> events;
public customAdapter(MainActivity mainActivity, List<Event> events){
this.events = events;
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return events.size(); // Method call expected.
}
@Override
public Object getItem(int position) {
return events.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
并访问,
Event event = events.get(position);
holder.resname.setText(event.getResturant());
holder.resadd.setText(event.getAddress());
holder.restim.setText(event.getTime());
holder.imageView.setImageResource(event.getImage());
而且,
在重复使用已经膨胀的元素时,不应该给ListView
元素充气。
View view = convertView;
if (view == null) {
// if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list_view_for_restaurant_names,null);
}