session-android/src/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java

107 lines
3.0 KiB
Java
Raw Normal View History

2014-01-18 18:17:08 -08:00
package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.recipients.Recipient;
import java.util.ArrayList;
public class SelectedRecipientsAdapter extends ArrayAdapter<SelectedRecipientsAdapter.RecipientWrapper> {
2014-01-18 18:17:08 -08:00
private ArrayList<RecipientWrapper> recipients;
private OnRecipientDeletedListener onRecipientDeletedListener;
2014-01-18 18:17:08 -08:00
public SelectedRecipientsAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public SelectedRecipientsAdapter(Context context, int resource, ArrayList<RecipientWrapper> recipients) {
2014-01-18 18:17:08 -08:00
super(context, resource, recipients);
this.recipients = recipients;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.selected_recipient_list_item, null);
}
final RecipientWrapper rw = getItem(position);
final Recipient p = rw.getRecipient();
final boolean modifiable = rw.isModifiable();
2014-01-18 18:17:08 -08:00
if (p != null) {
TextView name = (TextView) v.findViewById(R.id.name);
TextView phone = (TextView) v.findViewById(R.id.phone);
ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
if (name != null) {
name.setText(p.getName());
}
if (phone != null) {
phone.setText(p.getNumber());
}
if (delete != null) {
if (modifiable) {
delete.setVisibility(View.VISIBLE);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onRecipientDeletedListener != null) {
onRecipientDeletedListener.onRecipientDeleted(recipients.get(position).getRecipient());
}
recipients.remove(position);
SelectedRecipientsAdapter.this.notifyDataSetChanged();
}
});
} else {
delete.setVisibility(View.INVISIBLE);
delete.setOnClickListener(null);
}
2014-01-18 18:17:08 -08:00
}
}
return v;
}
public void setOnRecipientDeletedListener(OnRecipientDeletedListener listener) {
onRecipientDeletedListener = listener;
}
2014-01-18 18:17:08 -08:00
public interface OnRecipientDeletedListener {
public void onRecipientDeleted(Recipient recipient);
2014-01-18 18:17:08 -08:00
}
public static class RecipientWrapper {
private final Recipient recipient;
private final boolean modifiable;
public RecipientWrapper(final Recipient recipient, final boolean modifiable) {
this.recipient = recipient;
this.modifiable = modifiable;
}
public Recipient getRecipient() {
return recipient;
}
public boolean isModifiable() {
return modifiable;
}
}
2014-01-18 18:17:08 -08:00
}