How to have only one adapter for multiple Activity?

1

I would like to know if in some way it is recommended to have an adapter for each Activity .

I know what to do with an Adapter for every Activity is quiet.

Now how to make a generic Adapter , would it?

Would you be following the design patterns?

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 did not put layouts because I did not see any need.

    
asked by anonymous 02.12.2014 / 00:11

1 answer

1

I think I understood your doubt. You currently have the items being created on your adapter, however you want to use the same adapter with different items. If it is, you simply pass the list of elements in the constructor of your adapter, when you use the adapter in another screen, simply pass your list of elements making your adapter is easy to reuse, for example:

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

In your 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 have to have the list you need in your activity for each adapter.

    
13.04.2017 / 00:18