Java 在RecycleServiceAdapter中使用onActivityResult
我正在尝试在我的Java 在RecycleServiceAdapter中使用onActivityResult,java,android,android-recyclerview,onactivityresult,Java,Android,Android Recyclerview,Onactivityresult,我正在尝试在我的RecycleServiceAdapter中使用onActivityResult。但我得到一个错误,即“该方法不从其超类重写”。我尝试实现接口,但从未调用过activityresult。我已经在StackOverflow问题上搜索了很多,但是没有找到任何有用的东西 RecycleServiceAdapter.java package com.example.waheed.telegramchat; import android.app.Activity; import andr
RecycleServiceAdapter
中使用onActivityResult
。但我得到一个错误,即“该方法不从其超类重写”。我尝试实现接口,但从未调用过activityresult。我已经在StackOverflow问题上搜索了很多,但是没有找到任何有用的东西
RecycleServiceAdapter.java
package com.example.waheed.telegramchat;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
//String arrays to store user data
static String[] contact_name, Phone_no, user_id;
Context context;
public RecyclerAdapter(String[] contact_name, String[] Phone_no, String[] user_id, Context context) {
//Getting the data from MainActivity.java
this.context = context;
this.user_id = user_id;
this.contact_name = contact_name;
this.Phone_no = Phone_no;
}
@NonNull
@Override
public RecyclerAdapter.RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//Setting the row layout for Recyclerview
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, this.context);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
holder.tx_contact.setText(contact_name[position]);
holder.tx_phone.setText(Phone_no[position]);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return contact_name.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
Context context;
TextView tx_contact, tx_phone;
ImageButton message_btn;
static ImageView img_profile;
public RecyclerViewHolder(View itemView, Context context) {
super(itemView);
tx_contact = (TextView) itemView.findViewById(R.id.txt_contactname);
tx_phone = (TextView) itemView.findViewById(R.id.phonenumber);
message_btn = (ImageButton) itemView.findViewById(R.id.chat_icon);
img_profile = (ImageView) itemView.findViewById(R.id.profile_pic);
this.context = context;
//Calling onclick function
message_btn.setOnClickListener(this);
img_profile.setOnClickListener(this);
}
public void makeCall(String ID, String name, String packageName, String className) {
//
String data = "content://com.android.contacts/data/" + ID;
// Build the intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
// the _ids you save goes here at the end of /data/id
intent.setData(Uri.parse("content://com.android.contacts/data/" + ID));
intent.setDataAndType(Uri.parse(data), className);
intent.setPackage(packageName);
// Verify it resolves
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
Log.d("See this " + ID, activities.size() + "");
// Start an activity if it's safe
if (isIntentSafe) {
try {
//Toast to show which user is being called
context.startActivity(intent);
Toast.makeText(context, "Opening chat with " + name + " on Telegram", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, "OOPS!,Something went wrong\nPlease grant app permissions in setting or send feedback.", Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onClick(View v) {
//Getting the position of the item clicked
int i = getAdapterPosition();
//Storing the selected items in Strings
String name = RecyclerAdapter.contact_name[i];
String phone = RecyclerAdapter.Phone_no[i];
String ID = RecyclerAdapter.user_id[i];
String mimeType = "vnd.android.cursor.item/vnd.org.telegram.messenger.android.profile";
String packageName = "org.telegram.messenger";
switch (v.getId()) {
case R.id.chat_icon:
makeCall(ID, name, packageName, mimeType);
break;
case R.id.profile_pic:
Intent intent = new Intent(this.context, showImages.class);
((Activity) context).startActivityForResult(intent, 1);
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap;
if (requestCode == 1) {
if (resultCode == showImages.RESULT_OK) {
String result = data.getStringExtra("result");
Toast.makeText(this.context, "Clicked item is " + result, Toast.LENGTH_SHORT).show();
bitmap = BitmapFactory.decodeFile(result);
RecyclerViewHolder.img_profile.setImageBitmap(bitmap);
}
if (resultCode == showImages.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
}
}
package com.example.waheed.telegramchat;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.ImageAdapter;
import java.io.File;
import java.util.ArrayList;
public class showImages extends AppCompatActivity {
ImageAdapter myImageAdapter;
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_images);
gridView = (GridView) findViewById(R.id.gridview);
myImageAdapter = new ImageAdapter(this);
gridView.setAdapter(myImageAdapter);
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/Android/data/org.telegram.messenger/cache/";
File targetDirector = new File(targetPath);
final ArrayList<String> imagePaths = new ArrayList<String>();
final File[] files = targetDirector.listFiles();
for (File file : files) {
if (file.getName().toLowerCase().endsWith(".jpg") ||
file.getName().toLowerCase().endsWith(".png")) {
imagePaths.add(file.getAbsolutePath());
myImageAdapter.add(file.getAbsolutePath());
}
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String result = imagePaths.get(position);
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
setResult(showImages.RESULT_OK, returnIntent);
finish();
}
});
}
}
package com.example.waheed.telegramchat;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.content.pm.ResolveInfo;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.net.Uri;
导入android.support.annotation.NonNull;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageButton;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.List;
公共类RecyclerAdapter扩展了RecyclerView.Adapter{
//用于存储用户数据的字符串数组
静态字符串[]联系人姓名、电话号码、用户id;
语境;
公共回收适配器(字符串[]联系人姓名,字符串[]电话号码,字符串[]用户id,上下文){
//从MainActivity.java获取数据
this.context=上下文;
this.user\u id=user\u id;
this.contact\u name=contact\u name;
this.Phone\u no=电话号码;
}
@非空
@凌驾
public RecyclerAdapter.RecycleServiceWholder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
//设置Recyclerview的行布局
View=LayoutFlater.from(parent.getContext()).flate(R.layout.row\u布局,parent,false);
RecyclerViewHolder RecyclerViewHolder=新的RecyclerViewHolder(视图,this.context);
返回回收站;
}
@凌驾
BindViewHolder上的公共void(@NonNull RecycleServiceWholder holder,int位置){
holder.tx_contact.setText(联系人姓名[职位]);
holder.tx_phone.setText(电话号码[位置]);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
public int getItemViewType(int位置){
返回位置;
}
@凌驾
public int getItemCount(){
返回联系人_name.length;
}
公共静态类RecyclerViewWholder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
语境;
text查看发送联系人、发送电话;
图像按钮消息\u btn;
静态图像视图img_剖面图;
公共RecycleServiceWholder(查看项目视图、上下文){
超级(项目视图);
tx_contact=(TextView)itemView.findViewById(R.id.txt_contactname);
tx_phone=(TextView)itemView.findViewById(R.id.phonenumber);
message_btn=(ImageButton)itemView.findViewById(R.id.chat_图标);
img_profile=(ImageView)itemView.findViewById(R.id.profile_pic);
this.context=上下文;
//调用onclick函数
message_btn.setOnClickListener(此);
img_profile.setOnClickListener(此);
}
public void makeCall(字符串ID、字符串名称、字符串packageName、字符串类名){
//
字符串数据=”content://com.android.contacts/data/“+ID;
//建立意图
意图=新意图();
intent.setAction(intent.ACTION\u视图);
//保存的_id位于/data/id的末尾
intent.setData(Uri.parse(“content://com.android.contacts/data/“+ID”);
setDataAndType(Uri.parse(数据),类名);
intent.setPackage(packageName);
//验证它是否已解决
PackageManager PackageManager=context.getPackageManager();
列表活动=packageManager.QueryInputActivities(intent,0);
布尔值isIntentSafe=activities.size()>0;
Log.d(“参见此”+ID,activities.size()+”);
//如果安全的话,开始一项活动
如果(isIntentSafe){
试一试{
//Toast以显示正在调用的用户
背景。开始触觉(意图);
Toast.makeText(上下文,“在电报上用“+name+”打开聊天”,Toast.LENGTH_SHORT.show();
}捕获(例外e){
Toast.makeText(上下文,“OOPS!”,出现问题\n请在设置或发送反馈时授予应用程序权限。“,Toast.LENGTH_LONG).show();
}
}
}
@凌驾
公共void onClick(视图v){
//获取单击的项目的位置
int i=getAdapterPosition();
//以字符串形式存储所选项目
字符串名称=RecyclerAdapter.contact_name[i];
字符串phone=RecyclerAdapter.phone_no[i];
String ID=RecyclerAdapter.user_ID[i];
字符串mimeType=“vnd.android.cursor.item/vnd.org.telegrame.messenger.android.profile”;
字符串packageName=“org.telegrame.messenger”;
开关(v.getId()){
案例R.id.chat_图标:
makeCall(ID、name、packageName、mimeType);
打破
案例R.id.profile\u图片:
Intent Intent=新Intent(this.context,showImages.class);
((活动)上下文)。startActivityForResult(意图,1);
打破
}
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
位图;
if(requestCode==1){
if(resultCode==showmimages.RESULT\u确定){
String result=data.get
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap;
if (requestCode == 1) {
if (resultCode == showImages.RESULT_OK) {
String result = data.getStringExtra("result");
Toast.makeText(this.context, "Clicked item is " + result, Toast.LENGTH_SHORT).show();
bitmap = BitmapFactory.decodeFile(result);
RecyclerViewHolder.img_profile.setImageBitmap(bitmap);
}
if (resultCode == showImages.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String result = imagePaths.get(position);
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
setResult(showImages.RESULT_OK, returnIntent);
finish();
}
});