
我希望 *** 纵ListVIEw中所选行的背景颜色,从阅读中我需要使用CustomAdapter进行扩展.我的主适配器是SimpleCursorAdapter类型,所以我修改了一个将ArrayAdapter扩展为SimpleCursorAdapter的CustomAdapter.
我的问题是使用CustomAdapter时ListvIEw是空白但如果不扩展并使用SimpleCursorAdapter,ListvIEw有行/项.
Logcat在我的CustomAdapter中显示一个问题,见下文.
这是我的主要活动代码:
phrasesdb helper = new phrasesdb(this);database = helper.getWritableDatabase();data = database.query("phrases",fIElds,null,fIElds[0] + " ColLATE NOCASE ASC");//WORKING SimpleCursorAdapter//dataSource = new SimpleCursorAdapter(this,R.layout.phrases,data,new int[] { R.ID.phrase });//NOT WORKINGdataSource = new CustomAdapter(this,new int[] { R.ID.phrase });vIEw = getListVIEw();setlistadapter(dataSource);这是Custom Adapter类,我已经标记了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,is); mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); vIEwResourceID = resourceID; items = data;}@OverrIDepublic 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/ListVIEw(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)
10-18 13:33:17.869: 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): at androID.Widget.ListVIEw.layoutChildren(ListVIEw.java:1637)
10-18 13:33:17.869: E/ListVIEw(28378): at 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): at 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): at 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): at 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.main(Native Method)
这是布局
Logcat表示与linearLayout有关,但我无法弄清楚我哪里出错了.
请任何人能够朝着正确的方向发展我吗?
谢谢
标记
更新:根据Sardor的想法(谢谢你)更改为匹配膨胀之后:
tv = (TextVIEw)mInflater.inflate(vIEwResourceID,parent);
我现在得到这个错误
10-18 15:23:22.083: E/ListVIEw(28931): addVIEw(VIEw,LayoutParams) is not supported in AdapterVIEw10-18 15:23:22.083: E/ListVIEw(28931): java.lang.UnsupportedOperationException: addVIEw(VIEw,LayoutParams) is not supported in AdapterVIEw10-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.
还有其他想法吗?
最佳答案好吧,我终于开始工作……这里的问题是,如果您的自定义适配器数据来自Cursor,那么您必须在扩展的SimpleCursorAdapter类中使用bindVIEw和newVIEw而不是getVIEw.
我会为可能遇到此问题的其他人发布我的工作代码…
public class CustomAdapter extends SimpleCursorAdapter {private int mSelectedposition;Cursor items;private Context context;private int layout;@OverrIDepublic VIEw newVIEw(Context context,Cursor cursor,VIEwGroup parent) { Cursor c = getCursor(); final LayoutInflater inflater = LayoutInflater.from(context); VIEw v = inflater.inflate(layout,parent,false); int nameCol = c.getColumnIndex("phrase"); String name = c.getString(nameCol); TextVIEw name_text = (TextVIEw) v.findVIEwByID(R.ID.phrase); if (name_text != null) { name_text.setText(name); } return v;}public CustomAdapter(Context context,int layout,Cursor c,String[] from,int[] to) { super(context,layout,c,from,to); this.context = context; this.layout = layout;}@OverrIDepublic voID bindVIEw(VIEw v,Context context,Cursor c) { int nameCol = c.getColumnIndex("phrase"); String name = c.getString(nameCol); TextVIEw name_text = (TextVIEw) v.findVIEwByID(R.ID.phrase); if (name_text != null) { name_text.setText(name); } //name_text.setTextcolor(color.GREEN); int position = c.getposition(); if (mSelectedposition == position) { v.setBackgroundResource(R.drawable.ListvIEwbackground); v.getBackground().setDither(true); } else { v.setBackgroundcolor(color.BLACK); }}public voID setSelectedposition(int position) { mSelectedposition = position; notifyDataSetChanged();}
和
Cursor data; static final String fIElds[] = { "phrase",BaseColumns._ID }; dataSource = new CustomAdapter(this,new int[] { R.ID.phrase }); vIEw = getListVIEw(); setlistadapter(dataSource);
总结 以上是内存溢出为你收集整理的android – Listview和CustomAdapter扩展SimpleCursorAdapter全部内容,希望文章能够帮你解决android – Listview和CustomAdapter扩展SimpleCursorAdapter所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)