How to have only one Adapter for various Activity?

Asked

Viewed 146 times

1

I wonder if in any way or is it recommended to have a Adapter for each Activity.

I know what to do with a Adapter for each Activity is quiet.

Now how to make a Adapter generic, would have as?

Would be following the design standards?

My Activity

public class CardapioPizzaria extends Activity {

    ListView listViewCardapio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cardapio_pizzaria);

        this.listViewCardapio = (ListView) findViewById(R.id.listCardapio);

        CardapioAdapter cardapioAdapter = new CardapioAdapter(this);
        this.listViewCardapio.setAdapter(cardapioAdapter);

    }
}

Adapter

public class CustomAdapter extends BaseAdapter {

    private Context context;

    @SuppressWarnings("unused")
    private static final String TAG = CustomAdapter.class.getSimpleName();
    ArrayList<DataModel> listArray;

    /*Construtor*/
    public CustomAdapter(Context context) {
        this.context = context;
        listArray = new ArrayList<DataModel>();
        listArray.add(new DataModel("Bom Filé","3214 - 2435")); 
        listArray.add(new DataModel("Porcão","3214 - 2435"));
        listArray.add(new DataModel("Palmarys","3214 - 2435")); 
        listArray.add(new DataModel("Fat Boy","3217 - 2452")); 
        listArray.add(new DataModel("Fratelli","3215 - 5010")); 
       listArray.add(new DataModel("Pizza Paulista","3214 - 3271")); 
        listArray.add(new DataModel("Fogão a Lenha","3215 - 5168")); 
       listArray.add(new DataModel("Alô Pizza","3214 - 4193")); 
    }

    /**Total de elementos dentro do array */
    public int getCount() {
        return listArray.size();
    }

    /**Pegando o item da lista */
    public Object getItem(int i) {
        return listArray.get(i);
    }

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

    @Override
    public View getView(int index, View view, final ViewGroup parent) {

        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.itens_lista, parent, false);
        }

        final DataModel dataModel = listArray.get(index);

        TextView textView = (TextView) view.findViewById(R.id.nome_estabelecimento);
        textView.setText(dataModel.getName());

        TextView textViewTelefone = (TextView) view.findViewById(R.id.numeroTelefone);
        textViewTelefone.setText("" + dataModel.getTelefone());

        Button buttonLigar = (Button) view.findViewById(R.id.btn_ligar_pizza);
        Button buttonVerCardapio = (Button) view.findViewById(R.id.btn_ver_cardapio_pizza);

        buttonLigar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri url = Uri.parse("tel: " + dataModel.getTelefone());
                Intent it = new Intent(Intent.ACTION_CALL,url);
                context.startActivity(it);    
            }
        });

        buttonVerCardapio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(context, CardapioPizzaria.class);
                context.startActivity(it);   
            }
        });

       return view;
    }
}

I didn’t put the layouts for I saw no need.

  • There is no problem in sharing the same class of Adapter for several Activity. Or did you mean instance? It is very common to take advantage of classes if it is beneficial (no use sharing if it starts to get too complex or confused)... This is very subjective, it will vary from project to project.

  • you would have an example of this sharing???

  • For example I in my project have two Activities’s with two lists of different things. But the lists are so similar that I use the same Adapter for both lists. If you have to make many changes to the Adapter to be generic then you are reusing and yes complicate.

1 answer

1

I think I understand your question. Currently you have items being created on your Adapter, but you want to use the same Adapter with different items. If that is so, just pass the list of elements in your Adapter builder, when you are going to use the Adapter on another screen, just pass your list of elements making your Adapter easy to reuse, for example:

/*Construtor*/
    public CustomAdapter(Context context,ArrayList<DataModel> elements) {
        this.context = context;
        this.listArray = elements;
    }

In his Activity:

     public class CardapioPizzaria extends Activity {

            ListView listViewCardapio;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_cardapio_pizzaria);

                this.listViewCardapio = (ListView) findViewById(R.id.listCardapio);

                CardapioAdapter cardapioAdapter = new CardapioAdapter(this,getList());
                this.listViewCardapio.setAdapter(cardapioAdapter);

            }

       private ArrayList<DataModel> getList(){

            ArrayList<DataModel> listArray = new ArrayList<DataModel>();
            listArray.add(new DataModel("Bom Filé","3214 - 2435")); 
            listArray.add(new DataModel("Porcão","3214 - 2435"));
            listArray.add(new DataModel("Palmarys","3214 - 2435")); 
            listArray.add(new DataModel("Fat Boy","3217 - 2452")); 
            listArray.add(new DataModel("Fratelli","3215 - 5010")); 
            listArray.add(new DataModel("Pizza Paulista","3214 - 3271")); 
            listArray.add(new DataModel("Fogão a Lenha","3215 - 5168")); 
            listArray.add(new DataModel("Alô Pizza","3214 - 4193")); 
            return listArray;
       }
}

This way you only need to have the list you need in your Activity for each Adapter.

Browser other questions tagged

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