Define onClick on a BaseAdapter

2

This is a question that I have searched for on this page, but the solutions that I have been finding, I have implemented them, but they have not worked for me, and they will appear commented on in the code, to see if something escapes me or any nonsense but I do not get it out.

What I want to do is: I have a ListView , and when I click on an element show me "You have selected XXXXX element" , and now comes what I want to implement, the method onClick , then, always the application stops and there is no way.

Important: I work with Fragments , my ListView is in Fragment and I want it to take me to another Fragment .

public class UsersAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;
    private List<User> allUsers;

    private Context context;

    /*************
     * Constructor
     *****************/
    public UsersAdapter(Context ctx, ArrayList<User> data) {

        context = ctx;
        allUsers = data;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = convertView;

        UserViewHolder holder = new UserViewHolder();

        final User user = allUsers.get(position);

        if(convertView==null)
        {
            rowView = inflater.inflate(R.layout.boxer_item, null);
            holder.name = (TextView) rowView.findViewById(R.id.txt_name);
            holder.icon = (ImageView) rowView.findViewById(R.id.img_user);

            rowView.setTag(holder);
        }
        else
        {
            holder = (UserViewHolder) rowView.getTag();
        }
        holder.name.setText(user.getName());

        Glide.with(context).load(user.getImage()).into(holder.icon);

        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked " + user.getName(), Toast.LENGTH_SHORT).show();

// Lo que hago en este "if" es comparar lo que pulso con el nombre del elemento, para evitar una búsqueda más compleja. 

               if (user.getName() == "Ramon \nDekkers"){
                   Toast.makeText(context, "You ClickedFFFFFFFFFFFFEFEFF " + user.getName(), Toast.LENGTH_LONG).show(); 

                  ////////////// ESTO ES LO QUE INTENTÉ YO /////////////////

                 //  Intent ramon = new Intent (context,Ramon_dekkers.class);
                //   ramon.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

               //   context.startActivity(ramon);

                   //v.getContext().startActivity(context.getApplicationContext(),Ramon_dekkers.class);
                 //  v.getContext().startActivity(new Intent(context,Ramon_dekkers.class));
 ''////////////// ESTO ES LO QUE INTENTÉ YO /////////////////''

               }
            }
        });
        return rowView;
    }
    private static class UserViewHolder {
        public ImageView icon;
        public TextView name;
    }
}

The error that shows me is the following:

  

FATAL EXCEPTION: main android.content.ActivityNotFoundException:   Unable to find explicit activity class and then if declared in   the manifest.xml

    
asked by Rf Mvs 30.10.2016 в 22:08
source

2 answers

0

Friend you can do it in several ways and more easily. Right now, 2 ways to do it come to mind. By the way, I think it's a better option to have a LinearLayout or Realtive container and to do this to it:

rowView.setOnClickListener(new View.OnClickListener() {

For example:

linear.setOnClickListener(new View.OnClickListener() {

Because you can not set it directly to ParentView.

And with respect to the click:

You can create an Interface with a method that can be

onClick(View v)

and then send it to call and use it as best suits.

  • Another solution is to create a method on your adapter that will return the item to which you are clicking.
  • listview.setOnItemClickListener(new OnItemClickListener(){   
            @Override
            public void onItemClick(AdapterView<?>adapter,View v, int position){
                String item = adapter.getItem(position);
    
     Toast.makeText(getApplicationContext(),"Position :"+position+"  ListItem : " +item ,Toast.LENGTH_LONG).show();
            }
       });
    

    And in your adapter you can create a method that is something like this: (

    public String getItem(int position){
        return allUsers.get(position);
    }
    
        
    answered by 12.05.2017 в 15:21
    0

    Fragments are managed differently from activities. In the code that you attach, a startAcivity is made to which you should pass an activity, hence it tells you that the activity is not declared in the manifest. To show a navigate to a fragment you must include it with a transaction in the FragmentManager. I attached an example taken from the official Android Developers documentation in which an existing fragment is replaced by the new one we want to navigate:

    // Create new fragment and transaction
    Fragment newFragment = new ExampleFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    
    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);
    
    // Commit the transaction
    transaction.commit();
    

    source: link

        
    answered by 22.05.2017 в 17:30