【问题标题】:How combine android SimpleCursorAdapter result?如何结合 android SimpleCursorAdapter 结果?
【发布时间】:2017-01-30 13:36:36
【问题描述】:

我正在尝试将 sqlite 记录填充到 listview 中。 在我的表格中,我将地址拆分为列,例如 街道、邮政编码、城市和州。 我正在使用 SimpleCursorAdapter 将结果加载到如下代码所示的视图中: 这是我的 ListActivity.java

private DBManager dbManager;
private ListView listView;
private SimpleCursorAdapter adapter;
DbHelper myDb;

final String[] from = new String[] {
        DbHelper.COL_NAME,
        DbHelper.COL_STREET,
        DbHelper.COL_POSTCODE,
        DbHelper.COL_CITY,
        DbHelper.COL_STATE };


final int[] to = new int[] { R.id.name, R.id.address };


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



    setContentView(R.layout.fragment_emp_list);

    dbManager = new DBManager(this);
    myDb = new DbHelper(this);
    dbManager.open();
    Cursor cursor = dbManager.fetch();

    listView = (ListView) findViewById(R.id.list_view);
    listView.setEmptyView(findViewById(R.id.empty));
    adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0);
    adapter.notifyDataSetChanged();

    listView.setAdapter(adapter);

这是我的看法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


<TextView
    android:id="@+id/name"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@id/id"
    android:maxLines="1"
    android:padding="3dp"
    android:textSize="17sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/address"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/nama"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@id/id"
    android:ellipsize="end"
    android:maxLines="2"
    android:padding="3dp"
    android:visibility="visible" />



</RelativeLayout>

我的问题是,如何将STREET、POSTCODE、CITY、STATE组合起来放入R.id.address? 使用上述方法,我必须为每一列创建一个textview,否则会出错。

【问题讨论】:

  • 只想添加文字?
  • 是的。所有 4 列到单个文本视图中

标签: java android sqlite simplecursoradapter


【解决方案1】:

进行以下更改:

  1. 在您的布局 xml 更改中

    <TextView
        android:id="@+id/address"
        android:text="@string/format_address"
        ......
        />
    
  2. 在您的 strings.xm 中添加以下内容:

    <string name="format_address">%1$s, %2$s, %3$s, %4$s</string>
    
  3. 在您的源代码中添加以下内容:

    Resources res = getResources();
    String text = String.format(res.getString(R.string.format_address), STREET_VALUE, POSTCODE_VALUE, CITY_VALUE, STATE_VALUE);
    

注意 STREET_VALUE、POSTCODE_VALUE、CITY_VALUE、STATE_VALUE 替换为实际值或字符串参考。

【讨论】:

  • 不。我想我必须编辑问题以获取更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多