jeudi 18 juin 2015

Selected item on listview not removed android sqlite

I have an bookmark option in my application ,if i select the item on list view to remove,share the selected position is not shared or deleted...for example if i select the 2 position last position will be shared ,other wise the cursor value will be null any one help me to share or remove the correct position Advance in thanks.

In adapter class:

public List getBookMarks() {

    try{

        SQLiteDatabase db =myDataBase;// getReadableDatabase();

        SQLiteQueryBuilder qb1 = new SQLiteQueryBuilder();

        String [] sqlSelect1 = {"0 _id", KEY_NAME,KEY_PH_NO,KEY_ID}; 

        String sqlTables1 = TABLE_BOOKMARK;

        String strQuery1=KEY_PH_NO+"='1'";

        strOrder1=KEY_ID + "  DESC";

        //strOrder1= KEY_ID+"BETWEEN"+"MAX"+(KEY_ID)+"AND"+"MIN"+(KEY_ID);

        qb1.setTables(sqlTables1);

        String strvalues="";

        Cursor curs = qb1.query(db, sqlSelect1, strQuery1, null,null, null, strOrder1);

        if(curs!=null)

        {
            List<Cursor> listCursor = new ArrayList<Cursor>();

            curs.moveToFirst();

            if(curs.getCount()>=1)

            {

                do

{ strvalues+=""+curs.getString(curs.getColumnIndex(KEY_NAME))+",";

                    String strID = curs.getString(curs.getColumnIndex(KEY_NAME));
                    //strID+="-1";


                    String [] sqlSelect = {"0 _id", "TB","Version","Book","Chapter","NKJ"}; 

                    String sqlTables = "hindibible";

                    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

                    qb.setTables(sqlTables);

                    StringBuilder strQuery = new StringBuilder();

                    strQuery.append(" ID = "+strID); 

                    Cursor bible = qb.query(db, sqlSelect, strQuery.toString(), null,null, null,null);

                //  bible.moveToNext();

                    listCursor.add(bible);

                } 

while (curs.moveToNext());

                return listCursor;

            }

            else

            {

            }
        }

        else

        {

        }

    }

catch (Exception e) {

        Log.d("setBookMark",e.toString());

    }
    return null;

}

In ListActivity Fav method:

private void showFav()

{

    try

{

        List<Cursor> listCursor = db.getBookMarks();

        ArrayList<String> list=new ArrayList<String>();

        ArrayList<String> list_date=new ArrayList<String>();

        ArrayList<String> list_chapter=new ArrayList<String>();

        ArrayList<String> versenum=new ArrayList<String>();


        for(int i=0;i<listCursor.size();i++)

        {

             cursor = listCursor.get(i);


            if(MiddlewareInterface.lIntlanguage==0||MiddlewareInterface.lIntlanguage==2)
                this.mathstxt=getApplicationContext().getResources().getStringArray(R.array.Bookwi);

    else

                this.mathstxt=getApplicationContext().getResources().getStringArray(R.array.EngBookwi);

            try

            {
                if (cursor.moveToFirst())
                {

                    String strcursor=cursor.getString(cursor.getColumnIndex("NKJ"));

                    strcursor.replace("<br>","");

                    list.add(strcursor);


                    String strversenum=cursor.getString(cursor.getColumnIndex("Version"))

; strversenum.replace("
","");

                    versenum.add(strversenum);

                    short 

jbook=cursor.getShort(cursor.getColumnIndex("Book"));

                    short 

ichap=cursor.getShort(cursor.getColumnIndex("Chapter"));

                    short ivers=cursor.getShort(cursor.getColumnIndex("Version"));


                    String strdata=mathstxt[jbook-1]+" "+ichap+"  "+ivers;
                    list_chapter.add(strdata);

                    /*} while (cursor.moveToNext());*/


                }


            }

catch (Exception e) {

                e.printStackTrace();

                // TODO: handle exception

            }

        }


        Cursor curDate=db.getdate();

    //  gcursor=cursor;

        if (curDate.moveToFirst())
        {

            do

            {

                String strcursor=curDate.getString(curDate.getColumnIndex("Date"));

                strcursor.replace("<br>","");

                list_date.add(strcursor);


            } while (curDate.moveToNext());

        }

        String verse[]=new String[list.size()];

        verse=list.toArray(verse);

        String date[]=new String[list_date.size()];

        date=list_date.toArray(date);

        String versenumbr[]=new String[versenum.size()];

        versenumbr=versenum.toArray(versenumbr);


        String chapterversebook[]=new String[list_chapter.size()];

        chapterversebook=list_chapter.toArray(chapterversebook);

        gcursor=cursor;

        this.m_adapter = new Bookmark_Adapter(this, list,list_date,list_chapter,versenum);

        listview.setAdapter(this.m_adapter);

        listview.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {


                contposition=pos;

                share_verse(pos);


                return false;
            }

        });

    }

