我正在尝试使 GridView 代替 Listview 工作。该代码使用 ListActivity,如下所示:
public class BorrowerList extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final int ACTIVITY_PERSON_UPCLOSE = 3;
private SimpleCursorAdapter adapter;
// -----------------------------------------------------------
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view_person_record);
this.getListView().setDividerHeight(2);
fillData();
}
// -----------------------------------------------------------
private void fillData() {
// TODO Auto-generated method stub
String[] from = new String[] { BorrowMeTable.COLUMN_NAME };
int[] to = new int[] { R.id.pername};
getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.grid_view_rowitem, null, from,
to, 0);
setListAdapter(adapter);
}
// -----------------------------------------------------------
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
//fillData();
}
// -----------------------------------------------------------
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Remove to show all records
//String mSelection = BorrowMeTable.COLUMN_NAME + "=?"; // SQL request
//String[] selectionArgs = {"Jack Reacher"}; // The actual argument
BorrowMeContentProvider.distinctSwitch = true;
String[] projection = { BorrowMeTable.COLUMN_ID, BorrowMeTable.COLUMN_NAME };
CursorLoader cursorLoader = new CursorLoader(this,
BorrowMeContentProvider.CONTENT_URI, projection, null, null, null);
return cursorLoader;
}
// -----------------------------------------------------------
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
// TODO Auto-generated method stub
adapter.swapCursor(arg1);
}
// -----------------------------------------------------------
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
adapter.swapCursor(null);
}
// -----------------------------------------------------------
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// When Clicked - loads record ID and sends it to PersonRecord class
Intent i = new Intent(this, PersonRecord.class);
// Sends a URI request to the content provider saying we are looking for a
// particular record
Uri borrowUri = Uri.parse(BorrowMeContentProvider.CONTENT_URI + "/" + id);
i.putExtra(BorrowMeContentProvider.CONTENT_ITEM_TYPE, borrowUri);
// Activity returns an result if called with startActivityForResult
// startActivityForResult(i, ACTIVITY_PERSON_UPCLOSE);
// Removed the for Result as it would not allow the previous activity
// to release, so it would live people on the list even if all records had been deleted
startActivity(i);
}
}
我在 stackoverflow 上问了一个先前的问题,我得到了一个简单的“用 GridView 替换 ListView - 这可能是完全正确的 - 但我还没有完全明白。
这是我收到的错误消息:
09-15 18:30:43.169: E/AndroidRuntime(1081): FATAL EXCEPTION: main
09-15 18:30:43.169: E/AndroidRuntime(1081): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fthatnoise.borrow.me/com.fthatnoise.borrow.me.BorrowerList}: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.GridView
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.os.Handler.dispatchMessage(Handler.java:99)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.os.Looper.loop(Looper.java:137)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-15 18:30:43.169: E/AndroidRuntime(1081): at java.lang.reflect.Method.invokeNative(Native Method)
09-15 18:30:43.169: E/AndroidRuntime(1081): at java.lang.reflect.Method.invoke(Method.java:511)
09-15 18:30:43.169: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-15 18:30:43.169: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-15 18:30:43.169: E/AndroidRuntime(1081): at dalvik.system.NativeStart.main(Native Method)
09-15 18:30:43.169: E/AndroidRuntime(1081): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.GridView
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
09-15 18:30:43.169: E/AndroidRuntime(1081): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-15 18:30:43.169: E/AndroidRuntime(1081): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.Activity.setContentView(Activity.java:1881)
09-15 18:30:43.169: E/AndroidRuntime(1081): at com.fthatnoise.borrow.me.BorrowerList.onCreate(BorrowerList.java:39)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.Activity.performCreate(Activity.java:5104)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-15 18:30:43.169: E/AndroidRuntime(1081): ... 11 more
09-15 18:30:43.169: E/AndroidRuntime(1081): Caused by: java.lang.reflect.InvocationTargetException
09-15 18:30:43.169: E/AndroidRuntime(1081): at java.lang.reflect.Constructor.constructNative(Native Method)
09-15 18:30:43.169: E/AndroidRuntime(1081): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
09-15 18:30:43.169: E/AndroidRuntime(1081): ... 24 more
09-15 18:30:43.169: E/AndroidRuntime(1081): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x3
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.View.<init>(View.java:3340)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.view.ViewGroup.<init>(ViewGroup.java:431)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.widget.AdapterView.<init>(AdapterView.java:235)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.widget.AbsListView.<init>(AbsListView.java:766)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.widget.GridView.<init>(GridView.java:110)
09-15 18:30:43.169: E/AndroidRuntime(1081): at android.widget.GridView.<init>(GridView.java:106)
09-15 18:30:43.169: E/AndroidRuntime(1081): ... 27 more
任何帮助你都很棒。谢谢!
这是包含 GridView (R.layout.grid_view_person_record) 的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
tools:context=".PersonRecord" >
<GridView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:paddingLeft="@android:id/list"
android:stretchMode="columnWidth" >
</GridView>
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@android:id/list"
android:layout_alignTop="@android:id/list" />
</RelativeLayout>