2011-04-28 73 views
0

您好我的列表视图使用光标适配器,我的问题是当我把我的应用程序在后台,在重新调整我的屏幕是空的,在onresume我打开数据库并创建光标仍然我有问题,如何我可以解决我的问题,请帮助我。Android简单光标适配器

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); 
+1

发布您的代码将有所帮助! – 2011-04-28 04:45:24

+0

告诉我你的代码....我可以帮你 – Kandha 2011-04-28 04:48:28

+0

@willytate,@ john mike willey,你现在可以帮我 – Bytecode 2011-04-29 07:05:43

回答

1

你在谈论ListView并使用SimpleCursorAdapter。你不认为创建你自己的ArrayAdaptor不是更高效:你创建一个你自己的类来保存你的数据从你的数据库请求(迭代你的光标),然后填充该类的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/)。

+0

与simplecursor适配器,无需维护任何数组,所以我用它。 – Bytecode 2011-04-30 14:25:34