与ListView和自定义光标适配器的Andr​​oid奇怪的行为 [英] Android strange behavior with listview and custom cursor adapter

查看:200
本文介绍了与ListView和自定义光标适配器的Andr​​oid奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图问题,自定义光标适配器,我只是似乎无法弄清楚什么是错我的code。基本上,我的活动我打电话initalize(),做了一堆东西处理得到适当的数据和初始化列表视图。在活动的第一次运行,你可以从该项目中的一个列表中缺少图像看。如果我去另一个活动,并返回到本次活动唯一缺少的项目出现。我相信这事做的setContentView(R.layout.parent)。如果我动议我的初始化(),那么该项目从来没有从另一个活动返回时显示出来。因此,出于某种原因,从另一个活动返回绕过的setContentView(R.layout.parent),一切工作正常。我知道这是不可能的,我绕过的setContentView(R.layout.parent),所以我需要搞清楚的问题是什么。另外,我不包括布局,因为它是没有两个以上textviews。

另外,我还附加了图像不显示缺少的产品列表中的最后一个。

自定义光标适配器:

 公共类CustomCursorAdapter扩展SimpleCursorAdapter {私人上下文的背景下;
私人诠释布局;公共CustomCursorAdapter(上下文的背景下,INT布局,
      光标C,的String []从,INT []到){
    超(背景下,布局,C,从,到);
    this.context =背景;
    this.layout =布局;
}公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){ LayoutInflater吹气= LayoutInflater.from(背景);
 最后查看视图= inflater.inflate(布局,父母,假); 返回视图。
}@覆盖
公共无效bindView(视图V,上下文的背景下,光标C){    如果(c.getColumnName(0).matches(部分)){        INT nameCol = c.getColumnIndex(部分);
        弦乐部分= c.getString(nameCol);        TextView的section_text =(TextView中)v.findViewById(R.id.text1);
        如果((section.length()大于0)){            section_text.setText(部分);        }其他{
         //所以我们没有一个空点
            section_text.setText();
            section_text.setVisibility(2);
            section_text.setHeight(1);
        }    }否则如果(c.getColumnName(0).matches(code)){
        INT nameCol = c.getColumnIndex(code);
        串M code = c.getString(nameCol);
        TextView的code_text =(TextView中)v.findViewById(R.id.text1);
        如果(code_text!= NULL){
      INT I = 167;
      字节[]数据= {(字节)I};
      字符串strSymbol = EncodingUtils.getString(数据窗口1252);         M code = strSymbol ++ M code;
            code_text.setText(M code);
            code_text.setSingleLine();
        }    }    如果(c.getColumnName(1).matches(称号)){        INT nameCol = c.getColumnIndex(标题);
        字符串mTitle = c.getString(nameCol);
        TextView的title_text =(TextView中)v.findViewById(R.id.text2);
        如果(title_text!= NULL){            title_text.setText(mTitle);
        }    }否则如果(c.getColumnName(1).matches(节选)){
        INT nameCol = c.getColumnIndex(节选);
        字符串mExcerpt = c.getString(nameCol);
        TextView的excerpt_text =(TextView中)v.findViewById(R.id.text2);
        如果(excerpt_text!= NULL){            excerpt_text.setText(mExcerpt);
            excerpt_text.setSingleLine();        }
    }
}

