2016-03-11 61 views
0

我需要使用目录中的文件填充listview。我可以将文件放入数组中,但似乎无法将它们放入listview。这是我的代码;我哪里错了?ListView未从数组中填充

private void getDir(final String dirPath) 
{ 
    myPath.setText("Location: " + dirPath); 
    item = new ArrayList<String>(); 
    path = new ArrayList<String>(); 
    File f = new File(String.valueOf(dirPath)); 
    final File[] files = f.listFiles(); 
    myList = (ListView)findViewById(android.R.id.list); 


    if(!dirPath.equals(root)) 
    { 
     item.add(root); 
     path.add(root); 
     item.add("../"); 
     path.add(f.getParent()); 
    } 

    if (files != null) { 

     for (int i = 0; i < files.length; i++) { 
      File file = files[i]; 

      // For testing only --- 
      System.out.println("Files -- "+files[i]); 
      System.out.println("Files -- "+file); 
      // End testing ^^^^^ 


      if (!file.isHidden() && file.canRead()) { 
       path.add(file.getPath()); 
       if (file.isDirectory()) { 
        item.add(file.getName() + "/"); 
       } 
       else { 
        item.add(file.getName()); 

       } 
      } 
     } 

     ArrayAdapter<String> fileList = 
       new ArrayAdapter<String>(this, R.layout.activity_sendsave, android.R.id.list); 
     setListAdapter(fileList); 


    } 
    else { 
     Toast.makeText(this,"No Files",Toast.LENGTH_LONG).show(); 
    } 

} 

这里是我的xml文件。

<ListView 
    android:id = "@android:id/list" 
    android:layout_width = "match_parent" 
    android:layout_height = "wrap_content" 
    android:layout_alignParentTop = "true" 
    android:layout_alignParentLeft = "true" 
    android:layout_alignParentStart = "true" 
    android:layout_above = "@+id/btnSendSave" 
    android:clickable = "true" 
    android:focusable = "true" 
    android:visibility = "visible" /> 

我需要让你知道,一切都似乎是在做什么应该,但listview不会填满。

在代码中,有system.out.println(files[i])显示文件正在放入数组中。

+0

对不起,setListAdapter什么都不做。这是从我尝试的另一块代码中遗留下来的。而类是公共类GetFile扩展ListActivity,但我相信这与它无关。 – vbneil54

回答

0

只是想我会表现出工作代码。

public class ListViewDemo extends ListActivity { 
private TextView selection; 
private String root; 
private List<String> item = null; 
private List<String> path = null; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    // This is the part I added. 
    File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); 
    File dir = new File(sdCard.getPath() + "/hvac/"); 

    if (dir.exists()) { 
     root = new String(String.valueOf(dir)); 
    } 
    else { 
     dir.mkdir(); 
     root = new String(String.valueOf(dir)); 
    } 

    item = new ArrayList<String>(); 
    path = new ArrayList<String>(); 
    File f = new File(String.valueOf(root)); 
    final File[] files = f.listFiles(); 

    if (files != null) { 

     for (int i = 0; i < files.length; i++) { 
      File file = files[i]; 

      if (!file.isHidden() && file.canRead()) { 
       path.add(file.getPath()); 
       if (file.isDirectory()) { 
        item.add(file.getName() + "/"); 

       } 
       else { 
        item.add(file.getName()); 
       } 
      } 

     } 

    } 
    else { 
     Toast.makeText(this, "No Files", Toast.LENGTH_LONG).show(); 
    } 

    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item)); 
    selection = (TextView) findViewById(R.id.selection); 

} 

@Override 
public void onListItemClick(ListView parent, View v, int position, 
          long id) { 
    selection.setText(item.get(position)); 
} 
} 

它工作得很好。感谢所有这些让我朝着正确的方向前进。

0

您遍历文件并为每一个有效的,你这样做:

item.add(file.getName()); 

然而,从未使用过item你这样做了。按照documentation你应该做这样的事情:

ArrayAdapter<String> itemsAdapter = 
new ArrayAdapter<String>(this, R.layout.activity_sendsave, item); 

这将利用items阵列,你已经建立了。

顺便说一句,而你的调试方法,以确保你得到的物品进入名单是好的,你真的应该利用它后的完整列表,已建成(您if条款,例如,可能有问题)。然后,如果您确定该列表是正确的,那么您知道无论使用该列表是否存在问题 - 这就是发生在这里的事情。

+0

嗨,当我这样做,我得到这个错误'android.widget.ListView不能转换为android.widget.TextView'。对于附注,谢谢。它在'if'后效果更好。 – vbneil54

+0

是的,我清理并重建了该项目。仍然得到错误。 – vbneil54

+0

这听起来像是一个单独的问题?你的布局是否期望一个'TextView'并且它获得一个'ListView'? –

1

ArrayAdapter构造有多个问题:

ArrayAdapter<String> fileList = 
      new ArrayAdapter<String>(this, R.layout.activity_sendsave, android.R.id.list); 

首先,你是不是传递item,所以适配器将是空的。

其次,android.R.id.list不太可能是ListView所使用的行布局中的TextView的ID。

第三,我怀疑R.layout.activity_sendsave实际上并不是你想要的每个ListView行的布局。

试试这个:

ArrayAdapter<String> fileList = 
      new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item); 

它使用内置的简单列布局的ListView,并提供你的词汇列表。

下面是一个使用这种方法,从this sample project一个例子活动:

/*** 
    Copyright (c) 2008-2012 CommonsWare, LLC 
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License. You may obtain a copy 
    of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. 

    From _The Busy Coder's Guide to Android Development_ 
    https://commonsware.com/Android 
*/ 

package com.commonsware.android.list; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class ListViewDemo extends ListActivity { 
    private TextView selection; 
    private static final String[] items={"lorem", "ipsum", "dolor", 
      "sit", "amet", 
      "consectetuer", "adipiscing", "elit", "morbi", "vel", 
      "ligula", "vitae", "arcu", "aliquet", "mollis", 
      "etiam", "vel", "erat", "placerat", "ante", 
      "porttitor", "sodales", "pellentesque", "augue", "purus"}; 

    @Override 
    public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    setListAdapter(new ArrayAdapter<String>(this, 
         android.R.layout.simple_list_item_1, 
         items)); 
    selection=(TextView)findViewById(R.id.selection); 
    } 

    @Override 
    public void onListItemClick(ListView parent, View v, int position, 
           long id) { 
    selection.setText(items[position]); 
    } 
} 
+0

谢谢。我可以在我的项目中工作。我只需要用我的文件名替换项目字符串值。再次谢谢你 – vbneil54