0

リストビューにデータをアルファベット順に自動的に表示するにはどうすればよいですか。私のエミュレーターでは、データベースでの配置方法に従ってデータを配置するためです。

誰でもこの問題について説明できますか?

これは、データベースからデータを取得する方法です。

 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;
    }

}
4

5 に答える 5

1

興味のある順序で並べ替えてデータベースからデータを取得し、次に arrayadapter に追加します。これで問題が解決する場合があります

于 2012-08-14T06:25:10.870 に答える
1

データベースから情報を取得するためにクエリを使用していますか? もしそうなら、次のような SQL ステートメントを使用してみませんか。

SELECT * FROM your_table ORDER BY your_column 
于 2012-08-14T06:32:57.050 に答える
0
public Cursor fetchAllData() {
        return database.query(urtablename, null, null, null, null, null,
                null);
    }

カーソルを使用してすべてのデータを返します。アクティビティでは

 Cursor cr=dba.fetchAllData();
    cr.movetofirst();
    while(!cr.isAfterLast())
    {
    //fetch all data using column name
    //like cr.getString(cr.getColumnIndex("ur column name"));
    //it will return as per in dATABASE
    cr.movetoNext();
    }
于 2012-08-14T06:57:47.787 に答える
0

必要に応じてカスタム コンパレータを作成します。そして今これをArrayAdapterのソート機能で使用します。

于 2012-08-14T06:58:08.087 に答える
0

コンストラクターでこれを試してください:

public imageadapter(Context context, String[] mobileValues, String[] mobileValues1) {
    this.context = context;
    this.mobileValues = mobileValues;
    this.mobileValues2 = mobileValues1;

Arrays.sort(mobileValues);



//Arrays.sort(mobileValues2);

}

楽しい。

于 2012-08-14T08:14:24.820 に答える