适配器:
public class ServerAdapter extends ArrayAdapter<Server>
{
private Context Context;
private int ResourceId;
private List<Server> Data = null;
public ServerAdapter(Context context, int layoutResourceId, List<Server> data)
{
super(context, layoutResourceId, data);
ResourceId = layoutResourceId;
Context = context;
Data = data;
}
public void updateServersList(List<Server> newlist)
{
Data.clear();
Data = newlist;
this.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View Row = convertView;
ViewHolder Holder = null;
Server server = Data.get(position);
if(Row == null)
{
LayoutInflater inflater = ((Activity)Context).getLayoutInflater();
Row = inflater.inflate(ResourceId, parent, false);
Holder = new ViewHolder();
Holder.Hostname = (TextView)convertView.findViewById(R.id.hostname);
Holder.Address = (TextView)convertView.findViewById(R.id.address);
Holder.Players = (TextView)convertView.findViewById(R.id.players);
Holder.Photo = (ImageView) convertView.findViewById(R.id.imageView1);
Row.setTag(Holder);
}
else
{
Holder = (ViewHolder) Row.getTag();
}
Holder.Hostname.setText(server.getHostname());
Holder.Address.setText("Address: " + server.getIp() + ":" + server.getPort());
Holder.Players.setText("Players: " + server.getPlayers() + "/" + server.getMaxPlayers());
Holder.Photo.setImageResource(server.getGameObject().getImageResId());
return Row;
}
}
class ViewHolder
{
TextView Hostname;
TextView Address;
TextView Players;
ImageView Photo;
}
夏洛克片段:
public class Favourites extends SherlockFragment
{
public static ListView FavoritesListView;
public static ServerAdapter FavoritesAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.servers, null);
FavoritesListView = (ListView) v.findViewById(R.id.serverlistview);
FavoritesAdapter = new ServerAdapter(this.getSherlockActivity(), R.layout.server, DataHandler.getUserFavoriteServersFromLocal());
FavoritesListView.setAdapter(FavoritesAdapter);
return v;
}
}
服务器.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/serverlistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
服务器.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="55dp"
android:layout_height="59dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="6dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginTop="4dp"
android:contentDescription="@string/game"
android:src="@drawable/game" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_toRightOf="@+id/RelativeLayout1"
android:orientation="vertical" >
<TextView
android:id="@+id/hostname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:paddingLeft="5dp"
android:text="@string/hostname"
android:textSize="18sp"
android:textStyle="bold"
android:ellipsize="end"
android:singleLine="true" />
</RelativeLayout>
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/relativeLayout2"
android:layout_alignParentRight="true"
android:layout_below="@+id/relativeLayout2"
android:paddingLeft="5dp"
android:text="@string/address" />
<TextView
android:id="@+id/players"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/address"
android:layout_alignParentRight="true"
android:layout_below="@+id/address"
android:paddingLeft="5dp"
android:text="@string/players" />
</RelativeLayout>
错误:
07-30 13:11:38.824:E/AndroidRuntime(31423):致命异常:主要 07-30 13:11:38.824:E/AndroidRuntime(31423):java.lang.NullPointerException 07-30 13:11:38.824: E/AndroidRuntime(31423): 在 com.example.app.ServerAdapter.getView(ServerAdapter.java:47) 07-30 13:11:38.824: E/AndroidRuntime(31423): 在 android.widget.AbsListView.obtainView(AbsListView .java:2159) 07-30 13:11:38.824: E/AndroidRuntime(31423): 在 android.widget.ListView.makeAndAddView(ListView.java:1831) ...