如何在扩展BaseAdapter

时间:2015-10-08 10:38:15

标签: android android-fragments

我正在开发一个名为DashboardActivity的Activity,它包含两个选项卡,即SocialFragmentTab和ProfessionalFragmentTab。这两个都是Fragments here.In ProfesionalFragmentTab我有一个列表视图,由一个自定义适配器填充BaseAdapter.Inside ProfesionalFragmentTab,“Apply”按钮就在那里。点击apply buttton,打开一个包含一些信息的对话框。它还包含一个按钮“选择最新的简历”。点击此按钮我想从我的Android手机中选择简历(一个文本文件)。我在这里使用startActivityForResult()时遇到了一些问题。 Plz检查文件。点击“选择最新简历按钮”

,我无法浏览文件

FeedListProfessionalTimelineAdapter.java

public class FeedListProfessionalTimelineAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<FeedItemProfessionalTimeline> listProfessionalTimeline;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    private static final int PICKFILE_RESULT_CODE = 1;

    public FeedListProfessionalTimelineAdapter(Context context, List<FeedItemProfessionalTimeline> listProfessionalTimeline) {
        this.context = context;
        this.listProfessionalTimeline = listProfessionalTimeline;
    }


    @Override
    public int getCount() {
        return listProfessionalTimeline.size();
    }

    @Override
    public Object getItem(int position) {
        return listProfessionalTimeline.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    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.setTitle("Apply for JOB");
                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 Button resume
                Button resumeBtn = (Button) view.findViewById(R.id.btnResume);
                resumeBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(v.getContext(),"clicked",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                        intent.setType("file/*");
                        Context context = v.getContext();
                        ((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;
        }

    }

1 个答案:

答案 0 :(得分:0)

替换它,因为你已经在上面有上下文:

 resumeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(),"clicked",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("file/*");
                    //Context context=v.getContext();
                    ((Activity)context).startActivityForResult(intent, PICKFILE_RESULT_CODE);                    }
                });

            }

        });