活动:

 公共类的父扩展ListActivity {
私有静态的String [] = TITLE_FROM {SECTION,TITLE,_ID,};
私有静态的String [] code_FROM = {code,摘录,_ID,};
私人静态字符串ORDER_BY = _ID +ASC;
私有静态诠释[] TO = {R.id.text1,R.id.text2,};字符串痕迹= NULL;私人迈德特数据;
私人SQLiteDatabase分贝;
CharSequence的PARENT_ID =;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
 super.onCreate(savedInstanceState);
    数据=新迈德特(本);    DB = data.getReadableDatabase();    的setContentView(R.layout.parent);
    初始化();
}公共无效的initialize(){ 面包屑= NULL;
 捆绑馍= getIntent()getExtras()。
 TextView的tvBreadCrumb;
 tvBreadCrumb =(的TextView)findViewById(R.id.breadcrumb);    如果(BUN == NULL){
     //这是第一次运行
     tvBreadCrumb.setText(NULL);
  tvBreadCrumb.setHeight(0);
        PARENT_ID =0;
        尝试{
         光标光标=的getData(PARENT_ID);
         showSectionData(光标);
        } {最后
         data.close();
        }
    }其他{
     CharSequence的状态= bun.getString(国家);
  面包屑= bun.getString(面包屑);
  tvBreadCrumb.setText(痕迹);  CharSequence的code = bun.getString(code);
        PARENT_ID = code;     如果(state.equals(章)){            尝试{
             光标光标=的getData(PARENT_ID);
             showSectionData(光标);
            } {最后
             data.close();
            }     }否则如果(state.equals(code)){            尝试{
             光标光标=得到codeDATA(PARENT_ID);
             展会codeDATA(光标);
            } {最后
             data.close();
            }     }
    }
}@覆盖
公共无效调用onStart(){ //初始化();
 super.onResume();}@覆盖
公共无效onResume(){ 初始化();
 super.onResume();
}
私人光标的getData(CharSequence的PARENT_ID){ 光标CTITLES = db.query(TITLES_TABLE_NAME,TITLE_FROM,PARENT_ID =+ PARENT_ID,NULL,NULL,NULL,ORDER_BY);
 光标C codeS = db.query(codeS_TABLE_NAME,code_FROM,PARENT_ID =+ PARENT_ID,NULL,NULL,NULL,ORDER_BY);
 光标[] C = {CTITLES,C codeS};
 光标光标=新MergeCursor(C);
 startManagingCursor(光标);
 返回游标;
}私人光标得到codeDATA(CharSequence的parent_id2){
    捆绑馍= getIntent()getExtras()。
 CharSequence的意图= bun.getString(意图);
 CharSequence的检索类别= bun.getString(搜索类型);
  // SQLiteDatabase分贝= data.getReadableDatabase();
 如果(意向!= NULL){
  字符串sWhere = NULL;
      如果(searchtype.equals(code)){
       sWhere =code LIKE'%+ parent_id2 +%';
      }否则如果(searchtype.equals(内)){
       sWhere =定义LIKE'%+ parent_id2 +%';
      }
      //这是一个搜索请求
  光标光标= db.query(codeS_TABLE_NAME,code_FROM,sWhere,NULL,NULL,NULL,ORDER_BY);
  startManagingCursor(光标);
     返回游标;
 }其他{
  光标光标= db.query(codeS_TABLE_NAME,code_FROM,PARENT_ID =+ parent_id2,NULL,NULL,NULL,ORDER_BY);
  startManagingCursor(光标);
     返回游标;
 }}私人无效showSectionData(光标光标){
 CustomCursorAdapter适配器=新CustomCursorAdapter(这一点,R.layout.item,光标,TITLE_FROM,TO);
 setListAdapter(适配器);
}私人无效秀codeDATA(光标光标){
 CustomCursorAdapter适配器=新CustomCursorAdapter(这一点,R.layout.item,光标,code_FROM,TO);
 setListAdapter(适配器);
 捆绑馍= getIntent()getExtras()。
 CharSequence的意图= bun.getString(意图);
 如果(意向!= NULL){
  光标光标1 =((CursorAdapter的)getListAdapter())getCursor();
  startManagingCursor(光标1);
  TextView的tvBreadCrumb;
  tvBreadCrumb =(的TextView)findViewById(R.id.breadcrumb);
  tvBreadCrumb.setText(cursor1.getCount()+找到的记录);
  //cursor1.close(); // MDL
 }
}


解决方案

我想通了这个问题,因为我怀疑它是与我的code的一个问题。设置TextView的可见性view.GONE或view.INVISIBLE的伟大工程。问题是,我永远不会再使它可见。我从来没有所谓view.VISIBLE并引起一大堆问题。

 如果(c.getColumnName(0).matches(部分)){    INT nameCol = c.getColumnIndex(部分);
    弦乐部分= c.getString(nameCol);    TextView的section_text =(TextView中)v.findViewById(R.id.text1);
    如果((section.length()大于0)){        section_text.setText(部分);
        //我忘了这!!!!!
        section_text.setVisibility(view.VISIBLE);    }其他{
     //所以我们没有一个空点
        section_text.setVisibility(view.GONE);    }}

