I have a custom RecyclerView combined with CardView or vice versa. Here is the adapter:
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> { private ArrayList<Integer> alName; private ArrayList<Integer> alImage; private Context context; @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, viewGroup, false); return new ViewHolder(v); } MainAdapter(Context context, ArrayList<Integer> alName, ArrayList<Integer> alImage) { super(); this.context = context; this.alName = alName; this.alImage = alImage; } @Override public void onBindViewHolder(@NonNull final MainAdapter.ViewHolder viewHolder, final int position) { viewHolder.tvSpecies.setText(alName.get(position)); viewHolder.imgThumbnail.setImageResource(alImage.get(position)); viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int k = viewHolder.getAdapterPosition(); switch (k) { case 0: Intent intent = new Intent(context, MessageCenter.class); context.startActivity(intent); break; case 1: Toast.makeText(context, "JobAgent", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(context, "no service))", Toast.LENGTH_SHORT).show(); break; }} }); } @Override public int getItemCount() { return alName.size(); } public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { ImageView imgThumbnail; TextView tvSpecies; ItemClickListener clickListener; ViewHolder(View itemView) { super(itemView); imgThumbnail = itemView.findViewById(R.id.imgActivity); tvSpecies = itemView.findViewById(R.id.tvDescription); itemView.setOnClickListener(this); } @Override public void onClick(View view) { clickListener.onClick(view, getPosition(), false); } } } As you can see from the code, when choosing one of the items from this improvised list, I switch to another activation. But then I want to do something more hard to implement, for me personally, I want to call an activity that is based on ListActivity . It seems to be similar to the alert dialog, but this is not very much what I need, or I am not very correct on how to make a dialog with such functionality. Here is the adapter and the activation itself:
public class Action_List extends ListActivity { public static String RESULT_ACTION = "action_result"; public String[] action_names; private List<Action> actionList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); populateCountryList(); ArrayAdapter<Action> adapter = new ActionList_adapter(this, actionList); setListAdapter(adapter); getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Action c = actionList.get(position); Intent returnIntent = new Intent(); returnIntent.putExtra(RESULT_ACTION, c.getName()); setResult(RESULT_OK, returnIntent); finish(); } }); } private void populateCountryList() { actionList = new ArrayList<>(); action_names = getResources().getStringArray(R.array.action_list); for (int i = 0; i < action_names.length; i++) { actionList.add(new Action_List.Action(action_names[i])); } } public class Action { private String name; Action(String name) { this.name = name; } public String getName() { return name; } }} adapter:
class ActionList_adapter extends ArrayAdapter<Action_List.Action> { private final List<Action_List.Action> list; private final Activity context; static class ViewHolder { protected TextView name; } ActionList_adapter(Activity context, List<Action_List.Action> list) { super(context, R.layout.activity_action__list, list); this.context = context; this.list = list; } @SuppressLint("InflateParams") @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { View view; if (convertView == null) { LayoutInflater inflator = context.getLayoutInflater(); view = inflator.inflate(R.layout.activity_action__list, null); final ActionList_adapter.ViewHolder viewHolder = new ActionList_adapter.ViewHolder(); viewHolder.name = view.findViewById(R.id.name); view.setTag(viewHolder); } else { view = convertView; } ViewHolder holder = (ViewHolder) view.getTag(); holder.name.setText(list.get(position).getName()); return view; } } and here's what I'm actually trying to do - call this activation on a par with the usual activation from my custom list. After all my attempts, it starts to seem to me that it may be easier to do it through an alert, but still there is a slight desire to figure out - it is possible or still not possible to do what I'm trying to do with calling this function.
onActivityResult()is not called from the code. This method will be called by the activation, if it launched anotherstartActivityForResult()by thestartActivityForResult()method and it ended. - Eugene Krivenja