catch (Exception e) {

        Log.d("SSSSS","111="+e.toString());

    }
}

Share option method:

private void share_verse(int pos) {

    try

    {

        inflatedsharelayout.setVisibility(View.VISIBLE);

        inflatedsharelayout.startAnimation(share_show);

        listview.setEnabled(false);

        backimg.setEnabled(false);

        dash_bord.setEnabled(false);

        int verse=pos;

        Log.d("SSSSS","verse="+verse);

        String strdata = null;

        String strBible="";

        pos_txt=pos;

        //M
        int lIntVerse=-1,lIntBookId=-1,lIntchapter=-1;

        List<Cursor> lstcurs=db.getBookMarks();

        int lenght=lstcurs.size();


        for(int i=0;i<lstcurs.size();i++)

        {

            gcursor.moveToFirst();

            try

            {

            if(gcursor.getCount()>=1)

            {

            if(i==pos)

            {
                lIntBookId=gcursor.getShort(gcursor.getColumnIndex("Book"));

                lIntchapter=gcursor.getShort(gcursor.getColumnIndex("Chapter"));

                lIntVerse=gcursor.getShort(gcursor.getColumnIndex("Version"));


                strBible=gcursor.getString(gcursor.getColumnIndex("NKJ"));


                book=lIntBookId;chapter=lIntchapter;version=lIntVerse;  
            }

            }

            }

catch(Exception e){

                e.printStackTrace();

            }

        }

        try{

            int cnt=0;

            if(gcursor!=null)

            {

                gcursor.moveToFirst();


                try

                {

                if(gcursor.getCount()>=1)

                {
                    do {


                        if(cnt==pos)


                        {
                            lIntBookId=gcursor.getShort(gcursor.getColumnIndex("Book"));

                            lIntchapter=gcursor.getShort(gcursor.getColumnIndex("Chapter"));

lIntVerse=gcursor.getShort(gcursor.getColumnIndex("Version"));

                            strBible=gcursor.getString(gcursor.getColumnIndex("NKJ"));

                            book=lIntBookId;chapter=lIntchapter;version=lIntVerse;

                            break;

                        }

                        cnt++;

                    } while (gcursor.moveToNext());

                }
                }catch (Exception e) {

                    // TODO: handle exception

                    e.printStackTrace();

                }


                if(lIntVerse!=-1 && lIntBookId!=-1 && lIntchapter!=-1)


                {

                    String

mathstxt[]=getResources().getStringArray(R.array.EngBookwi);

                    strdata=mathstxt[lIntBookId-1]+" "+lIntchapter+" :"+lIntVerse;

                }


            }   


        }

catch (Exception e) {

            // TODO: handle exception

        }

share_list.setOnItemClickListener(new OnItemClickListener() {

                @Override

                public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                        long arg3) {

if(pos==3)

                        {

                            removebookmark_popup(pos_txt, strVerse, strBook);

                            inflatedsharelayout.setVisibility(View.GONE);

                            inflatedsharelayout.startAnimation(share_hide);

                            listview.setEnabled(true);

                            backimg.setEnabled(true);

                            dash_bord.setEnabled(true);

                        }

                        else

                        {

                            removebookmark_popup(pos_txt, strVerse, strBook);
                            inflatedsharelayout.setVisibility(View.GONE);

                            inflatedsharelayout.startAnimation(share_hide);

                            listview.setEnabled(true);

                            backimg.setEnabled(true);

                            dash_bord.setEnabled(true);

                        }

}

Aucun commentaire:

Enregistrer un commentaire