I have a problem with a list view and a custom cursor adapter and I just can't seem to figure out what is wrong with my code. Basically, in my activity I call initalize() that does a bunch of stuff to handle getting the proper data and initializing the listview. On first run of the activity you can see from the images that one of the items is missing from the list. If I go to another activity and go back to this activity the item that was missing shows up. I believe it has something to do with setContentView(R.layout.parent). If I move that to my initialize() then the item never shows up even when returning from another activity. So, for some reason, returning from another activity bypasses setContentView(R.layout.parent) and everything works fine. I know it's impossible for me to bypass setContentView(R.layout.parent) so I need to figure out what the problem is. Also, I did not include the layout because it is nothing more then two textviews.

Also, the images I have attached do not show that the missing item is the last one on the list.

Custom Cursor Adapter:

public class CustomCursorAdapter extends SimpleCursorAdapter {

private Context context;
private int layout;

public CustomCursorAdapter (Context context, int layout, 
      Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.context = context;
    this.layout = layout;
}

public View newView(Context context, Cursor cursor, ViewGroup parent) {

 LayoutInflater inflater = LayoutInflater.from(context);
 final View view = inflater.inflate(layout, parent, false);

 return view;
}

@Override
public void bindView(View v, Context context, Cursor c) {

    if (c.getColumnName(0).matches("section")){

        int nameCol = c.getColumnIndex("section");
        String section = c.getString(nameCol);

        TextView section_text = (TextView) v.findViewById(R.id.text1);
        if ((section.length() > 0)) {

            section_text.setText(section);

        }   else {
         //so we don't have an empty spot
            section_text.setText("");
            section_text.setVisibility(2);
            section_text.setHeight(1);
        }

    } else if (c.getColumnName(0).matches("code")) {


        int nameCol = c.getColumnIndex("code");
        String mCode = c.getString(nameCol);
        TextView code_text = (TextView) v.findViewById(R.id.text1);
        if (code_text != null) {
      int i = 167;
      byte[] data = {(byte) i};
      String strSymbol = EncodingUtils.getString(data, "windows-1252");

         mCode = strSymbol + " " + mCode;
            code_text.setText(mCode);
            code_text.setSingleLine();
        }

    }

    if (c.getColumnName(1).matches("title")){

        int nameCol = c.getColumnIndex("title");
        String mTitle = c.getString(nameCol);
        TextView title_text = (TextView) v.findViewById(R.id.text2);
        if (title_text != null) {

            title_text.setText(mTitle);
        }

    } else if (c.getColumnName(1).matches("excerpt")) {


        int nameCol = c.getColumnIndex("excerpt");
        String mExcerpt = c.getString(nameCol);
        TextView excerpt_text = (TextView) v.findViewById(R.id.text2);
        if (excerpt_text != null) {

            excerpt_text.setText(mExcerpt);
            excerpt_text.setSingleLine();

        }
    }
}

The Activity:

public class parent extends ListActivity {
private static String[] TITLE_FROM = {  SECTION, TITLE, _ID,  };
private static String[] CODE_FROM = { CODE, EXCERPT, _ID,  };
private static String ORDER_BY = _ID + " ASC";
private static int[] TO = { R.id.text1, R.id.text2, };



String breadcrumb = null;

private MyData data;
private SQLiteDatabase db;
CharSequence parent_id = "";


@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);      


    data = new MyData(this);

    db = data.getReadableDatabase();

    setContentView(R.layout.parent);


    initialize();


}

