我尝试使用此示例创建自己的列表视图http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for.html 我还检查了我的提要和消息类是否正常工作。
customadapter.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
CustomAdapter.java
package ***;
import java.util.ArrayList;
import ***;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
private ArrayList<RssMessage> _data;
Context _c;
CustomAdapter(ArrayList<RssMessage> data, Context c) {
_data = data;
_c = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.w("here","here");
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) _c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.customadapter, null);
}
TextView fromView = (TextView) v.findViewById(R.id.textView2);
TextView subView = (TextView) v.findViewById(R.id.textView1);
RssMessage msg = _data.get(position);
fromView.setText(msg.getTitle());
subView.setText(msg.getPUBDATE());
return v;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return _data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
自定义适配器的使用
ListView lv=(ListView)findViewById(R.id.listView1);
lv.setAdapter(new CustomAdapter(feed.getMessages() , this));
日志
05-31 18:29:23.112: E/AndroidRuntime(1227): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.ayratbadykov.rssunion/ru.ayratbadykov.rssunion.ArticleListActivity}: java.lang.NullPointerException
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.os.Looper.loop(Looper.java:130)
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-31 18:29:23.112: E/AndroidRuntime(1227): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 18:29:23.112: E/AndroidRuntime(1227): at java.lang.reflect.Method.invoke(Method.java:507)
05-31 18:29:23.112: E/AndroidRuntime(1227): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-31 18:29:23.112: E/AndroidRuntime(1227): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-31 18:29:23.112: E/AndroidRuntime(1227): at dalvik.system.NativeStart.main(Native Method)
05-31 18:29:23.112: E/AndroidRuntime(1227): Caused by: java.lang.NullPointerException
05-31 18:29:23.112: E/AndroidRuntime(1227): at ru.ayratbadykov.rssunion.ArticleListActivity.onCreate(ArticleListActivity.java:55)
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-31 18:29:23.112: E/AndroidRuntime(1227): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-31 18:29:23.112: E/AndroidRuntime(1227): ... 11 more