2011-12-05 33 views
0

我试图创建一个扩展CursorAdapter的类,以便在ListView中使用其newView和bindView方法显示RatingBar和文本。这是一个试图进一步从开发网站的记事本教程。我的问题是,该程序崩溃男子气概,因为我实施的方法错了,虽然可能有其他问题的实现在这个类或filldata方法。我的问题是什么是写代码,使我可以显示与其他textviews沿着列表视图评级条正确的方法是什么?谢谢使用光标适配器在ListView中显示TextViews和RatingBar

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方法延伸利斯塔ctivity

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

你需要创建一个使用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. 

    } 
+0

不看我得从什么太大的不同......还不该我是否使用CursorAdapter而不是BaseAdapter作为类扩展? – Stefan

+0

检查logcat,发现应用程序崩溃时发生的错误 –

+0

我更新了代码....现在程序运行,但它不会返回任何内容......即使我在程序本身中添加了一个注释......上帝我很累 – Stefan

相关问题