-1

所以我收到 3 个错误“final int sortColumnIndex = c.getColumnIndex(YOUR_SORT_COLUMN_NAME);” & "public class MyAdapter extends CursorAdapter implements SectionIndexer" <---这个被标记为错误两次。

我的 MainActivityNext.java

package testing.android.application.three;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.widget.AlphabetIndexer;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;

public class MainActivityNext extends ListActivity{

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

}

public class MyAdapter extends CursorAdapter implements SectionIndexer
{
    // All valid characters. May want to include numbers, etc if they show up 
    // in your sort column
    private final static String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

    private AlphabetIndexer mIndexer = null;

    public MyAdapter(Context context, Cursor c, int flags)
    {
        super(context, c, flags);
        // Assumes your cursor is non-null, 
        // otherwise do this in swapCursor if mIndexer==null
        final int sortColumnIndex = c.getColumnIndex(YOUR_SORT_COLUMN_NAME);
        mIndexer = new AlphabetIndexer(c, sortColumnIndex,
            ALPHABET);
    }

    public Cursor swapCursor(Cursor newCursor)
    {
        super.swapCursor(newCursor);
        // Make sure the AlphabetIndexer knows about the new Cursor
        mIndexer.setCursor(newCursor);
        return newCursor;

    }

    public int getPositionForSection(int section)
    {
        // AlphabetIndexer does all the hard work
        return mIndexer.getPositionForSection(section);
    }

    public int getSectionForPosition(int position)
    {
        // AlphabetIndexer does all the hard work
        return mIndexer.getSectionForPosition(position);
    }

    public Object[] getSections()
    {
        // AlphabetIndexer does all the hard work
        return mIndexer.getSections();
    }

}

}
4

3 回答 3

1

根据您的 Logcat:

无法启动活动 ComponentInfo{testing.android.application.three/testing.android.application.three.MainActivityNext}:java.lang.RuntimeException:您的内容必须有一个 id 属性为“android.R.id.list”的 ListView

每当您使用或扩展 listactivity 时,您为其构建的视图必须具有具有该 ID 的列表视图。

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

     <ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:background="#FF0000"
               android:text="No data"/>
 </LinearLayout>

有关更多信息,请参阅http://developer.android.com/reference/android/app/ListActivity.html

于 2013-06-02T16:41:29.747 回答
0

如日志:

我们的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView

意味着您需要@id/android:list在 xml 中为 ListView 使用 id。这样做:

<ListView android:id="@id/android:list"  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         ..../>
于 2013-06-02T16:41:50.287 回答
0

如果您的活动扩展了 ListActivity,那么您必须设置 ListView 的 xml 的 ID,如下所示

< ListView
    android:id="@android:id/list"
...
于 2013-06-02T16:42:08.433 回答