使用光标适配器在ListView中显示TextViews和RatingBar

时间:2011-12-05 19:03:38

标签: android android-widget

我正在尝试创建一个扩展CursorAdapter的类,以便使用其newView和bindView方法在ListView中显示RatingBar和文本。这是尝试进一步使用Dev网站上的Notepad教程。我的问题是程序崩溃,因为我正在实现错误的方法,尽管在这个类或filldata方法中可能存在其他实现问题。我的问题是编写代码的正确方法是什么,以便我可以显示列表视图中的评级栏以及其他文本视图?谢谢

package com.android.demo.notepad3;

import android.content.Context;
import android.database.Cursor;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.RatingBar;

public class NoteA extends BaseAdapter{
    private final LayoutInflater mInflater;
    private EditText mTitleText;
    private EditText mBodyText;
    private RatingBar mRatingBar;
    private NotesDbAdapter mDbHelper;
    Context c1;
    static Cursor c;
    private static boolean autoRequery;


    public NoteA(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, c, autoRequery);
    mInflater = LayoutInflater.from(context);
    c1=context;
    this.c=c;
     mDbHelper = new NotesDbAdapter(context);
      mDbHelper.open();

    }
    @Override
public View getView(int arg0, View arg1, ViewGroup arg2) {


      final View view = inflater.inflate(R.layout.notes_row, null);
      mTitleText = (EditText) view.findViewById(R.id.title);
      mBodyText = (EditText) view.findViewById(R.id.body);
      mRatingBar= (RatingBar) view.findViewById(R.id.ratingbar);

      mTitleText.setText(c.getString(
             c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
     mBodyText.setText(c.getString(
             c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
     mRatingBar.setRating(c.getFloat(
             c.getColumnIndexOrThrow(NotesDbAdapter.KEY_RATING)));

    return view;
}
     // @Override
//public void bindView(View view, Context context, Cursor cursor) {

//}

//@Override
//public View newView(Context context, Cursor cursor, ViewGroup parent) {
//final View view = mInflater.inflate(R.layout.notes_row, parent, false);
//bindView(view, context, cursor);
//return view;
//}



}

来自Notepad3类的Filldata方法扩展了ListActivity

 private void fillData() {
       Cursor notesCursor = mDbHelper.fetchAllNotes();
       startManagingCursor(notesCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
       String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_BODY};

       // and an array of the fields we want to bind those fields to (in this case just text1)
       int[] to = new int[]{R.id.text1,R.id.text2};

         //Now create a simple cursor adapter and set it to display
        ListAdapter notes = new NoteA(this, R.layout.notes_row, notesCursor, from, to);
        setListAdapter(notes);
    }

1 个答案:

答案 0 :(得分:1)

您需要使用BaseAdapter创建自己的自定义ListView ..

喜欢这个..

public class MyCustomAdapter extends BaseAdapter{
private Context context;

private List<Phonebook> listPhonebook;

public PhonebookAdapter(Context context, List<Phonebook> listPhonebook) {
    this.context = context;
    this.listPhonebook = listPhonebook;
}

public int getCount() {
    return listPhonebook.size();
}

public Object getItem(int position) {
    return listPhonebook.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup viewGroup) {
 outInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.phone_row, null); //This should be your Layout with the rating bar

 //Here you can get your widgets for use like this..

  TextView tvPhone = (TextView) convertView.findViewById(R.id.tvMobile);
 //You can do the same thing for the Rating bar you.

    }
相关问题