Note that there are some explanatory texts on larger screens.

plurals
  1. POListview with custom adapter won't work inside a SherlockFragment
    text
    copied!<p>Adapter:</p> <pre><code>public class ServerAdapter extends ArrayAdapter&lt;Server&gt; { private Context Context; private int ResourceId; private List&lt;Server&gt; Data = null; public ServerAdapter(Context context, int layoutResourceId, List&lt;Server&gt; data) { super(context, layoutResourceId, data); ResourceId = layoutResourceId; Context = context; Data = data; } public void updateServersList(List&lt;Server&gt; 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; } </code></pre> <p>SherlockFragment:</p> <pre><code>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; } } </code></pre> <p>servers.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/serverlistview" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;/ListView&gt; </code></pre> <p>server.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;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" &gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; &lt;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" &gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Errors:</p> <blockquote> <p>07-30 13:11:38.824: E/AndroidRuntime(31423): FATAL EXCEPTION: main 07-30 13:11:38.824: E/AndroidRuntime(31423): java.lang.NullPointerException 07-30 13:11:38.824: E/AndroidRuntime(31423): at com.example.app.ServerAdapter.getView(ServerAdapter.java:47) 07-30 13:11:38.824: E/AndroidRuntime(31423): at android.widget.AbsListView.obtainView(AbsListView.java:2159) 07-30 13:11:38.824: E/AndroidRuntime(31423): at android.widget.ListView.makeAndAddView(ListView.java:1831) ...</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload