2015-04-29 43 views
0
import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ListView; 
import java.util.ArrayList; 

public class AttachFiles extends Activity { 

    Button btnAddFile; 
    ListView listViewFiles; 
    String[] str; 
    ArrayList<String> arrayList = new ArrayList<String>(); 

    Adapter myFileListAdapter; 
    final int RQS_LOADIMAGE = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btnAddFile = (Button) findViewById(R.id.addphoto); 
     btnAddFile.setOnClickListener(btnAddFileOnClickListener); 

    } 

    View.OnClickListener btnAddFileOnClickListener 
      = new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.setType("file/*"); 
      startActivityForResult(intent, RQS_LOADIMAGE); 

     } 
    }; 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 


     if (resultCode == RESULT_OK) { 
      switch (requestCode) { 
       case RQS_LOADIMAGE: 
        Uri imageUri = data.getData(); 
        String string = imageUri.toString(); 
        Log.e("STRING", " "+string); 

        arrayList.add(string); 

        Log.e("Get arrayList :" , " "+arrayList); 

        myFileListAdapter = new Adapter(AttachFiles.this, arrayList); 
        listViewFiles = (ListView) findViewById(R.id.filelist); 
        listViewFiles.setAdapter(myFileListAdapter); 
        myFileListAdapter.notifyDataSetChanged(); 
        break; 
      } 
     } 
    } 
} 

在上面的代碼中,我可以看到這些文件,但無法將其加載到ListView中。雖然在使用ACTION_GET_CONTENT導航到文件後無法附加文件

回答

0

如果你在onactivityresult()中得到結果,我沒有看到任何錯誤發生的機會。 更改適配器到ArrayAdapter

ArrayAdapter<String> myFileListAdapter = new 
ArrayAdapter<String>(AttachFiles.this, 
android.R.layout.simple_list_item_1, arrayList); 

,並宣佈裏面的OnCreate列表視圖是更好的。

並試一試:)