本文介绍了SimpleCursorAdapter不MainActivity工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

并请告诉我,我是使用String [] FROM正确映射光标

当我使用的是SimpleCursorAdapter在我MainActivity它给错误的onCreate方法

When I am using a SimpleCursorAdapter in my oncreate method of MainActivity it is giving error

的方法SimpleCursorAdapter(MainActivity,INT,光标的String [],INT [])是未定义的类型MainActivity

我照着这我正确的使用它检查了API文档的contastructor

I have checked the contastructor in api docs according to which I am using it correctly

公开SimpleCursorAdapter(上下文的背景下,INT布局,光标C,的String []从,INT []到)

这里是我的MainActivity.java

package com.example.listview;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;

import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {

    private ListView list;  
    private Cursor cursor;
    private SimpleCursorAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        list = (ListView) findViewById(R.layout.listview);      

        cursor = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);

         String [] FROM = {cursor
            .getString(cursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)), cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))};

         int [] TO = {android.R.id.text1, android.R.id.text2};

        adapter = SimpleCursorAdapter(MainActivity.this, android.R.layout.two_line_list_item,cursor,FROM,TO);

        list.setAdapter(adapter);
    }

}

这是我的布局listview.xml

<?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="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

请帮我

推荐答案

创建 SimpleCursorAdapter 对象:

adapter =new  SimpleCursorAdapter(MainActivity.this, 
         ^^^       android.R.layout.two_line_list_item,cursor,FROM,TO);

您忘记使用创建 SimpleCursorAdapter

和使用ListView控件ID来初始化列表而不是 R.layout.listview

and use ListView id to initialize list instead of R.layout.listview

list= (ListView) findViewById(R.id.listview);

这篇关于SimpleCursorAdapter不MainActivity工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 06:25