public void initialize() {

 breadcrumb = null;
 Bundle bun = getIntent().getExtras();
 TextView tvBreadCrumb;
 tvBreadCrumb = (TextView)findViewById(R.id.breadcrumb); 

    if (bun == null) {
     //this is the first run 
     tvBreadCrumb.setText(null);
  tvBreadCrumb.setHeight(0);
        parent_id = "0";
        try {
         Cursor cursor = getData(parent_id);
         showSectionData(cursor);
        } finally {
         data.close();
        }
    } else {
     CharSequence state = bun.getString("state");
  breadcrumb = bun.getString("breadcrumb");
  tvBreadCrumb.setText(breadcrumb);

  CharSequence code = bun.getString("code");
        parent_id = code;

     if (state.equals("chapter")) {

            try {
             Cursor cursor = getData(parent_id);
             showSectionData(cursor);
            } finally {
             data.close();
            }

     } else if (state.equals("code")) {

            try {
             Cursor cursor = getCodeData(parent_id);
             showCodeData(cursor);
            } finally {
             data.close();
            }

     }
    }
}

@Override
public void onStart() {

 //initialize();
 super.onResume();

}

@Override
public void onResume() {

 initialize();
 super.onResume();
}


private Cursor getData(CharSequence parent_id) {

 Cursor cTitles = db.query(TITLES_TABLE_NAME, TITLE_FROM, "parent_id = " + parent_id, null, null, null, ORDER_BY);
 Cursor cCodes = db.query(CODES_TABLE_NAME, CODE_FROM, "parent_id = " + parent_id, null, null, null, ORDER_BY);
 Cursor[] c = {cTitles, cCodes};
 Cursor cursor = new MergeCursor(c);
 startManagingCursor(cursor);
 return cursor;
}

private Cursor getCodeData(CharSequence parent_id2) {
    Bundle bun = getIntent().getExtras();
 CharSequence intent = bun.getString("intent");
 CharSequence searchtype = bun.getString("searchtype");
  //SQLiteDatabase db = data.getReadableDatabase();
 if (intent != null) {
  String sWhere = null; 
      if(searchtype.equals("code")) {
       sWhere = "code LIKE '%"+parent_id2+"%'";
      } else if(searchtype.equals("within")){
       sWhere = "definition LIKE '%"+parent_id2+"%'";
      }
      //This is a search request
  Cursor cursor = db.query(CODES_TABLE_NAME, CODE_FROM, sWhere, null, null, null, ORDER_BY); 
  startManagingCursor(cursor);
     return cursor;
 } else {
  Cursor cursor = db.query(CODES_TABLE_NAME, CODE_FROM, "parent_id = "+ parent_id2, null, null, null, ORDER_BY);
  startManagingCursor(cursor);
     return cursor;
 }

}

private void showSectionData(Cursor cursor) {
 CustomCursorAdapter adapter= new CustomCursorAdapter(this, R.layout.item, cursor, TITLE_FROM, TO);
 setListAdapter(adapter);
}

private void showCodeData(Cursor cursor) {
 CustomCursorAdapter adapter = new CustomCursorAdapter(this, R.layout.item, cursor, CODE_FROM, TO);
 setListAdapter(adapter);
 Bundle bun = getIntent().getExtras();
 CharSequence intent = bun.getString("intent");
 if (intent != null) {
  Cursor cursor1 = ((CursorAdapter)getListAdapter()).getCursor();
  startManagingCursor(cursor1);
  TextView tvBreadCrumb;
  tvBreadCrumb = (TextView)findViewById(R.id.breadcrumb); 
  tvBreadCrumb.setText(cursor1.getCount() + " Records Found");
  //cursor1.close(); //mdl
 }
}

解决方案

I figured out the problem and as I suspected it was a problem with my code. Setting the TextView's visibility to view.GONE or view.INVISIBLE works great. The problem was that I never made it visible again. I never called view.VISIBLE and that caused a whole bunch of problems.

 if (c.getColumnName(0).matches("section")){

    int nameCol = c.getColumnIndex("section");
    String section = c.getString(nameCol);

    TextView section_text = (TextView) v.findViewById(R.id.text1);
    if ((section.length() > 0)) {

        section_text.setText(section);
        // I forgot this!!!!!
        section_text.setVisibility(view.VISIBLE);

    }   else {
     //so we don't have an empty spot
        section_text.setVisibility(view.GONE);

    }

}

这篇关于与ListView和自定义光标适配器的Andr​​oid奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