非常感谢任何帮助或暗示下一步。
我正在尝试以编程方式更改ListView中行的内容。在一行中有3个TextView和一个ProgressBar。如果当前行的'result'列为零,我想为ProgressBar设置动画。
在阅读了一些教程和文档之后,我得出的结论是必须使用LayoutInflater并且getView()
- 覆盖。也许我错了。
如果我从函数返回row = inflater.inflate(R.layout.row, null);
,它会给出NullPointerException。
以下是代码:
private final class mySimpleCursorAdapter extends SimpleCursorAdapter {
private Cursor localCursor;
private Context localContext;
public mySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.localCursor = c;
this.localContext = context;
}
/**
* 1. ListView asks adapter "give me a view" (getView) for each item of the list
* 2. A new View is returned and displayed
*/
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater)localContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String result = localCursor.getString(2);
int resInt = Integer.parseInt(result);
Log.d(TAG, "row " + row);
// if 'result' column form the TABLE is 0, do something useful:
if(resInt == 0) {
ProgressBar progress = (ProgressBar) row.findViewById(R.id.update_progress);
progress.setIndeterminate(true);
TextView edit1 = (TextView)row.findViewById(R.id.row_id);
TextView edit2 = (TextView)row.findViewById(R.id.request);
TextView edit3 = (TextView)row.findViewById(R.id.result);
edit1.setText("1");
edit2.setText("2");
edit3.setText("3");
row = inflater.inflate(R.layout.row, null);
}
return row;
}
这是Stack Trace:
03-08 03:15:29.639: ERROR/AndroidRuntime(619): java.lang.NullPointerException
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:149)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at com.dhristov.test1.test1$mySimpleCursorAdapter.getView(test1.java:105)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.AbsListView.obtainView(AbsListView.java:1256)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.ListView.makeAndAddView(ListView.java:1668)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.ListView.fillDown(ListView.java:637)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.ListView.fillSpecific(ListView.java:1224)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.ListView.layoutChildren(ListView.java:1499)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.AbsListView.onLayout(AbsListView.java:1113)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.view.View.layout(View.java:6830)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.view.ViewRoot.performTraversals(ViewRoot.java:996)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.os.Looper.loop(Looper.java:123)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at java.lang.reflect.Method.invoke(Method.java:521)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-08 03:15:29.639: ERROR/AndroidRuntime(619): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:19)
对于CursorAdapter
和子类,您应该覆盖newView()
和bindView()
而不是getView()
。
更重要的是,你不应该打电话给super.getView()
。那是你崩溃的地方。
答案 1 :(得分:2)
扩展getView()
课程时无需SimpleCursorAdapter
。我们从覆盖getView()
和newView()
方法中获得bindView()
的所有功能。
if (convertview == null) // is equal to `newView()` and
if (convertview != null) // is equal to `bindView()`
一个主要区别在于getView()
;我们将该位置作为参数,在simpleCursorAdapter
中我们得到getPositionForView(view)
的位置
view是bindView()
的参数。
示例:我们通常使用Listview
来设置值...因此您调用ListViewObj.getPositionForView(view)
答案 2 :(得分:1)
实际上您应该使用ViewBinder来实现自定义绑定。这样您就不必覆盖任何代码。只需确保在设置视图值后返回 true ,否则适配器将覆盖该值。
public void refresh() {
Cursor cursor = ....get cursor....
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.list_item,
cursor,
new String[] { KEY_NAME, KEY_SHORT_DESC},
new int[] { R.id.icon, R.id.text1});
adapter.setViewBinder(new ProductViewBinder());
setAdapter(adapter);
}
private static class ProductViewBinder implements ViewBinder {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view instanceof ProgressBar) {
String result = cursor.getString(2);
Int resInt = Int.parseInt(result);
if (resInt == 0) {
((ProgressBar)view).setIndeterminate(true);
return true;
}
}
return false;
}
}