Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

java - Eclipse / Android error

So I'm getting 3 errors "final int sortColumnIndex = c.getColumnIndex(YOUR_SORT_COLUMN_NAME);" & "public class MyAdapter extends CursorAdapter implements SectionIndexer" <---this one is getting marked as an error twice.

My 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();
    }

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to your Logcat:

Unable to start activity ComponentInfo{testing.android.application.three/testing.android.application.three.MainActivityNext}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

Whenever you use or extend a listactivity the view you build for it must have a listview with that 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>

See http://developer.android.com/reference/android/app/ListActivity.html for more info on it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...