Android 实现游标适配器,使用newView发出

Android 实现游标适配器,使用newView发出,android,Android,我已经创建了一个CursorAdapter,但不断出现以下错误: 06-01 20:36:58.890: E/AndroidRuntime(21204): java.lang.ClassCastException: android.view.View cannot be cast to android.view.ViewGroup 这是我的密码: @Override public View newView(Context context, Cursor cursor, ViewGroup pa

我已经创建了一个CursorAdapter,但不断出现以下错误:

06-01 20:36:58.890: E/AndroidRuntime(21204): java.lang.ClassCastException: android.view.View cannot be cast to android.view.ViewGroup
这是我的密码:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
    final LayoutInflater inflater = LayoutInflater.from(context);

    //***The error originates from this line***
    final View view = inflater.inflate(R.layout.competitor_autocomplete_list_item, parent, false);

    TextView stockNameTextView = (TextView) view.findViewById(R.id.autocomplete_stock_name); 
    TextView stockSymbolTextView = (TextView) view.findViewById(R.id.autocomplete_stock_symbol);        

    String stockName = cursor.getString(cursor.getColumnIndex(StournamentConstants.TblStocks.COLUMN_NAME)); 
    String stockSymbol = cursor.getString(cursor.getColumnIndex(StournamentConstants.TblStocks.COLUMN_EXTERNAL_ID)); 

    stockNameTextView.setText(stockName);
    stockSymbolTextView.setText(stockSymbol);       

    return view;
}
我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

    <TextView
    android:id="@+id/autocomplete_stock_name"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="#95B9C7"
    android:padding="10dp"        
    android:textColor="#000"
    android:textSize="16sp" />

    <TextView
    android:id="@+id/autocomplete_stock_symbol"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="#95B9C7"
    android:padding="10dp"        
    android:textColor="#000"
    android:textSize="16sp" />
</LinearLayout>
这是引发异常后打印的堆栈

06-02 00:53:37.031: E/AndroidRuntime(32327): FATAL EXCEPTION: main
06-02 00:53:37.031: E/AndroidRuntime(32327): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.widget.ListView.measureScrapChild(ListView.java:1163)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1095)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.widget.ListPopupWindow.show(ListPopupWindow.java:524)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1062)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:939)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:921)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.os.Looper.loop(Looper.java:137)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at java.lang.reflect.Method.invokeNative(Native Method)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at java.lang.reflect.Method.invoke(Method.java:511)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-02 00:53:37.031: E/AndroidRuntime(32327):    at dalvik.system.NativeStart.main(Native Method)

不能将视图放在其他视图中。您需要一个视图组来执行此操作,因此会出现错误。如果在布局xml文件中切换回LinearLayout,错误应该会消失。

不能将视图放在其他视图中。您需要一个视图组来执行此操作,因此会出现错误。如果在布局xml文件中切换回LinearLayout,错误应该会消失。

尝试以下操作:

View view = inflater.inflate(R.layout.competitor_autocomplete_list_item, parent, false);
编辑

我错过了你从线性布局改变过来的机会。雷纳德是正确的,把它改回去(不要试着投下它),它应该会起作用。

试试这个:

View view = inflater.inflate(R.layout.competitor_autocomplete_list_item, parent, false);
编辑


我错过了你从线性布局改变过来的机会。雷纳德是正确的,把它改回去(不要试图投下它),它应该会起作用。

我将这一点记录下来,以供后代参考

误导我的是,我不明白我必须复制
newView()
bindView()中的内容

我看到人们像这样绑定一个
TextView
((TextView)视图)
我想如果我通过一个
视图,我也必须这样做,因为你不能
视图上设置文本()
,所以我做了这样的事情:
((LinearLayout)视图)

我宁愿忘记这件事甚至发生在我身上。如果您有兴趣在游标适配器中使用LinearLayout,这是正确的代码:

@Override
public View bindView(Context context, Cursor cursor, ViewGroup parent)
{
  TextView stockNameTextView = (TextView) view.findViewById(R.id.autocomplete_stock_name); 
  TextView stockSymbolTextView = (TextView) view.findViewById(R.id.autocomplete_stock_symbol);        

  String stockName = cursor.getString(cursor.getColumnIndex(TournamentConstants.TblStocks.COLUMN_NAME)); 
  String stockSymbol = cursor.getString(cursor.getColumnIndex(TournamentConstants.TblStocks.COLUMN_EXTERNAL_ID)); 

  stockNameTextView.setText(stockName);
  stockSymbolTextView.setText(stockSymbol);       
}

我把这一点记录了好几代人

误导我的是,我不明白我必须复制
newView()
bindView()中的内容

我看到人们像这样绑定一个
TextView
((TextView)视图)
我想如果我通过一个
视图,我也必须这样做,因为你不能
视图上设置文本()
,所以我做了这样的事情:
((LinearLayout)视图)

我宁愿忘记这件事甚至发生在我身上。如果您有兴趣在游标适配器中使用LinearLayout,这是正确的代码:

@Override
public View bindView(Context context, Cursor cursor, ViewGroup parent)
{
  TextView stockNameTextView = (TextView) view.findViewById(R.id.autocomplete_stock_name); 
  TextView stockSymbolTextView = (TextView) view.findViewById(R.id.autocomplete_stock_symbol);        

  String stockName = cursor.getString(cursor.getColumnIndex(TournamentConstants.TblStocks.COLUMN_NAME)); 
  String stockSymbol = cursor.getString(cursor.getColumnIndex(TournamentConstants.TblStocks.COLUMN_EXTERNAL_ID)); 

  stockNameTextView.setText(stockName);
  stockSymbolTextView.setText(stockSymbol);       
}

包括
R.layout的XML。竞争对手\u autocomplete\u list\u项目
包括
R.layout的XML。竞争对手\u autocomplete\u list\u项目
充气
已经返回了一个
视图
,因此演员阵容一开始是无害的,删除它不会改变任何东西。我实际上不想尝试将它投射到线性布局中,但这一点说得很好,因为我不清楚。现在我得到了以下错误:06-02 00:48:15.820:E/AndroidRuntime(32157):java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams-有什么想法吗?
充气
已经返回了
视图
,因此,演员阵容一开始是无害的,移除它不会改变任何事情。实际上,我本不想尝试将其设置为线性布局,但我不清楚,这一点做得很好。现在我得到了以下错误:06-02 00:48:15.820:E/AndroidRuntime(32157):java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams-有什么想法吗?