使用listview和自定义光标适配器的Android奇怪行为

使用listview和自定义光标适配器的Android奇怪行为,android,listview,cursor,Android,Listview,Cursor,我的列表视图和自定义游标适配器有问题,我似乎无法找出代码的错误。基本上,在我的活动中,我调用initalize()来处理获取正确数据和初始化listview的大量工作。在第一次运行“活动”时,您可以从图像中看到列表中缺少一个项目。如果我转到另一个活动并返回此活动,则会显示丢失的项目。我相信这与setContentView(R.layout.parent)有关。如果将其移动到我的initialize(),则即使从其他活动返回,该项也不会显示。因此,出于某种原因,从另一个活动返回时会绕过setCon

我的列表视图和自定义游标适配器有问题,我似乎无法找出代码的错误。基本上,在我的活动中,我调用initalize()来处理获取正确数据和初始化listview的大量工作。在第一次运行“活动”时,您可以从图像中看到列表中缺少一个项目。如果我转到另一个活动并返回此活动,则会显示丢失的项目。我相信这与setContentView(R.layout.parent)有关。如果将其移动到我的initialize(),则即使从其他活动返回,该项也不会显示。因此,出于某种原因,从另一个活动返回时会绕过setContentView(R.layout.parent),一切正常。我知道我不可能绕过setContentView(R.layout.parent),所以我需要找出问题所在。此外,我没有包括布局,因为它只是两个文本视图

此外,我所附的图片并没有显示缺少的项目是列表中的最后一个项目

自定义光标适配器:

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();

        }
    }
}
活动:

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
 }
}

我发现了这个问题,我怀疑这是我的代码出了问题。将文本视图的可见性设置为view.GONE或view.INVISIBLE效果很好。问题是我再也看不见它了。我从来没有调用view.VISIBLE,这导致了一大堆问题

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

    }

}

在您使用的列表视图中,如何指定布局高度?是“包装内容”吗?如果是,尝试一些硬编码的维度,比如240dp,检查在主活动和从另一个活动返回时是否工作正常,是否一致!?我试过你的建议,一切都顺理成章。这到底意味着什么?如果我在TextView布局的高度上设置了一个大小,一切都会正常工作。这并不能解决我的问题,因为我不想让TextView显示它是否没有任何内容。我只是注意到我提出的代码在光标适配器上的BindView中有一个错误,它应该是section_text.setText(“”);第四节文本设置可见性(2);第四节文本设置高度(1);是的,你的建议解决了这个问题。现在,我在顶部有一个空的文本视图,我真的想避免在没有显示的情况下出现空白。