Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ArrayList适配器(用于ListView)仅显示第一个添加的项_Java_Android_Listview_Arraylist - Fatal编程技术网

Java ArrayList适配器(用于ListView)仅显示第一个添加的项

Java ArrayList适配器(用于ListView)仅显示第一个添加的项,java,android,listview,arraylist,Java,Android,Listview,Arraylist,Edit3:我自己的项目列表布局: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_c

Edit3:我自己的项目列表布局:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/text1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceListItemSmall"
      android:gravity="center_vertical"
/>
仅显示templateFiles[1]。同样,如果我这样做:

    templateList.add(templateFiles[0].getName());
    templateList.add(templateFiles[1].getName());
仅显示templateFiles[0]。有什么问题吗

以下是我的XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.template_housing.MyTemplatesFrag"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    >

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_templates"
        android:textSize="34sp"
        android:textColor="@color/black"
        android:background="@color/Gold1"
        android:gravity="center_horizontal"
        android:padding="5dp"
        android:layout_marginBottom="10dp"
/>

<ListView
        android:id="@+id/templateFilesList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
>

</ListView>


如果要更新适配器的内容,还应调用
notifyDataSetChanged()
,以确保适配器知道您的内容已更改尝试将其转换为字符串数组:

String [] templateListArray = templateList.toArray(new String[templateList.size()]);
然后按照以下步骤完成作业:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,
            templateListArray);
ArrayAdapter ArrayAdapter=new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,
圣殿骑士团);

希望这能解决问题。

您也可以发布简单的列表项1 XML文件吗。这个问题与Java代码无关,它与XML和元素的大小有关。尝试删除列表视图顶部的文本视图,简单列表项视图的布局高度应为
wrap content


如果您仍然想使用textview,可以使用“权重”属性。将文本视图的权重设置为0,列表视图的权重设置为1。

可能是阵列适配器设置不正确,因此无法正常工作,因此请尝试使用以下代码替换阵列适配器,并检查其是否正常工作

   ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, templateList);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,android.R.id.text1,templateList);

我复制了你的代码并在Android Studio中运行。看来没问题

这是我的活动代码(相同的xml代码),唯一的区别是我使用一个活动,你可以使用一个片段

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView = (ListView) findViewById(R.id.templateFilesList);
    ArrayList<String> templateList = new ArrayList<>();

    File myDir = getFilesDir();
    File[] templateFiles = myDir.listFiles();
    int length = templateFiles.length;

    //case 1: in my project, length is 1, show only one element, see case2.
//        for (int i = 0; i < length; i++) {
//            templateList.add(templateFiles[i].getName());
//        }

    //case 2: try to add simple strings, 
    //because you said put simple strings in the list could cause the problem, too.
    templateList.add("a1");
    templateList.add("a2");
    templateList.add("a3");

    ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
            templateList);
    listView.setAdapter(arrayAdapter);
}
}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView ListView=(ListView)findViewById(R.id.templateFilesList);
ArrayList templateList=新的ArrayList();
File myDir=getFilesDir();
File[]templateFiles=myDir.listFiles();
int length=templateFiles.length;
//案例1:在我的项目中,长度为1,只显示一个元素,请参见案例2。
//for(int i=0;i


我在上共享了我的代码。我强烈建议您将完整的代码发布到Github上,让我们检查一下您的代码。

您也可以发布您的xml代码吗?尝试在列表中添加一些简单的字符串(“a”、“b”、“c”…),而不是templateFiles[I],然后看看问题是否仍然存在。您的代码似乎很好。我假设您的
ListView
已正确填充,但不知何故,只有第一项可见。在调用
setAdapter()
之后,请在
ListView
实例上调用
getCount()
进行确认。如果是这种情况,请发布布局的XML代码。好的,XML发布@Leo.Han-将简单字符串放入仍然存在只显示第一个字符串(在本例中为“a”)的问题。getCount结果为8。这是正确的,因为我在内部存储器中有8个文件。如果在设置适配器后更新数据集,则应调用
notifyDataSetChanged()
。这里完全没有必要。请发布您的xmlfile@JorgeMendezXML已发布。同一问题,它仅显示第一个项目。请尝试更新的项目。并检查阵列适配器包含的值,同样的问题。字符串数组在传递到数组适配器时包含正确的项,这不是问题,因为如果单个项匹配[parent然后其他人仍然会通过向下滚动来。哦,我明白了:)但我很确定这与XML有关。只是看不出让列表视图关闭标记和自动关闭标记有什么区别?你的意思是这样吗?两者都可以:)删除文本视图后出现同样的问题。你可以使用此或getApplicationContext()任何一个“this”和“getApplicationContext”将整个语句加上红色下划线。改为使用getContext()不会出错,但相同的原始问题仍然存在。是的,将我的确切代码复制并粘贴到新的测试应用程序中效果非常好。我现在对这一点太迷茫了……我现在将考虑将其发布到GitHub
   ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, templateList);
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView = (ListView) findViewById(R.id.templateFilesList);
    ArrayList<String> templateList = new ArrayList<>();

    File myDir = getFilesDir();
    File[] templateFiles = myDir.listFiles();
    int length = templateFiles.length;

    //case 1: in my project, length is 1, show only one element, see case2.
//        for (int i = 0; i < length; i++) {
//            templateList.add(templateFiles[i].getName());
//        }

    //case 2: try to add simple strings, 
    //because you said put simple strings in the list could cause the problem, too.
    templateList.add("a1");
    templateList.add("a2");
    templateList.add("a3");

    ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
            templateList);
    listView.setAdapter(arrayAdapter);
}
}