Android: Problème avec newView et bindView dans SimpleCursorAdapter personnalisé

J'ai créé un custom SimpleCursorAdapter de l'un de le seul exemple que j'ai trouvé.

Quand mon ListActivity est appelé, newView et bindView sont appelés pour tous de ma DB entrée, et de nouveau appelé pour chaque entrée. J'ai quelques questions:

-est l'exemple à droite (si non, où puis-je en trouver un)?

-si bindView appel est toujours précédée par newView appel, pourquoi faire la même dans les deux fonctions?

-pourquoi la séquence newView-bindView appelé deux fois pour chaque élément?

-pourquoi certains CursorAdapter exemples d'utilisation getView au lieu de newView et bindView?

Fondamentalement, comment doit-SimpleCursorAdapter être utilisé, et quel est le problème avec mon code?

Grâce


ListActivity

public class ContactSelection extends ListActivity {

    private WhipemDBAdapter mDbHelper;
    private FriendAdapter friendAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDbHelper = new WhipemDBAdapter(this);
        mDbHelper.open();     

        setContentView(R.layout.contact_list);   

        Cursor c = mDbHelper.fetchAllFriends();
        startManagingCursor(c);     
        String[] from = new String[] {};
        int[] to = new int[] {};

        this.friendAdapter = new FriendAdapter(this, R.layout.contact_row, c, from, to);
        setListAdapter(this.friendAdapter);

        getListView().setItemsCanFocus(false);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDbHelper.open();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDbHelper.close();
    }
}

Personnalisé SimpleCursorAdapter

public class FriendAdapter extends SimpleCursorAdapter implements OnClickListener {
private Context mContext;
private int mLayout;
public FriendAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mLayout, parent, false);     
String name = c.getString(c.getColumnIndex(WhipemDBAdapter.KEY_NAME));
String fb_id = c.getString(c.getColumnIndex(WhipemDBAdapter.KEY_FB_ID));
TextView name_text = (TextView) v.findViewById(R.id.contact_name);
if (name_text != null) {
name_text.setText(name);
}
ImageView im = (ImageView) v.findViewById(R.id.contact_pic);            
Drawable drawable = LoadImageFromWebOperations("http://graph.facebook.com/"+fb_id+"/picture");
if (im != null) {
im.setImageDrawable(drawable);
}
CheckBox bCheck = (CheckBox) v.findViewById(R.id.checkbox);
if (im != null) {
bCheck.setTag(fb_id);
}            
if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
bCheck.setChecked(true);
bCheck.setOnClickListener(this);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
String name = c.getString(c.getColumnIndex(WhipemDBAdapter.KEY_NAME));
String fb_id = c.getString(c.getColumnIndex(WhipemDBAdapter.KEY_FB_ID));
TextView name_text = (TextView) v.findViewById(R.id.contact_name);
if (name_text != null) {
name_text.setText(name);
}
ImageView im = (ImageView) v.findViewById(R.id.contact_pic);            
Drawable drawable = LoadImageFromWebOperations("http://graph.facebook.com/"+fb_id+"/picture");
if (im != null) {
im.setImageDrawable(drawable);
}
CheckBox bCheck = (CheckBox) v.findViewById(R.id.checkbox);
if (im != null) {
bCheck.setTag(fb_id);
}
ArrayList<String> dude = ((GlobalVars) mContext.getApplicationContext()).getSelectedFriendList();
if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
bCheck.setChecked(true);
bCheck.setOnClickListener(this);
}
@Override
public void onClick(View v) {
CheckBox cBox = (CheckBox) v;
String fb_id = (String) cBox.getTag();
if (cBox.isChecked()) {
if (!((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
((GlobalVars) mContext.getApplicationContext()).addSelectedFriend(fb_id);
} else {
if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(fb_id))
((GlobalVars) mContext.getApplicationContext()).removeSelectedFriend(fb_id);
}
}
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}

source d'informationauteur jul