【问题标题】:empty list view from sqlite cursor来自 sqlite 光标的空列表视图
【发布时间】:2011-12-09 23:33:43
【问题描述】:

我有一个有 5 条记录的 sqlite,我想用这个 bbdd 中的光标填充一个列表视图,但只创建一个带有白色位置的列表视图,任何地方都是 bbdd 查询的结果,没有内容。我认为是针对 textview 颜色但不是。谁能帮帮我?

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);   
    setContentView(R.layout.listatab);

     //Creamos la instancia de DataBaseHelper, un cursor y aplicamos el metodo getNombres al cursor.
     ayudabbdd = new DataBaseHelper(this);
     Cursor nombresC;     
     nombresC = (Cursor) ayudabbdd.getNombres();  
     nombresC.moveToFirst();
     startManagingCursor(nombresC);
     String[] datosRecibidos = new String[] {"nombre"};
    int[] layoutDondeLoPongo = new int[] { R.layout.entrada_lista};

     //Mientras el cursor no este vacio rellenamos la lista con el adaptador, que nos sirve para conectar unos datos con el listview.
     if(nombresC!=null){
         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listatab, nombresC,datosRecibidos, layoutDondeLoPongo); 
         this.setListAdapter(adapter);
     this.getListView();  
     }

Listatab.xml

    android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
    android:id="@android:id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

和 entradalista.xml

android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
<TextView
    android:id="@+id/nombre"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dip"
    android:textColor="#000000"/>

【问题讨论】:

  • 你试过我的答案了吗?只需将其替换为您的代码并尝试一下..
  • 看看我新编辑的答案,删除你的代码并把我的代码放到试试看。让我知道发生了什么。。
  • 相同,所有结果均为空,但结果数正确

标签: android sqlite listview simplecursoradapter


【解决方案1】:

改变这个:

int[] layoutDondeLoPongo = new int[] { R.layout.entrada_lista};
String[] datosRecibidos = new String[] {"nombre"};

到:

int[] layoutDondeLoPongo = new int[] { R.id.nombre };
String[] datosRecibidos = new String[] {"son"};

【讨论】:

  • 如果我更改 int[] layoutDondeLoPongo = new int[] { R.id.nombre };
  • 使用nombresC.getCount()(应该返回5)和nombresC.getColumnNames()(应该返回String[] {"nombre"})进行调试。如果不是这种情况,则意味着您的 SQL 查询错误,或者您的结果集没有名为 nombrecolumn
  • Log.e("debugeando", "son "+nombresC.getCount()); Log.e("debugeando", "son "+nombreC.getColumNames() 它返回 6 和儿子 [Ljava.lang.String;@4052ceb0
  • 崩溃,子列不存在
【解决方案2】:

试试这个,

编辑::

super.onCreate(savedInstanceState);   
setContentView(R.layout.listatab);

 //Creamos la instancia de DataBaseHelper, un cursor y aplicamos el metodo getNombres al cursor.
  ayudabbdd = new DataBaseHelper(this);
  Cursor nombresC;     
  nombresC = (Cursor) ayudabbdd.getNombres();  
  nombresC.moveToFirst();
  startManagingCursor(nombresC);
  getListView(); 
 ListAdapter adapter = new SimpleCursorAdapter(this,R.layout.listatab,nombresC,        
  new String[] {"nombre"},   
  new int[] { R.id.nombre}); 
  setListAdapter(adapter);

【讨论】:

  • 相同的结果..我可以调试看看光标是否正确填充?我不知道看到游标结果的值
  • 只是 nomresC.getCount(); Log.e("光标大小:",nombresC.getCount());
  • 有什么新想法吗?我会为这个疯狂的!
【解决方案3】:

我找到了解决办法:

listtab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
<ListView  
     android:id="@android:id/list"
     android:cacheColorHint="#666666"
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     />
</LinearLayout>

entrada_lista.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:paddingTop="4dip"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:paddingBottom="6dip"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical">

 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

 <TextView android:id="@+id/nombreLugar"
    android:textColor="#4B89E0" 
     android:layout_width="130dip"
     android:layout_height="40dip"
     android:textSize = "15dip"
     />


</LinearLayout>          

</LinearLayout>

和活动

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);   
    setContentView(R.layout.listatab);

   //Creamos la instancia de DataBaseHelper, un cursor y aplicamos el metodo getNombres al cursor.
    ayudabbdd = new DataBaseHelper(this);
    Cursor nombresC;     
    nombresC = (Cursor) ayudabbdd.getNombres();  
    nombresC.moveToFirst();
    startManagingCursor(nombresC);
    String[] from = new String[]{DataBaseHelper.CNOMBRE};
    int[] to = new int[]{R.id.nombreLugar};
    SimpleCursorAdapter lugares = 
        new SimpleCursorAdapter(this, R.layout.entrada_lista, nombresC, from, to);
    setListAdapter(lugares);

  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多