2012-06-05 48 views
0

我已经写了代码来下载SD卡上的内容,现在我试图列出已下载的内容,我已经写了代码,并且它的工作正常,但我想要列表中的每个项目的一个查看按钮我正在写在rowmydownload.xml.when我这样做我收到异常为java.lang.IllegalStateException:ArrayAdapter需要资源ID是一个TextView。Arraylist不工作​​

我的主类是

public class Downloadlist extends ListActivity { 
    private List<String> item = null; 
    private List<String> path = null; 
    private String root="/sdcard"; 
    private TextView myPath; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mydownload); 
     myPath = (TextView)findViewById(R.id.path); 
     getDir(root); 
    } 
    private void getDir(String dirPath) 
    { 
     myPath.setText("Location: " + dirPath); 
     item = new ArrayList<String>(); 
     path = new ArrayList<String>(); 
     File f = new File(dirPath); 
     File[] files = f.listFiles(); 
     if(!dirPath.equals(root)) 
     { 
      item.add(root); 
      path.add(root); 
      item.add("../"); 
      path.add(f.getParent()); 
     } 
     for(int i=0; i < files.length; i++) 
     { 
      File file = files[i]; 
      path.add(file.getPath()); 
      if(file.isDirectory()) 
       item.add(file.getName() + "/"); 
      else 
       item.add(file.getName()); 
     } 
     Log.d("itemssssssss", item.toString()); 
     ArrayAdapter<String> fileList = 
       new ArrayAdapter<String>(this, R.layout.rowmydownload, item); 
     setListAdapter(fileList); 
    } 
} 

我的XML是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFFFF" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:background="#A9D0F5" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/image1" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="8dp" 
      android:src="@drawable/untitled" /> 

     <TextView 
      android:id="@+id/txt1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="65dp" 
      android:layout_toRightOf="@+id/image1" 
      android:gravity="center" 
      android:text="MY Downloads" 
      android:textColor="#000000" 
      android:textSize="26px" 
      android:textStyle="bold" /> 
    </RelativeLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/path" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@android:id/empty" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="No Data" /> 
    </LinearLayout> 

</LinearLayout> 

和我row.xml是

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

    <TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/rowtext" 
     android:layout_width="fill_parent" 
     android:layout_height="25px" 
     android:textSize="23sp" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="50dp" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/course" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Sales Training" 
      android:textColor="#000000" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="20dp" 
     android:orientation="horizontal" > 

     <Button 
      android:layout_width="60dp" 
      android:layout_height="30dp" 
      android:layout_marginLeft="90dp" 
      android:background="#81BEF7" 
      android:text="View" /> 
    </LinearLayout> 

</LinearLayout> 
+0

我不觉得你可以使用自定义布局以这种方式你的数组适配器,也是问题不在于你的ArrayList –

+1

这里http://stackoverflow.com/questions/9280965找到答案/ arrayadapter-要求-的资源-ID将要-A-TextView的-XML的问题 –

回答

0

当使用ArrayAdapter,你行的第一要素。 xml应该是一个TextView,而不是一个LinearLayout。

如果您想要在行中使用多个元素,则不应使用ArrayAdapter,而应使用自定义适配器。例如见this android developer example

相关问题