游标只获取UserDataDictionary中的最后一项?

时间:2015-01-11 09:40:02

标签: android cursor android-contentprovider android-cursor android-contentresolver

我只是在Android中学习内容提供者和解析器,并尝试制作一个简单的应用程序来打印UserDataDictionary的详细信息, 但是,虽然我有很多项目,光标数显示为 1 ,只打印一个项目, 我去了清除字典中的所有单词,但仍显示一个并打印出一个项目,我只是学习android,任何帮助都会受到赞赏,请参阅Oncreate(0方法下面

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

        //Get and display the data dictionary
        TextView dictionaryTextView=(TextView)findViewById(R.id.dictionary_text_view);
        ContentResolver resolver=getContentResolver();
        //get cursor containing all words
        Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);
        String userDictString;
        try {

            //get total words
            int numWords = cursor.getCount();
            //get all column Indexes
            int columnIndex = cursor.getColumnIndex(UserDictionary.Words._ID);
            int columnWords = cursor.getColumnIndex(UserDictionary.Words.WORD);
            int columnFrequency = cursor.getColumnIndex(UserDictionary.Words.FREQUENCY);

            userDictString=("The User dictionary Contains\n" +cursor.getCount()+" words\n"+UserDictionary.Words._ID +"\t\t"+UserDictionary.Words.WORD +"\t"+UserDictionary.Words.FREQUENCY+"\n");
            Log.d("Word",userDictString);
            dictionaryTextView.setText(userDictString);
            while (cursor.moveToNext())//zero if end of list in cursor(no more rows)
            {
                Log.d("Word",cursor.getString(columnWords));
                int id = cursor.getInt(columnIndex);
                int frequency = cursor.getInt(columnFrequency);
                String word = cursor.getString(columnWords);
                dictionaryTextView.append(("\n" + id + " - " + frequency + " - " + word));
            }

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        finally {
            cursor.close();
        }


    }

1 个答案:

答案 0 :(得分:1)

您的代码在我的手机上完美运行。除非我刚添加了这个词,否则频率值默认为250。尝试在另一部手机上进行测试。