0

こんにちは、リストビューにカーソルアダプターを使用しています。アプリケーションをバックグラウンドに置いたときに問題が発生し、画面を再調整すると空になりました。onresume でデータベースを開いてカーソルを作成しましたが、まだ問題があります。どうすれば問題を解決できますか?助けてください。

searchCursor= dbReaderContact.rawQuery(query, null);
startManagingCursor(searchCursor);
String[] from=new String[] {ALDbAdapter.TITLE,DbAdapter.CATID,DbAdapter.LTID,DbAdapter.RK,DbAdapter.SUBTITLE};
                        int [] to=new int[] {R.layout.ctllist_item};
catSearchAdapter=new CategorySearchAdapter(context, R.layout.ctllist_item, searchCursor, from, to);

//////////////Adapter calss

public class CategorySearchAdapter extends SimpleCursorAdapter implements Filterable{

    private Context context;
    private int layout;
    private ALDbAdapter dbadapter;

    /**
     * @param context
     * @param layout
     * @param c
     * @param from
     * @param to
     */
    public CategorySearchAdapter(Context context, int layout, Cursor c, String[] from,  int[] to) {
        super(context, layout, c, from, to);
        this.layout=layout;
        this.context=context;

        dbadapter=new ALDbAdapter(context);
        try {
            dbadapter.openAngiesListDb();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

     @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

            StringBuilder buffer = null;
            String[] args = null;
            if (constraint != null) {
                buffer = new StringBuilder();
                buffer.append("UPPER(");
                buffer.append("name");
                buffer.append(") GLOB ?");
                args = new String[] { constraint.toString().toUpperCase() + "*" };
            }
            Cursor c=null;
           // c=dbadapter.getPartialNamesSearch(constraint.toString());
            return c;
        }
     @Override
        public void bindView(View v, Context context, Cursor c) {

            TextView subTitle=(TextView)v.findViewById(R.id.ctlName);
            TextView title=(TextView)v.findViewById(R.id.cltAssocationName);
            ImageView imgIcon=(ImageView)v.findViewById(R.id.ctlLogo);

            int subTitInd=c.getColumnIndex(ALDbAdapter.SUBTITLE);
            int titInd=c.getColumnIndex(ALDbAdapter.TITLE);
            int ltId=c.getColumnIndex(ALDbAdapter.LTID);
            int ltype=c.getInt(ltId);
            if(!c.getString(subTitInd).equalsIgnoreCase("")){
                subTitle.setText(c.getString(subTitInd));
                title.setText(c.getString(titInd));
            }else{
                subTitle.setText(c.getString(titInd));
            }
            if(ltype==1){
                imgIcon.setImageResource(R.drawable.logo1);
            }else if(ltype==2){
                imgIcon.setImageResource(R.drawable.logo2); 
            }else{
                imgIcon.setImageResource(R.drawable.logo3); 
            }
        }

}
//On post i closed the cursor
///On Resume

searchCursor= dbReaderContact.rawQuery(query, null);
            startManagingCursor(searchCursor);
4

1 に答える 1

1

ListView について話し、SimpleCursorAdapter を使用しています。独自の ArrayAdaptor を作成するのは効率的ではないと思いませんか?DB リクエストから必要なデータを保持する独自のクラスを作成し (カーソルを反復処理)、そのクラスの ArrayList を設定します。次に、この ArrayList を ArrayAdaptor を拡張するクラスに渡します。ArrayList を ArrayAdaptor に格納し、コンストラクターと public View getView(int position, View convertView, ViewGroup parent) メソッドをオーバーライドして、ListView の各ビューを作成する必要があります。

カスタム ListView をググることができます (最初の結果: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ )。

于 2011-04-30T12:52:19.713 に答える