Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Listview和CustomAdapter扩展SimpleCursorAdapter_Android_Listview_Background Color_Extend_Custom Adapter - Fatal编程技术网

Android Listview和CustomAdapter扩展SimpleCursorAdapter

Android Listview和CustomAdapter扩展SimpleCursorAdapter,android,listview,background-color,extend,custom-adapter,Android,Listview,Background Color,Extend,Custom Adapter,我希望在我的ListView中操纵所选行的背景色,从阅读中,我需要使用CustomAdapter进行扩展。我的主适配器是SimpleCursorAdapter类型,因此我修改了一个CustomAdapter,将ArrayAdapter扩展为SimpleCursorAdapter 我的问题是,当使用CustomAdapter时,listview是空的,但是如果不扩展并使用SimpleCursorAdapter,listview就会有行/项。 Logcat显示我的CustomAdapter中存在问题

我希望在我的ListView中操纵所选行的背景色,从阅读中,我需要使用CustomAdapter进行扩展。我的主适配器是SimpleCursorAdapter类型,因此我修改了一个CustomAdapter,将ArrayAdapter扩展为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/ListView(28378):java.lang.ClassCastException:android.widget.LinearLayout 10-18 13:33:17.869:E/ListView(28378):位于com.xxx.xxx.CustomAdapter.getView(CustomAdapter.java:49) 10-18 13:33:17.869:E/ListView(28378):在android.widget.AbsListView.obtainView(AbsListView.java:1449) 10-18 13:33:17.869:E/ListView(28378):在android.widget.ListView.makeAndAddView(ListView.java:1801) 10-18 13:33:17.869:E/ListView(28378):位于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):在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.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):在android.view.ViewRoot.handleMessage(ViewRoot.java:1991) 10-18 13:33:17.869:E/ListView(28378):在android.os.Handler.dispatchMessage(Handler.java:99)上 10-18 13:33:17.869:E/ListView(28378):位于android.os.Looper.loop(Looper.java:150) 10-18 13:33:17.869:E/ListView(28378):位于android.app.ActivityThread.main(ActivityThread.java:4385) 10-18 13:33:17.869:E/ListView(28378):位于java.lang.reflect.Method.Invokenactive(本机方法) 10-18 13:33:17.869:E/ListView(28378):位于java.lang.reflect.Method.invoke(Method.java:507) 10-18 13:33:17.869:E/ListView(28378):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 10-18 13:33:17.869:E/ListView(28378):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 10-18 13:33:17.869:E/ListView(28378):在dalvik.system.NativeStart.main(本机方法)

这是布局图

<?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>
我现在得到这个错误

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
当检查父项时,它会看到文本视图


还有其他想法吗?

tv=(TextView)mInflater.充气(viewsourceid,
parent
null)

好的,我终于让它工作了

这里的问题是,如果自定义适配器数据来自游标,则必须在扩展的SimpleCursorAdapter类中使用bindView和newView,而不是getView

我将为任何可能遇到此问题的其他人发布我的工作代码

public class CustomAdapter extends SimpleCursorAdapter {

private int mSelectedPosition;
Cursor items;
private Context context;
private int layout;

@Override
public 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;
}


@Override
public 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();

}


如果您试图自定义布局中项目的显示,可以使用一种更简单的方法。

谢谢,更改为匹配充气后:
code
tv=(TextView)mInflater.inflate(viewsourceid,parent);
public class CustomAdapter extends SimpleCursorAdapter {

private int mSelectedPosition;
Cursor items;
private Context context;
private int layout;

@Override
public 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;
}


@Override
public 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, R.layout.phrases, data, fields, new int[] { R.id.phrase });
  view = getListView();
  setListAdapter(dataSource);