我正在尝试创建一个基本的列表视图应用程序。我让它在默认的系统阵列适配器上运行良好。然后我尝试修改它以允许我使用自定义适配器。不幸的是我遇到了一个问题。
当我运行应用程序时,我没有遇到运行时错误,而是出现黑屏,应该显示列表,这让我认为它可能正在显示列表,但由于某种原因适配器没有找到数据....
无论如何,下面是我的列表活动:
package com.example.waitronproto2;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class AppetizersActivity extends ListActivity{
//Instance Variables
//Array to hold menu appetizers - In future this will be pulled from web server
private List<MenuItem> appetizers;
private MenuItemArrayAdapter appetizersAdapter;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Garbage collection
System.gc();
setContentView(R.layout.appetizers_list);
fillList();
//Create custom array adapter - links list with source of data, an array.
appetizersAdapter = new MenuItemArrayAdapter(this, appetizers);
//Set the newly created adapter as the lists adapter.
this.setListAdapter(appetizersAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
//Toast for testing
Toast.makeText(this.getApplicationContext(), l.getItemAtPosition(position) + " Clicked!", Toast.LENGTH_LONG).show();
//store clicked item in String variable
String choice = l.getItemAtPosition(position).toString();
//add to an array that is being passed from Activity to Activity
}
//fill the list with menu items
private void fillList(){
appetizers = new ArrayList<MenuItem>();
appetizers.add(new MenuItem("Appetizer 1", "Appetizer", 8.95));
appetizers.add(new MenuItem("Appetizer 2", "Appetizer", 9.95));
appetizers.add(new MenuItem("Appetizer 3", "Appetizer", 12.95));
appetizers.add(new MenuItem("Appetizer 4", "Appetizer", 4.95));
appetizers.add(new MenuItem("Appetizer 5", "Appetizer", 5.95));
appetizers.add(new MenuItem("Appetizer 6", "Appetizer", 7.95));
appetizers.add(new MenuItem("Appetizer 7", "Appetizer", 5.95));
appetizers.add(new MenuItem("Appetizer 8", "Appetizer", 12.95));
appetizers.add(new MenuItem("Appetizer 9", "Appetizer", 10.95));
}
}
这是我的适配器类:
package com.example.waitronproto2;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
//Default array adapter for menu items
public class MenuItemArrayAdapter extends ArrayAdapter<MenuItem> {
private List<MenuItem> menuItems;
//Constructor
public MenuItemArrayAdapter(Context context, List<MenuItem> menuItems) {
super(context, R.layout.menuitem_row);
this.menuItems = menuItems;
}
//get views
public View getView(final int position, View convertView, ViewGroup parent){
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.menuitem_row, null);
}
//assign values to view
MenuItem item = this.menuItems.get(position);
TextView nameView = (TextView) v.findViewById(R.id.item_name);
TextView priceView = (TextView) v.findViewById(R.id.item_price);
nameView.setText(item.getName());
priceView.setText(String.valueOf(item.getPrice()));
return v;
}
}
有什么想法我可能会出错吗?
非常感谢!