【问题标题】:array adapter is showing field value, not data values from database in android数组适配器显示字段值,而不是来自 android 数据库的数据值
【发布时间】:2015-01-18 05:27:58
【问题描述】:

我试图在fragments 中显示来自数据库的listview 数据。出于这个原因,我在数据库中添加了一个表。现在我正在尝试创建数据适配器。这是我的代码

list=(ListView)rootView.findViewById(R.id.listView1);
try{
DatabaseHandler db = new DatabaseHandler(getActivity());
List<Label> allLabel = db.getAllLabels();
for (Label label : allLabel) {   
ArrayAdapter<Label> dataadapter= new ArrayAdapter<Label>(getActivity(),android.R.layout.simple_list_item_1,allLabel);   
list.setAdapter(dataadapter);

list.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent,View v, int position,long id)
    {
        Toast.makeText(getActivity(), ((TextView)v).getText(), Toast.LENGTH_LONG).show();

    }
});
}
}
catch (Exception e)
{
    e.printStackTrace();
}

这是我的输出包含字段值而不是数据值:

   com.qcash.atmlocator.Label@52852d88
   com.qcash.atmlocator.Label@52852dd8
   com.qcash.atmlocator.Label@52852e08
   com.qcash.atmlocator.Label@52852e38

这是我的代码中使用的 getAllLabels() 函数:

public List<Label> getAllLabels(){

List<Label> labels = new ArrayList<Label>();

// Select All Query
String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
    do {
        Label label = new Label();
        label.setId(cursor.getInt(0));
        label.setName(cursor.getString(1));
        label.setLatitude(cursor.getDouble(2));
        label.setLongitude(cursor.getDouble(3));
        labels.add(label);
    } while (cursor.moveToNext());
}

// closing connection
cursor.close();
db.close();

// returning lables
return labels;
}

如何获取数据库值?请帮帮我。

【问题讨论】:

    标签: android sqlite android-fragments android-listview android-arrayadapter


    【解决方案1】:

    将光标数据添加到字符串并将该字符串添加到调用函数片段解决了这个问题。代码如下:

     String name=cursor.getString(0)+"\n"+cursor.getString(1)+"\n"+cursor.getString(2)+"\n"+cursor.getString(3);
                    FindPeopleFragment.ArrayofName.add(name);
    

    最后将数组显示给适配器完成:

    ArrayAdapter<String> dataadapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,ArrayofName);  
    

    【讨论】:

      【解决方案2】:

      重写 Label 类中的 toString() 方法。我假设您的班级有一个变量“名称”。

      class Label{
         // other variables. 
      
          @Override
          public String toString() {
              return name;
          }
      }
      

      更新:

      更改 onItemclick 监听器

      list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      
          public void onItemClick(AdapterView<?> parent,View v, int position,long id)
          {
              Label label = (Label)parent.getItemAtPosition(position);
              Toast.makeText(getActivity(), "" + label.toString(), Toast.LENGTH_LONG).show();
      
          }
      });
      

      【讨论】:

      • 我在 Label 类中所做的。
      猜你喜欢
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 2018-01-12
      相关资源
      最近更新 更多