我已经创建了一个接收器,它接收从SQLite数据库加载到Listview的Cursor和Context。以下是我传递Cursor的方法:
aCursor = mDb.rawQuery("SELECT * FROM Chat", null);
aCursor.moveToFirst();
adapter = new ChatAdapter(this,aCursor);
在这里我的适配器:
public ChatAdapter(Context context,Cursor allentries) {
this.mContext = context;
this.mCursor=allentries;
// BusProvider.getInstance().register(this);
mCursor.moveToFirst();
mLayoutInflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mCursor.getCount();
}
@Override
public Object getItem(int position) {
return mCursor.getString(position);
}
@Override
public long getItemId(int position) {
return mCursor.getPosition();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
theMessage = (TextView) row.findViewById(R.id.comment);
theName = (TextView) row.findViewById(R.id.MSGname);
theImage = (ImageView)row.findViewById(R.id.MSGimage);
android.widget.FrameLayout.LayoutParams lp =
(android.widget.FrameLayout.LayoutParams) theName.getLayoutParams();
String Namer= mCursor.getString(mCursor.getColumnIndex("username"));
String namer[] = Namer.split("\\@");
String imagenamer=namer[0];
theName.setText(imagenamer);
theMessage.setText(mCursor.getString(mCursor.getColumnIndex("message")));
int isright=Integer.valueOf(mCursor.getString(mCursor.getColumnIndex("isright")));
if(isright==0){
theMessage.setBackgroundResource(R.drawable.bubble_yellow);
//theMessage.setBackgroundResource(isright==0 ? R.drawable.bubble_yellow : R.drawable.bubble_green);
wrapper.setGravity(Gravity.LEFT);
//wrapper.setGravity(isright==0 ? Gravity.LEFT : Gravity.RIGHT);
lp.gravity = Gravity.LEFT;
theName.setLayoutParams(lp);
android.widget.FrameLayout.LayoutParams params = new android.widget.FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT);
theImage.setLayoutParams(params);
theImage.setPadding(10,0,0,0);
wrapper.setPadding( ImageWidth+10,40,0, 0);
theName.setPadding( ImageWidth+25, 0,0, 0);
//(left, top, right, bottom)
File file= new File("storage/sdcard/LifeMatePrivate/ProfileImage/ProfileImage,imagechange_2,"+imagenamer+",.jpg");
if(file.exists()){
Picasso.with(mContext).load(new File("storage/sdcard/LifeMatePrivate/ProfileImage/ProfileImage,imagechange_2,"+imagenamer+",.jpg")).resize(ImageWidth,ImageHeight).centerCrop().into(theImage);
}else{
Picasso.with(mContext).load(new File("storage/sdcard/LifeMatePrivate/ProfileImage/Default.jpg")).resize(ImageWidth,ImageHeight).centerCrop().into(theImage);
}
}
}
一切都很好,但是当我调用adapter.NotifyDatasetChanged时,它返回索引-1请求大小为21,这意味着我还没有初始化我的Cursor。但根据我的理解,没有必要调用movetofirst()。但也许我错了。你能帮帮我吗?