2015-10-08 57 views
1

我被困在一个问题中,涉及到从手机中选取文本文件并在TextView中显示其位置。我想从我的手机中选择一个文本文件。选择文件我想要在Adapter中显示它的位置和名称。但我无法在适配器内执行此任务。如何在一个适配器中使用onActivityResult扩展BaseAdapter android

这里是适配器的在片段

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方法getView方法:

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

所以我已经使用startActivityForResult定制适配器和onActivityResult方法insode片段内作为适配器填充列表视图里面的片段。但吐司不在这里工作,请帮助。

+1

在这里发布一些代码..! –

+0

您可能在适配器右侧的使用中有活动的上下文来调用startActivityResult并处理该意图结果以获取文件。 – Raghavendra

+0

@Raghvendra,我在适配器中使用startActivityResult,但我无法处理适配器内部的结果,因为我不知道在哪里使用onActivityResult.Here adpater正在填充片段中的列表视图。 –

回答

1

OnActivityResult将呼叫者活动被调用,看看this

此外,您还可以了解更多从here

希望这有助于。

+0

感谢Nanoc,但我仍然有问题。其实我的问题是,我正在使用startActivityForResult自定义适配器扩展基本适配器。此自定义适配器正在填充列表视图里面的一个片段,这意味着自定义适配器正在创建一个片段的视图。在这个片段中,我使用onActivityResult方法。我只能在这里处理结果。请帮助。 –

+0

我的第一个链接显示如何接收片段内的OnActivityResult你试过吗? – Nanoc

+0

Nanoc,在你的代码中,你在同一个片段中使用startActivityForResult和onActivityResult,但是我的问题是不同的。我从我的android手机中使用startActivityForResult方法在适配器中选择一个文件。我必须使用onActivityResult中选定文件的路径fragment.I认为onActivityResult不适合我。简而言之,startActivityForResult位于适配器内,onActivityResult位于片段内。 –

相关问题