如何在android中扩展BaseAdapter的适配器中使用onActivityResult

时间:2015-10-08 13:07:43

标签: android baseadapter

我遇到了一个问题,涉及从移动设备中挑选文本文件并在TextView中显示其位置。我想从我的手机中选择一个文本文件。选择文件后我想在适配器中显示其位置和名称。但是我无法在适配器内执行此任务。

这是适配器的getView方法

public View getView(int position, View convertView, final ViewGroup parent) {
    if (inflater == null)
        inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.feed_item_professional_tab_timeline, parent, false);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();

    //Getting the views
    TextView companyName = (TextView) convertView.findViewById(R.id.txtCompanyName);
    TextView jobCategory = (TextView) convertView.findViewById(R.id.txtJobCategory);
    TextView timeStamp = (TextView) convertView.findViewById(R.id.timeStampPost);
    TextView postName = (TextView) convertView.findViewById(R.id.postName);
    TextView jobLocation = (TextView) convertView.findViewById(R.id.jobLocation);
    TextView noOfPositions = (TextView) convertView.findViewById(R.id.noOfPositions);
    TextView jobDescriptions = (TextView) convertView.findViewById(R.id.jobDescription);
    Button apply = (Button) convertView.findViewById(R.id.btnApply);
    Button share = (Button) convertView.findViewById(R.id.btnShare);
    // On clicking apply button
    apply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // A dialog will be opened that will cllow the user to apply for the job
            AlertDialog.Builder alertDialogBuilder;
            AlertDialog alertDialog;
            Toast.makeText(v.getContext(), "clicked", Toast.LENGTH_SHORT).show();
           // alertDialogBuilder = new AlertDialog.Builder(v.getContext()); // we are using v.getContext here because View v is a part of Fragment that is shown through BaseAdapter inside Fragment
            alertDialogBuilder = new AlertDialog.Builder(context);
            alertDialogBuilder.setTitle("Apply for JOB");
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // LayoutInflater inflater = (LayoutInflater) v.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.custom_dialog_apply_job, parent, false);
            alertDialogBuilder.setView(view);
            alertDialog = alertDialogBuilder.create();
            alertDialog.show();
            // Getting Resume Button
            Button resumeBtn = (Button) view.findViewById(R.id.btnResume);
            resumeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("file/*");
                    ((Activity) context).startActivityForResult(intent, PICKFILE_RESULT_CODE);

                }
            });
        }
    });


    FeedItemProfessionalTimeline item = listProfessionalTimeline.get(position);
    //Getting values from List
    String companyNam = item.getCompanyName();
    String jobCat = item.getJobCategory();
    Log.e("job category", jobCat);
    String date_time = item.getDate_time();
    String jobTitle = item.getJobTitle();
    //To make job title underline
    String htmlJobTitle = "<u>" + jobTitle + "</u>";
    String jobLocatn = item.getJobLocation();
    String positions = item.getPositions();
    String positionsOpen = "Positions open : " + positions;
    String jobInfo = item.getJobInfo();

    //Setting values to the views
    String postedJob = companyName + " posted a job under " + jobCategory + " category ";
    Log.e("posted job", postedJob);
    companyName.setText(companyNam); // Setting Company name
    jobCategory.setText(jobCat); // Setting job category
    timeStamp.setText(date_time);//Setting timestamp
    postName.setText(Html.fromHtml(htmlJobTitle)); //Setting post name
    jobLocation.setText(jobLocatn); // Setting job location
    noOfPositions.setText(positionsOpen);//Setting number of positions
    jobDescriptions.setText(jobInfo);
    return convertView;
}

片段中的onActivityResult方法:

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        String filepath = data.getDataString();
        Toast.makeText(getActivity(), "Hello onActivityResult", Toast.LENGTH_SHORT).show();
    }
}

所以我在自定义适配器和onActivityResult方法insode片段中使用了startActivityForResult,因为适配器正在填充片段内的列表视图。但Toast在这里不起作用。请帮助。

1 个答案:

答案 0 :(得分:1)

将在调用者活动上调用OnActivityResult,看看this

此外,您可以从here

了解更多信息

希望这有帮助。