Select items with Longclick, imagebutton

Asked

Viewed 78 times

1

I have the items of a list, which when clicking on the imagebutton, a message is sent to the user.

I would like to implement longclick so that I can select multiple and send messages to multiple users at once.

Thank you and follow the codes:

Presenter with clicks logic:

public class InvitePresenter extends BaseAdapter implements AdapterView.OnItemClickListener, AdapterView.OnLongClickListener{

    private InviteView inviteView;
    private List<UserCommunity> usersList = new ArrayList<UserCommunity>();
    LayoutInflater inflater;
    private UsersMySQLiteHelper serviceDB;
    private CommunityService service;
    private SessionManager sessionManager = new SessionManager();
    private EditText search;

    public InvitePresenter(InviteView inviteView){

        inflater = (LayoutInflater)inviteView.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        serviceDB = new UsersMySQLiteHelper(AppController.getAppContext());

        RequestManager.Users(new FutureCallback<JsonObject>() {
            @Override
            public void onCompleted(Exception e, JsonObject result) {

                if (!sessionManager.getInvitedUser()) { //verifica nos preferences se já passou pela hash
                    usersList = new Gson().fromJson(result.get("data"), new TypeToken<ArrayList<UserCommunity>>() {
                    }.getType());
                    sessionManager.setInvitedUser(true);
                }else{ //se já passou, busca do banco os usuários
                    usersList = serviceDB.getUsersToInvite(0);
                }
                notifyDataSetChanged();
            }
        });
        this.inviteView = inviteView;
        this.service = CommunityService.i(inviteView.getContext());
}

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //recupera o estado da posição atual
        final UserCommunity inviteUsers = usersList.get(position);

        //Cria uma instancia do layout .. na view
        View view = inflater.inflate(R.layout.item_invite_listview,null);

        TextView txt_Nome = (TextView)view.findViewById(R.id.txt_nome_invite);
        TextView txt_Email = (TextView)view.findViewById(R.id.txt_email_invite);
        TextView txt_Distancia = (TextView)view.findViewById(R.id.txt_distancia_invite);
        ImageButton btn_Share = (ImageButton)view.findViewById(R.id.img_invite);

        txt_Nome.setText(inviteUsers.name);
        txt_Email.setText(inviteUsers.email);
        txt_Distancia.setText(Integer.toString(inviteUsers.distance));

        btn_Share.setTag(position);
        btn_Share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = "Mensagem de invite";
                usersList.remove(position);
                service.usersInvited(inviteUsers.id,message,inviteUsers.name);
                notifyDataSetChanged();
                serviceDB.updateDataBaseInvited(inviteUsers.id,1);
            }
        });

    btn_Share.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            return true;
        }
    });

        return view;
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Log.e("TOP","Doidera");
    }


    public void searchEmail(String q, String company,int invited) {
        usersList = serviceDB.searchEmailInvite(q,company,invited);
        if (usersList != null){
            usersList.clear();
            usersList = serviceDB.searchEmailInvite(q,company,invited);
            notifyDataSetChanged();
        }else{
            Toast.makeText(AppController.getAppContext(),"Usuário não encontrado",Toast.LENGTH_SHORT).show();
        }
    }

    public static void hideSoftKeyboard(Activity activity) {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            View currentFocus = activity.getCurrentFocus();
            if (currentFocus != null) {
                inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onLongClick(View v) {
        //selecionar vários e enviar
        return true;
    }
}

XML of generated listview items:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/background_light_blue">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/bynd_teal"
        android:padding="3dp"
        >

        <ImageButton
            android:id="@+id/img_invite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_send_white_48px"
            android:background="@drawable/background_btn_submit"
            />

        <TextView
            android:id="@+id/txt_nome_invite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/img_invite"
            android:layout_toRightOf="@+id/img_invite"
            android:paddingLeft="5dp"
            android:text="Nome"
            android:textSize="20sp"
            android:textStyle="bold"
            />

        <TextView
            android:id="@+id/txt_email_invite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txt_nome_invite"
            android:layout_toRightOf="@+id/img_invite"
            android:paddingLeft="5dp"
            android:text="Email"
            android:textStyle="bold"
            />

        <TextView
            android:id="@+id/txt_distancia_invite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/txt_nome_invite"
            android:text="100000"
            android:textStyle="bold"
            android:layout_marginRight="5dp"
            />

    </RelativeLayout>

</ScrollView>

1 answer

1


You have to set the time to send. use a button for example.

Inside the button click...

You will have to save these values when the user clicks long on the item.

message = "Mensagem de invite",
position,
inviteUsers.id,
inviteUsers.name

You’ll have to execute this code, to all items, when the user clicks on the send button. That is, take the previously saved values and loop them.

for(/* para todos os inviteUsers salvos */){
    usersList.remove(position);    
    serviceDB.updateDataBaseInvited(inviteUsers.id,1);        
    service.usersInvited(inviteUsers.id,message,inviteUsers.name);        
}

Then run this line only once.

notifyDataSetChanged(); 

This solution is not the best. I just suggested the changes of the classes you presented. It would be interesting that your method updateDataBaseInvited remain so.

updateDataBaseInvited(List<inviteUsers> invites, String message)

This would avoid the loop inside your Adapter and leave the code less coupled.

  • Really the best would be to use the listview itself. But as it is assembled today, I have to use so, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.