0

我正在尝试将数据从 sqlite 获取到 listview,所以我按照这个 url 中的答案示例 从 SQLite 数据库制作 listview

CustomCursorAdapter 就是这样

    public class CustomCursorAdapter extends SimpleCursorAdapter  {
    private int layout; 
    private LayoutInflater inflater;
    private Context context;

    public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.layout = layout;
        this.context = context;
        inflater = LayoutInflater.from(context);

    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        View v = inflater.inflate(R.layout.topics, parent, false);

        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {
                //1 is the column where you're getting your data from
        String name = c.getString(1);
        /**
         * Next set the name of the entry.
         */
        TextView name_text = (TextView) v.findViewById(R.id.listLabel);
        if (name_text != null) {
            name_text.setText(name);
        }   
    }
}

但是 onCreate 我有这个代码

ListView listView = (ListView)findViewById(R.layout.topics);
    connectToDB();
    from = new String[] { "topicTitle" };
    to = new int[] { R.id.listLabel };

    CustomCursorAdapter adapter = new CustomCursorAdapter(this,
            R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);

用xml如下

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/listLogo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
         >
    </ImageView>

    <TextView
        android:id="@+id/listLabel"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="15sp" >
    </TextView>

</LinearLayout>

问题是.. listView 为空!!..我不知道为什么!

4

2 回答 2

2

您指的是您的布局主题。您应该在 xml 中定义一个带有 id 的列表视图。在 setCONntentView(R.layout.topics) 之后的活动 onCreate() 中,找到 listview 的 id 并将适配器设置为您的列表。

在你的 xml 中说 topic.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#0095FF">
   <ListView android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="100dp" 
    android:id="@+id/lv">
   </ListView>
    // other ui elements
</LinearLayout>

然后在你的活动 onCreate()

   setContentview(R.layout.topics);
   ListView listView = (ListView)findViewById(R.id.lv);
   connectToDB();
   from = new String[] { "topicTitle" };
   to = new int[] { R.id.listLabel };


   CustomCursorAdapter adapter = new CustomCursorAdapter(this,
        R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);

来自聊天中的讨论

编辑:

定义 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#0095FF">

<ListView android:id="@+id/list"
android:layout_width="fill_parent"  
android:layout_height="0dip"
android:focusableInTouchMode="false"
android:listSelector="@android:color/transparent"
android:layout_weight="2"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:dividerHeight="8dp" 
android:divider="#000000" 
android:cacheColorHint="#000000"
android:drawSelectorOnTop="false">
</ListView>
</LinearLayout>

在你的活动中

 public class MainActivity extends Activity {

 ListView lv;
 CustomCursorAdapter cus;
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv = (ListView) findViewById(R.id.list);
    cus= new CustomCursorAdapter(parameters);
    lv.setAdapter(cus);

 }
 }

定义 customCursor 适配器为自定义布局充气。为图像视图设置图像,为文本视图设置文本。

于 2013-04-09T07:59:17.903 回答
0

尝试编辑您的 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" 
android:id="@+id/main"
>

<ImageView
    android:id="@+id/listLogo"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
     >
</ImageView>

<TextView
    android:id="@+id/listLabel"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="15sp" >
</TextView>

<ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/topics"
        ></ListView>

 </LinearLayout>

在 Activity onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView listView = (ListView)findViewById(R.layout.topics);
    connectToDB();
    from = new String[] { "topicTitle" };
    to = new int[] { R.id.listLabel };

    CustomCursorAdapter adapter = new CustomCursorAdapter(this,
            R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);
}
于 2013-04-09T08:02:09.513 回答