我的列表视图如何自动按字母顺序显示数据。因为在我的模拟器中,它显示的数据是按照它在我的数据库中的排列方式排列的。
谁能解释这个问题?
这就是我从数据库中检索数据的方式。
SQLiteDatabase db = openOrCreateDatabase("database", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT DISTINCT category,cimage from FoodList", null);
int count = c.getCount();
String[] values = new String[count];
String[] values1 = new String[count];
c.moveToFirst();
for(int x = 0; x < count; x++){
values[x] = c.getString(c.getColumnIndex("category"));
values1[x] = c.getString(c.getColumnIndex("cimage"));
c.moveToNext();
}
list.setAdapter(new imageadapter(this,values,values1));
db.close();
c.close();
这是我的图像适配器活动
public class imageadapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
public final String[] mobileValues2;
public imageadapter(Context context, String[] mobileValues, String[] mobileValues1) {
this.context = context;
this.mobileValues = mobileValues;
this.mobileValues2 = mobileValues1;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewHolder holder = new viewHolder();
if (convertView == null) {
convertView = inflater.inflate(R.layout.logo, null);
holder.category = (TextView) convertView.findViewById(R.id.cat);
holder.image = (ImageView) convertView.findViewById(R.id.imglogo);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
holder.category.setText(mobileValues[position]);
holder.s = mobileValues2[position];
byte[] decodedString = null;
try {
decodedString = Base64.decode(holder.s, 0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.image.setImageBitmap(decodedByte);
return convertView;
}
public int getCount() {
return mobileValues.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
static class viewHolder{
ImageView image;
TextView category;
String s;
}
}