actually I've read some previous questions about this...
this is the code that I use
auto = (ListView)findViewById(R.id.auto);
String[] projection = new String[] {Browser.BookmarkColumns._ID,Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL};
String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL};
int[] displayViews = new int[] { R.id.text1,R.id.text2 };
Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI,projection, null, null, null);
//auto.setAdapter(new SimpleCursorAdapter(this, R.layout.mylist, cur,displayFields, displayViews));
myAdapter apt = new myAdapter(this, R.layout.mylist, cur,displayFields, displayViews);
auto.setAdapter(apt);
and class myAdapter
class myAdapter extends SimpleCursorAdapter{
private Cursor c;
private Context context;
public myAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
this.c = c;
this.context = context;
AutoList att = new AutoList();
mListView = att.auto;
}
@Override
public View getView(int pos, View inView, ViewGroup parent) {
View vix = inView;
if (vix == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vix = inflater.inflate(R.layout.mylist, null);
}
this.c.moveToPosition(pos);
String title = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.TITLE));
String cont = this.c.getString(this.c.getColumnIndex(Browser.BookmarkColumns.URL));
TextView text1 = (TextView) vix.findViewById(R.id.text1);
text1.setText(title);
TextView text2 = (TextView) vix.findViewById(R.id.text2);
text2.setText(cont);
Button butt = (Button) vix.findViewById(R.id.button);
butt.setOnClickListener(mButt);
return vix;
}
private OnClickListener mButt = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = mListView.getPositionForView((View) v.getParent());
Log.v("BUTT", "Title clicked, row "+position);
}
};
However, when I click the button, I still get a lot of errors like this:
04-10 22:30:55.152: ERROR/AndroidRuntime(695): FATAL EXCEPTION: main
04-10 22:30:55.152: ERROR/AndroidRuntime(695): java.lang.NullPointerException
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.auto2.AutoList$myAdapter$1.onClick(AutoList.java:113)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.view.View.performClick(View.java:2408)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.view.View$PerformClick.run(View.java:8816)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Handler.handleCallback(Handler.java:587)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.os.Looper.loop(Looper.java:123)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at java.lang.reflect.Method.invoke(Method.java:521)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-10 22:30:55.152: ERROR/AndroidRuntime(695): at dalvik.system.NativeStart.main(Native Method)
That's it!
I hope it won't be so difficult to be figured out!
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…