1

I am stuck in a problem which involves picking a text file from mobile and display its location in a TextView .I want to select a text file from my mobile.After selecting the file i want to display its location and name in Adapter .But i am not able to perform this task inside adapter.

Here is the getView method of adapter

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 method in fragment:

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

So i have used startActivityForResult inside custom adapter and onActivityResult method insode fragment as adapter is populating the list view inside fragment.But Toast is not working here.Please help.

Deepak Rattan
  • 1,279
  • 7
  • 21
  • 47
  • 1
    post some code here..!! – Asif Sb Oct 08 '15 at 13:12
  • You might have context of activity in Adapter right use that to call startActivityResult and handle that Intent result to get file. – Raghavendra Oct 08 '15 at 13:14
  • @Raghvendra, I am using startActivityResult in the Adapter but i am not able to handle result inside adapter as i am not aware where to use onActivityResult.Here the adpater is populating the list view inside a Fragment. – Deepak Rattan Oct 09 '15 at 04:37

1 Answers1

1

OnActivityResult will be called on the caller activity, take a look at this

Also, you can learn more from here

Hope this helps.

Community
  • 1
  • 1
Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • Thanks Nanoc ,but i am still having the issue. Actually my problem is that i am using startActivityForResult in a custom adapter extending base adapter .This custom adapter is populating list view inside a fragment which means custom adapter is creating views for a fragment.Inside this fragment, i am using the onActivityResult method . I have to handle the results here only.Please help. – Deepak Rattan Oct 09 '15 at 06:48
  • My first link shows how to receive OnActivityResult inside the fragment have you tried it? – Nanoc Oct 09 '15 at 06:57
  • Nanoc ,In your code,you are using startActivityForResult and onActivityResult inside the same fragment but my problem is different.I am picking a file from my android phone using startActivityForResult method in adapter .I have to use the path of selected file using onActivityResult inside fragment.I think onActivityResult is not working for me. In short , startActivityForResult is inside adapter and onActivityResult is inside a fragment . – Deepak Rattan Oct 09 '15 at 07:11
  • It should not make any difference, why dont you use an interface to pass the data to where you need it. – Nanoc Oct 09 '15 at 07:32
  • Nanoc , Please check my code and tell me where is the problem as i am not able to resolve it till now. – Deepak Rattan Oct 09 '15 at 07:40
  • That links says it should work by changing ((Activity) context).startActivityForResult(... to YOURFRAGMENTCLASS.this.startActivityForResult(... have you tried that? – Nanoc Oct 09 '15 at 07:53
  • Nanoc,it is not working. ProfessionalFragmentTab.this.startActivityForResult(intent,PICKFILE_RESULT_CODE); – Deepak Rattan Oct 09 '15 at 08:04
  • Then i can only tell you to debug your app to find where is it returning the result and then manage to post it to where you need it. – Nanoc Oct 09 '15 at 08:32