【问题标题】:TextView is ListView is not shown correctlyTextView 是 ListView 未正确显示
【发布时间】:2013-05-08 06:08:10
【问题描述】:

我有一个从数据库中获取字符串并将其放入 ListView 的应用程序。 这是从数据库中获取我的字符串的代码:

    public void setLogView(String date){


    ArrayAdapter<TextView> adapter = new ArrayAdapter<TextView>(this, android.R.layout.simple_list_item_1);
    listView.setAdapter(adapter);

    DataBaseMain dataBase = new DataBaseMain(this);
    dataBase.open();
    String[][] all = dataBase.dayLog(date);
    dataBase.close();

    if(all == null)
        return;

String temporay = "";

    for(int j = 0; j < all[0].length; j++){
            temporay = "";  
        for (int i = 0; i < all.length; i++){
            TextView text = new TextView(this);
            temporay = temporay + " " + all[i][j];
            text.setText(temporay);
            adapter.add((TextView)text);
            }
        }   
}

似乎我在我的 ListView 中获得了新的 TextView,但文本被弄乱了。 我检查了我的temporay 字符串,很好。 在某个地方把他放在 ListView 中。 logcat 中没有错误或异常。 这是我在我的应用程序 ListView 中得到的我想要的文本。(我想放 picutrue 但我没有足够的重复:

【问题讨论】:

  • 你能分享一下你所期待的和你观察到的截图吗
  • 我加了你的声望,你可以加个图
  • 请添加截图。
  • @CRUSADER。添加了屏幕截图

标签: android android-listview textview


【解决方案1】:

在那里,从你提供的图像中可以清楚地看到

试试,这个..

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    listView.setAdapter(adapter);

您的适配器附加 textView 对象而不是您提供的字符串。

然后在循环中添加 temporay 而不是 textView..like

 adapter.add(temporay);

这个,一定能解决你的问题。

【讨论】:

    【解决方案2】:

    将您的适配器更改为ArrayAdapter&lt;String&gt;,并添加temporay 而不是整个列表视图。

    否则,您可以扩展 ArrayAdapter 并覆盖 getView()

    【讨论】:

      【解决方案3】:

      假设您正在尝试使用单独的 layout.xml 在自定义列表视图中显示文本,其中仅包含 textview。

      查看下面给出的示例,我就是这样做的:

      首先获取要显示的数据并将其存储在 ArrayList 中。这里,al_rec_idal_rec_name 分别是 IntegerString 类型的数组列表。

      cursor = database.query("records_data", new String[]{"rec_id", "rec_name"}, "cat_id=?", new String[]{cat_id+""}, null, null, null);
                  if(cursor.getCount() == 0)
                      Toast.makeText(getBaseContext(), "No records found.", Toast.LENGTH_SHORT).show();
                  else
                  {
                      while(cursor.moveToNext())
                      {
                          al_rec_id.add(cursor.getInt(0));
                          al_rec_name.add(cursor.getString(1));
                      }
                      cursor.close();
                  }
      

      之后,将此 arraylist 与 ArrayAdapter 绑定,然后将此适配器设置为 listview,如下所示。这里,array_adapter_all_records 是一个 String 类型的 ArrayAdapter

      array_adapter_all_records = new ArrayAdapter<String>(this, R.layout.single_row_home, R.id.textViewSingleRowHome, al_rec_name);
              listview_all_records.setAdapter(array_adapter_all_records);
      

      这是我的 single_row_home.xml 代码:

      <?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:padding="15dp"
          android:background="#ffffff" >
      
          <TextView
              android:id="@+id/textViewSingleRowHome"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              style="@style/listview_only_textview"
              android:text="TextView" />
      
      </RelativeLayout>
      

      就是这样。然后你就完成了……!!!

      【讨论】:

        【解决方案4】:

        创建一个类自定义适配器,它将扩展您的 ArrayList 适配器,您可以扩展包含您的 textview 的不同 xml,或者您可以像现在在自定义适配器的 getView 方法中那样创建一个动态 textview。如果您需要示例,请告诉我。

        【讨论】:

          猜你喜欢
          • 2014-07-18
          • 1970-01-01
          • 2021-12-08
          • 1970-01-01
          • 1970-01-01
          • 2021-11-23
          • 2020-06-14
          • 2013-08-25
          • 1970-01-01
          相关资源
          最近更新 更多