2017-03-17 123 views
0

我想添加一些项目ListView - 他们没有显示。ListView不显示任何添加项目

我意识到这个问题已被多次询问,但我试着按照我在那里找到的任何建议 - 没有为我工作。

这里是公共类MyListActivity我的onCreate方法的内容延伸AppCompatActivity - >

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_list); 
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
if (getSupportActionBar() != null) { 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
} 
((EditText) findViewById(R.id.editText2)).setInputType(InputType.TYPE_NULL); 

ListView view = (ListView) findViewById(R.id.myList); 
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1); 
view.setAdapter(adapter); 
adapter.add("aaaaa"); 
adapter.add("bbbbb"); 
adapter.notifyDataSetChanged(); 

这是我的相关布局XML - >

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.kuba.myapplication3.MyListActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay" 
    > 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/background_dark" 
    android:orientation="vertical" 
    > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/editText2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="textPersonName" 
      android:text="@string/txt_searching" 
      android:textColor="@android:color/white" /> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="ButtonTxt" /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/myList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:dividerHeight="1dp" /> 

</LinearLayout> 

好吧,就像我说的,“aaaa”和“bbbb”不要在任何地方展示。

在此先感谢您的任何建议!

+0

你能显示你的适配器声明吗? – tahsinRupam

+0

@tahsinRupam它在MyListActivity类中作为私有ArrayAdapter 适配器; – awa993

+0

请检查我的回答请 – tahsinRupam

回答

0

您的活动代码非常完美。 问题是你的布局文件和非常愚蠢的一个。

您设定主要的LinearLayout背景background_dark。而你的ListView文字颜色也是黑暗的。所以它不可见。

尝试背景颜色为background_light并且您的ListView将可见。

,或者,如果你想改变的ListView文本颜色然后执行如下:

1)创建自定义布局如下:(custom_list_layout.xml)

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tvlist" 
    android:textColor="@color/white" 
    android:layout_width="fill_parent" 
    android:gravity="center" 
    android:layout_height="fill_parent"/> 

2)设置此您数组适配器

adapter = new ArrayAdapter<>(this, android.R.layout.custom_list_layout); 

然后用白色的文字你的ListView将b e在黑暗的背景中可见。希望这可以帮助。

+0

我喜欢愚蠢的错误,因为这意味着没有一些严重的问题。我现在不能尝试,但我会尽快完成,然后回到你身边。感谢您的建议。 – awa993

+1

它没有帮助。我尝试了浅色背景,但它在视觉上完全没有改变。看起来像整个ListView没有被添加到活动,而不仅仅是它的项目。 – awa993

+0

我实际上运行了您在设备上提供的所有代码。首先,我复制了你的活动代码,它工作正常。然后我复制了xml代码,这是一个黑暗的背景,这就是为什么listview不可见。改变它,它再次运作。可能b你正在使用重复的xml文件或其他东西。您可以提供完整的活动代码。 – tahsinRupam

0

我看到你正在初始化适配器而没有参考它的一组项目,这可能是问题所在。请改为试试这个代码:

类字段:

private List<String> items; 

onCreate方法:

... 

items = new ArrayList<>(); 

adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items); 
view.setAdapter(adapter); 
items.add("aaaaa"); 
items.add("bbbbb"); 
adapter.notifyDataSetChanged(); 

... 

或者你可以简单地初始化与两个元素的项目列表,而不是增加他们的后来。

+0

感谢您的快速反应,但唉,它并没有帮助。项目仍然不显示。 – awa993

0

我不确定,但我认为有时UI元素修改可能会在另一个线程上执行。因此,也许当你添加元素,然后notifyDataSetChanged(),视图既不显示也没有显示,因此,什么都不做。然后适配器尝试显示某些内容并且没有元素。

我建议两件事: - 首先,尝试向适配器列表添加元素(如Juan Martinez的答案),然后是适配器声明和setAdapter(adapter);。然后创建你的适配器,设置适配器。 - 第二,尝试在“onStart”而不是“onCreate()”上添加元素和notifyDataSetChanged。

第一个测试将允许您查看问题是否存在于构造函数,线程等中。第二个测试将向您显示您是否可以稍后添加元素,以便在第一个测试显示不能添加元素立即在“setAdapter”之后。

看起来你的代码是用于例子或小测试的,所以,在不知道你真正需要添加对象的地方以及显示问题的情况下,可能很难给出精确的代码。

在onCreate方法:

items = new ArrayList<>(); 

    items.add("aaaaa"); 
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items); 
    view.setAdapter(adapter); 
    items.add("bbbbb"); 
    adapter.notifyDataSetChanged(); 

覆盖在onStart太

@Override 
    public void onStart() { 
     super.onStart(); 
     adapter.add("cccc"); 
     adapter.notifyDataSetChanged(); 
    } 

,如果你的视图只AAAA和中交股份,那么你就不能只是setAdapter后添加项目。如果什么都没有显示,那么问题就在别的地方。

+0

我尝试了所有这些,没有出现。正如你所说,问题很可能在其他地方。 – awa993

+0

而不是直接添加字符串“aaaa”等,如果你尝试String aString =“aaaa”;然后添加aString到arrayList /适配器,这是否工作?也许android试图向字符串数组中添加char [],并且存在一个抛出问题而不会引发异常。 – Feuby

+0

它不会抛出任何类型的错误..没有什么简单的事情发生。当我有机会的时候,我会试试这个。 – awa993