Select items with LongClick, imagebutton

1

I have the items in a list, which when clicking the imagebutton, sends a message to the user.

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

Thanks and follow the codes:

Present 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 listview generated 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>
    
asked by anonymous 05.12.2016 / 17:29

1 answer

1

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

Inside the click of the button ...

You will have to save these values when the user makes a long click on the item.

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

You'll need to run this code for all items when the user clicks the submit button. In other words, get 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 once.

notifyDataSetChanged(); 

This solution is not the best. I just suggested changes to the classes you presented. It would be interesting if your updateDataBaseInvited method would look like this.

updateDataBaseInvited(List<inviteUsers> invites, String message)

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

    
05.12.2016 / 19:56