如何在android中使用带ArrayAdapter的FileChooser

时间:2015-06-03 08:31:50

标签: android listview android-arrayadapter

在我的android应用程序中,我需要在listview行中添加一个filechooser.listview包含一个用于打开filechooser的imagebutton。使用ArrayAdapter填充listview。如果我选​​择了一个文件,我需要在文件中显示文件名listview,所以我需要将带有活动结果的选定listitem从arrayadapter.But传递给父活动。当我使用filechooser点击图库时,它会在设备中显示错误,如下所示

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);
    }
}
 }

AdvAttachmentAdapter.java:

public class AdvAttachmentAdapter extends ArrayAdapter<COAAccount>{
private Context context;
private AddAdvance advContext;
private final int resourceID;
private List<COAAccount> items;
DecimalFormat df=new DecimalFormat("#.00");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
private static final int FILE_SELECT_CODE = 2;

public AdvAttachmentAdapter(AddAdvance advContext, int resource, List<COAAccount> items) {
    super(advContext, resource,items);
    this.advContext = advContext;
    this.resourceID = resource;
    this.items=items;
}

public static class AdvanceAtmtHolder{
    COAAccount advAttCoa;
    TextView txtAttachment;
    ImageButton imgBrowse;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

@Override
public COAAccount getItem(int position) {
    // TODO Auto-generated method stub
    return items.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    df.setMinimumIntegerDigits(1);
    AdvanceAtmtHolder advAttHolder=new AdvanceAtmtHolder();
    LayoutInflater inflater=advContext.getLayoutInflater();
    View rowView=inflater.inflate(resourceID, parent, false);
    advAttHolder.advAttCoa=items.get(position);
    advAttHolder.txtAttachment=(TextView) rowView.findViewById(R.id.txtAttachment);
    advAttHolder.imgBrowse=(ImageButton) rowView.findViewById(R.id.imgBrowse);
    advAttHolder.imgBrowse.setTag(advAttHolder.advAttCoa);
    browseRow(advAttHolder);
    setupItem(advAttHolder);
    return rowView;
}

private void browseRow(AdvanceAtmtHolder advAttHolder) {
    advAttHolder.imgBrowse.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            COAAccount itemToBrowse = (COAAccount)v.getTag();
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
            intent.setType("*/*"); 
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.putExtra("browseCoa", itemToBrowse);
            try {
                advContext.startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"),FILE_SELECT_CODE);
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
    });     
}



private void setupItem(AdvanceAtmtHolder advAttHolder) {

    advAttHolder.txtAttachment.setText(advAttHolder.advAttCoa.getAltName());

}

}

COAAccount.java:

public class COAAccount implements Serializable {
private int iD;
private String strName;
private String altName;
private float budAmount;

public COAAccount() {
    super();
    // TODO Auto-generated constructor stub
}

public int getiD() {
    return iD;
}

public void setiD(int iD) {
    this.iD = iD;
}

public String getStrName() {
    return strName;
}

public void setStrName(String strName) {
    this.strName = strName;
}

public String getAltName() {
    return altName;
}

public void setAltName(String altName) {
    this.altName = altName;
}

@Override
public String toString() {
    return strName;
}
}

1 个答案:

答案 0 :(得分:0)

要传递Serializable个对象,您可以将其放到Bundle并将Bundle放到Intent

Bundle bundle = new Bundle();
bundle.putSerializable("browseCoa", itemToBrowse);
intent.putExtras(bundle);

或者您可以使用Parcelable代替Serializable来获取优势。