使光标对应的数组具有多行,后跟相同的行id' s

时间:2018-01-20 20:20:15

标签: arrays database rows android-cursoradapter android-cursor

我有一个数据库,其中id在几行之后发生变化,然后在某些行中相同,然后更改我的数据。我想要的是,相同的我的值将在一行中填充listview,然后当Ids相同时,它们都在一行中。因此,下一行填充相同的行,这些行具有相同的ID。

我的数据库列和行如下:

Verse_id ------- words_id ---------- words_ar ------ translate_bn -

1.     Mamun.           Salam             John 
       Assistant.       Operator.        Assistant 
------------------------------------------------------------------
2.     Smith.            Roger.  
       Manager.         Director 
------------------------------------------------------------------
3.      Qoel.            Saki.              Ali. 
        Helper.        Mechanics.          Getman

我希望这个DB分为三行,如下所示:(listview):

private static final String PRIMARY_ID = "_id";
    private static final String TABLE_NAME = "bywords";
    private static final String FRIEND_ID = "verse_id";
    private static final String WORDS_ID = "words_id";
    private static final String WORDS_bN = "translate_bn";
    private static final String WORDS_AR = "words_ar";

private SQLiteDatabase database;
    private ArrayList<String> friends;

    private ArrayList<String> trans1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        mContext = this;

        ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();

        Cursor cursor = database.rawQuery("SELECT * FROM  bywords", null);



        ListView list4=(ListView)findViewById(R.id.list4)   ;

        String[] from = {WORDS_AR }; // _id mandatory
        int[] to = new int[]{R.id.alquran_text};

        CursorAdapter adapter = new SimpleCursorAdapter(
                mContext,
                R.layout.list_item,
                cursor,
                from,
                to,
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);


        list4.setAdapter(adapter);

我试过两种方式: 第一:

    trans1 = new ArrayList<String>();

        String[] allColumns2 = new String[] { FRIEND_ID,
                BN_TRANS };

        Cursor friendCursor2 = database.query(TABLE_NAME, allColumns2, null,
                null, null, null, null);


        if (friendCursor2 != null) {
            friendCursor2.moveToFirst();
        }
        //return friendCursor;


        if(!friendCursor2.isAfterLast()) {
            do {
                String name = friendCursor2.getString(1);
                trans1.add(name);
            } while (friendCursor2.moveToNext());
        }

        friendCursor2.close();





    String [] bntrans1= new String[trans1.size()];
                bntrans1 = trans1.toArray(bntrans1);





ListAdapter adapter = new ArrayAdapter<String>(
                        getApplicationContext(), R.layout.alert_row, seleted_route) {

                    ViewHolder holder;
                    Drawable icon;

                    class ViewHolder {
                        ImageView icon;
                        TextView title;
                        TextView title2;
                    }

                    public View getView(int position, View convertView,
                                        ViewGroup parent) {
                        final LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                                .getSystemService(
                                        Context.LAYOUT_INFLATER_SERVICE);

                        if (convertView == null) {
                            convertView = inflater.inflate(
                                    R.layout.alert_row, null);

                            holder = new ViewHolder();
                            //holder.icon = (ImageView) convertView
                            //      .findViewById(R.id.icon4);
                            holder.title = (TextView) convertView
                                    .findViewById(R.id.title4);
                            holder.title2 = (TextView) convertView
                                    .findViewById(R.id.title2);


                            convertView.setTag(holder);
                        } else {
                            // view already defined, retrieve view holder
                            holder = (ViewHolder) convertView.getTag();
                        }

                        Drawable drawable = getResources().getDrawable(R.drawable.iqra1); //this is an image from the drawables folder

                        holder.title.setText(seleted_route[position]);
                        holder.title2.setText(seleted_route[position]);
                        //holder.icon.setImageDrawable(drawable);

                        return convertView;
                    }
                };

然后第二:

a

通过第二种方式,我可以在一行中的两个文本视图中填充两列。但是我想要的是,具有相同行ID的相应行必须在列表视图的一行中填充第一节&#d; d。 id的whataver在行中相同可以是5,10或20行相同的id。每当相同的Id它们是一行时,当Id改变时,换行开始。

0 个答案:

没有答案