如何在android中使用带有ArrayAdapter的文件选择器
在我的android应用程序中,我需要在listview行中添加一个文件选择器。listview包含一个用于打开文件选择器的imagebutton。listview使用ArrayAdapter填充。如果我选择一个文件,我需要在listview中显示文件名,因此,我需要从arrayadapter将所选的listitem和活动结果传递给父活动。但是,当我使用filechooser单击gallery时,它会在设备中显示一个错误,如下所示如何在android中使用带有ArrayAdapter的文件选择器,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,在我的android应用程序中,我需要在listview行中添加一个文件选择器。listview包含一个用于打开文件选择器的imagebutton。listview使用ArrayAdapter填充。如果我选择一个文件,我需要在listview中显示文件名,因此,我需要从arrayadapter将所选的listitem和活动结果传递给父活动。但是,当我使用filechooser单击gallery时,它会在设备中显示一个错误,如下所示 The application Camera(process c
The application Camera(process com.android.gallery) has stoped unexpectedly.
这是因为排队
intent.putExtra("browseCoa", itemToBrowse);
在AdvAttachmentAdapter类中。我需要将itemToBrowse与活动结果一起传递给父活动(即AddAdvance)
如何解决这个问题?下面是我的代码
AddAdvance.java:
public class AddAdvance extends Activity{
private static final int FILE_SELECT_CODE = 2;
AdvAttachmentAdapter atcmtAdapter;
RestTemplate restTemplate=new RestTemplate();
String constr;
int advPar;
DecimalFormat df=new DecimalFormat("#.00");
ListView lstAttachment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_advance);
constr=getResources().getString(R.string.base_url);
curAcct=(COAAccount) getIntent().getSerializableExtra("coaAcct");
advPar=getIntent().getExtras().getInt("advpar");
lstAttachment=(ListView) findViewById(R.id.lstAttachment);
addAttachment();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
COAAccount advAttmtCoa=(COAAccount) data.getSerializableExtra("browseCoa");
String path = getPath(this, uri);
File file = new File(path);
int repPos=atcmtAdapter.getPosition(advAttmtCoa);
atcmtAdapter.remove(advAttmtCoa);
advAttmtCoa.setStrName(file.getName());
advAttmtCoa.setAltName(path);
atcmtAdapter.insert(advAttmtCoa, repPos);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
public static String getPath(Context context, Uri uri) {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
private void addAttachment(){
if(advPar==-26){
atcmtAdapter=new AdvAttachmentAdapter(AddAdvance.this, R.layout.attachment_list, new ArrayList<COAAccount>());
lstAttachment.setAdapter(atcmtAdapter);
COAAccount newAttmt=new COAAccount();
newAttmt.setTransDate(curAcct.getGlobalDate());
newAttmt.setStrName("");
newAttmt.setAltName("");
atcmtAdapter.add(newAttmt);
}
}
}
要传递可序列化的
对象,可以将其放入捆绑包中,然后将该捆绑包放入意图中
Bundle bundle = new Bundle();
bundle.putSerializable("browseCoa", itemToBrowse);
intent.putExtras(bundle);
或者,您可以使用Parcelable
而不是Serializable
来获得优势。要传递Serializable
对象,您可以将其放入捆绑包
并将该捆绑包
放入意图
Bundle bundle = new Bundle();
bundle.putSerializable("browseCoa", itemToBrowse);
intent.putExtras(bundle);
或者您可以使用Parcelable
而不是Serializable
来获得优势
Bundle bundle = new Bundle();
bundle.putSerializable("browseCoa", itemToBrowse);
intent.putExtras(bundle);