gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:51:14 25 4
gpt4 key购买 nike

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

这里是adapter的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;
}

fragment 中的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();
}
}

所以我在自定义适配器中使用了 startActivityForResult 并在 fragment 中使用了 onActivityResult 方法,因为适配器正在填充 fragment 中的 ListView 。但是 Toast 在这里不起作用。请帮忙。

最佳答案

OnActivityResult 将在调用者 Activity 上调用,请查看 this

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

希望这对您有所帮助。

关于android - 如何在扩展 BaseAdapter 的适配器中使用 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33016797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com