Android: ListView elements with multiple clickable buttons

后端 未结 8 1173
天命终不由人
天命终不由人 2020-11-22 01:58

I\'ve a ListView where every element in the list contains a TextView and two different Buttons. Something like this:

ListView
------------------         


        
相关标签:
8条回答
  • 2020-11-22 02:24

    I am not sure about be the best way, but works fine and all code stays in your ArrayAdapter.

    package br.com.fontolan.pessoas.arrayadapter;
    
    import java.util.List;
    
    import android.content.Context;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ImageView;
    import br.com.fontolan.pessoas.R;
    import br.com.fontolan.pessoas.model.Telefone;
    
    public class TelefoneArrayAdapter extends ArrayAdapter<Telefone> {
    
    private TelefoneArrayAdapter telefoneArrayAdapter = null;
    private Context context;
    private EditText tipoEditText = null;
    private EditText telefoneEditText = null;
    private ImageView deleteImageView = null;
    
    public TelefoneArrayAdapter(Context context, List<Telefone> values) {
        super(context, R.layout.telefone_form, values);
        this.telefoneArrayAdapter = this;
        this.context = context;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.telefone_form, parent, false);
    
        tipoEditText = (EditText) view.findViewById(R.id.telefone_form_tipo);
        telefoneEditText = (EditText) view.findViewById(R.id.telefone_form_telefone);
        deleteImageView = (ImageView) view.findViewById(R.id.telefone_form_delete_image);
    
        final int i = position;
        final Telefone telefone = this.getItem(position);
        tipoEditText.setText(telefone.getTipo());
        telefoneEditText.setText(telefone.getTelefone());
    
        TextWatcher tipoTextWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                telefoneArrayAdapter.getItem(i).setTipo(s.toString());
                telefoneArrayAdapter.getItem(i).setIsDirty(true);
            }
        };
    
        TextWatcher telefoneTextWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                telefoneArrayAdapter.getItem(i).setTelefone(s.toString());
                telefoneArrayAdapter.getItem(i).setIsDirty(true);
            }
        };
    
        tipoEditText.addTextChangedListener(tipoTextWatcher);
        telefoneEditText.addTextChangedListener(telefoneTextWatcher);
    
        deleteImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                telefoneArrayAdapter.remove(telefone);
            }
        });
    
        return view;
    }
    
    }
    
    0 讨论(0)
  • 2020-11-22 02:31

    I Know it's late but this may help, this is an example how I write custom adapter class for different click actions

     public class CustomAdapter extends BaseAdapter {
    
        TextView title;
      Button button1,button2;
    
        public long getItemId(int position) {
            return position;
        }
    
        public int getCount() {
            return mAlBasicItemsnav.size();  // size of your list array
        }
    
        public Object getItem(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
    
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.listnavsub_layout, null, false); // use sublayout which you want to inflate in your each list item
            }
    
            title = (TextView) convertView.findViewById(R.id.textViewnav); // see you have to find id by using convertView.findViewById 
            title.setText(mAlBasicItemsnav.get(position));
          button1=(Button) convertView.findViewById(R.id.button1);
          button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //your click action 
    
               // if you have different click action at different positions then
                if(position==0)
                  {
                           //click action of 1st list item on button click
            }
               if(position==1)
                  {
                           //click action of 2st list item on button click
            }
        });
    
     // similarly for button 2
    
       button2=(Button) convertView.findViewById(R.id.button2);
          button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //your click action 
    
        });
    
    
    
            return convertView;
        }
    }
    
    0 讨论(0)
提交回复
热议问题