我希望在我的ListView中處理選定行的背景顏色,從閱讀中我需要使用CustomAdapter進行擴展。我的主適配器是SimpleCursorAdapter類型的,所以我修改了一個將ArrayAdapter擴展到SimpleCursorAdapter的CustomAdapter。ListView和CustomAdapter擴展SimpleCursorAdapter
我的問題是,當使用CustomAdapter時,listview是空白的,但如果不擴展並使用SimpleCursorAdapter,那麼listview就有行/項目。 Logcat在我的CustomAdapter中顯示了一個問題,如下所示。
這是我的主要活動代碼:
phrasesdb helper = new phrasesdb(this);
database = helper.getWritableDatabase();
data = database.query("phrases", fields, null, null, null, null, fields[0] + " COLLATE NOCASE ASC");
//WORKING SimpleCursorAdapter
//dataSource = new SimpleCursorAdapter(this, R.layout.phrases, data, fields, new int[] { R.id.phrase });
//NOT WORKING
dataSource = new CustomAdapter(this, R.layout.phrases, data, fields, new int[] { R.id.phrase });
view = getListView();
setListAdapter(dataSource);
這是自定義適配器類,我已標記的位置,其中logcat的錯誤:
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class CustomAdapter extends SimpleCursorAdapter {
protected static final int NO_SELECTED_COLOR = 0xFF191919;
protected static final int SELECTED_COLOR = 0xFF3366CC;
Cursor items;
private LayoutInflater mInflater;
private int viewResourceId;
private int selectedPosition;
public CustomAdapter(Context context, int resourceId, Cursor data, String[] fields, int[] is) {
super(context, resourceId, data, fields, is);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewResourceId = resourceId;
items = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView)convertView;
if (tv == null) {
//WHEN DEBUGGING THIS IS WHERE LOGCAT ERROR MESSAGES STARTS
tv = (TextView)mInflater.inflate(viewResourceId, null);
}
tv.setText(items.getString(position));
// Change the background color
if (position==selectedPosition) tv.setBackgroundColor(SELECTED_COLOR);
else tv.setBackgroundColor(NO_SELECTED_COLOR);
return tv;
}
public void setSelected(int position) {
selectedPosition = position;
}
}
這是logcat的:
10-18 13:33:17.869:E/ListView(28378):android.widget.LinearLayout 10-18 13:33:17.869:E/List查看(28378):java.lang.ClassCastException:android.widget.LinearLayout 10-18 13:33:17.869:E/ListView(28378):at com.xxx.xxx.CustomAdapter.getView(CustomAdapter.java:49) E:/ ListView(28378):at android.widget.AbsListView.obtainView(AbsListView.java:1449) 10-18 13:33:17.869:E/ListView(28378):at android.widget.ListView.makeAndAddView(ListView.java:1801) 10-18 13:33:17.869:E/ListView(28378):at android.widget.ListView.fillSpecific(ListView.java:1339) 10-18 13/33:17.869:E/ListView(28378):在android.widget.ListView.layoutChildren(ListView.java:1637) 10-18 13:33:17.869:E/ListView(28378):在android.widget。 AbsListView.onLayout(AbsListView.java:1279) 10-18 13:33:17.869:E/ListView(28378):at android.view.View.layout(View.java:7321) 10-18 13:33:17.869:E/ListView(28378):在android.widget.FrameLayout.onLayout(FrameLayout.java:338) 10-18 13:33:17.869:E/ListView(28378):at android.view.View.layout(View.java:7321) 10-18 13:33:17.869:E/ListView(28378):android.widget.FrameLayout.onLayout(FrameLayout.java:338) 10-18 13:33:17.869:E/ListView(28378):在android.view.View.layout(View.java:7321) 10-18 13:33:17.869:E/ListView(28378):在android.view。 ViewRoot.performTraversals(ViewRoot.java:1217) 10-18 13:33:17.869:E/ListView(28378):at android.view.ViewRoot.handleMessage(ViewRoot.java:1991) 10-18 13:33: 17.869:E/ListView(28378):at android.os.Handler.dispatchMessage(Handler.java:99) 10-18 13:33:17.869:E/ListView(28378):at android.os.Looper.loop( Looper.java:150) 10-18 13:33:17.869:E/ListView(28378):at android.app.ActivityThread.main(ActivityThread.java:4385) 10-18 13:33:17.869:E/ListView(28378):at java.lang.reflect.Method.invokeNative(Native Method) 10-18 13:33:17.869:E/ListView(28378):at java.lang.reflect.Method.invoke(Method.java:507) 10- 18 13:33:17.869:E/ListView(28378):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849) 10-18 13:33:17.869:E/ListView(28378) ):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 10-18 13:33:17.869:E/ListView(28378):at dalvik.system.NativeStart。主(本地方法)
這是logcat的是表明一些與LinearLayout中進行佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/rowLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
<TextView
android:id="@+id/phrase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/wordforphrases"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"/>
</LinearLayout>
,但我想不出哪裏出了問題。
請任何人都能夠在正確的方向嗎?
感謝 馬克
更新:改變以匹配膨脹按照薩爾多的想法(三江源)後:
tv = (TextView)mInflater.inflate(viewResourceId, parent);
現在我得到這個錯誤
10-18 15:23:22.083: E/ListView(28931): addView(View, LayoutParams) is not supported in AdapterView
10-18 15:23:22.083: E/ListView(28931): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
10-18 15:23:22.083: E/ListView(28931): at android.widget.AdapterView.addView(AdapterView.java:461)
10-18 15:23:22.083: E/ListView(28931): at android.view.LayoutInflater.inflate(LayoutInflater.java:416)
10-18 15:23:22.083: E/ListView(28931): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-18 15:23:22.083: E/ListView(28931): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-18 15:23:22.083: E/ListView(28931): at com.xxx.xxx.CustomAdapter.getView(CustomAdapter.java:36)
10-18 15:23:22.083: E/ListView(28931): at android.widget.AbsListView.obtainView(AbsListView.java:1449)
10-18 15:23:22.083: E/ListView(28931): at android.widget.ListView.makeAndAddView(ListView.java:1801)
10-18 15:23:22.083: E/ListView(28931): at android.widget.ListView.fillSpecific(ListView.java:1339)
10-18 15:23:22.083: E/ListView(28931): at android.widget.ListView.layoutChildren(ListView.java:1637)
10-18 15:23:22.083: E/ListView(28931): at android.widget.AbsListView.onLayout(AbsListView.java:1279)
10-18 15:23:22.083: E/ListView(28931): at android.view.View.layout(View.java:7321)
10-18 15:23:22.083: E/ListView(28931): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-18 15:23:22.083: E/ListView(28931): at android.view.View.layout(View.java:7321)
10-18 15:23:22.083: E/ListView(28931): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-18 15:23:22.083: E/ListView(28931): at android.view.View.layout(View.java:7321)
10-18 15:23:22.083: E/ListView(28931): at android.view.ViewRoot.performTraversals(ViewRoot.java:1217)
10-18 15:23:22.083: E/ListView(28931): at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
10-18 15:23:22.083: E/ListView(28931): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 15:23:22.083: E/ListView(28931): at android.os.Looper.loop(Looper.java:150)
10-18 15:23:22.083: E/ListView(28931): at android.app.ActivityThread.main(ActivityThread.java:4385)
10-18 15:23:22.083: E/ListView(28931): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 15:23:22.083: E/ListView(28931): at java.lang.reflect.Method.invoke(Method.java:507)
10-18 15:23:22.083: E/ListView(28931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
10-18 15:23:22.083: E/ListView(28931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
10-18 15:23:22.083: E/ListView(28931): at dalvik.system.NativeStart.main(Native Method)
10-18 15:23:22.093: D/View(28931): onTouchEvent: viewFlags: 0x18244001
當檢查父它看到TextView。
還有其他想法嗎?
謝謝,更改後匹配充氣: 'code' tv =(TextView)mInflater.inflate(viewResourceId,parent); – Mark