Can I make an adapter with an activity?

1

I'm a novice programmer and I'm practicing creating screens and other things, including a ListView. The case is that I created 3 activities, the second screen (Activity) is linked to a third one that says nothing and I wanted to take advantage of that to create the Adapter class there, but the AS does not allow it, because it is a different class from AppCompatActivity. I need to create a different class? Can I link that Adapter class with the second activity?

Thank you!

  

This would be the second screen

public class SecondActivity extends AppCompatActivity {


private Button acepto, noacepto;

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

    acepto= (Button) findViewById(R.id.bacepto);
    noacepto=(Button)findViewById(R.id.bnacepto);

    acepto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                startActivity(intent);
                finish();
            }
        });

    noacepto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(SecondActivity.this, "Debes aceptar las condiciones para avanzar", Toast.LENGTH_SHORT).show();
                    //(MainActivity.this, "Los campos estan vacios", Toast.LENGTH_SHORT).show();
        }
    });
    }

}
  

Which brings me to this, where I have a TextView in the XML (Adapter_a3) that says "Product List"

public class ThirdActivity extends AppCompatActivity {

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

}

  

What I want to do is write the adapter code in this Activity, to be able to link it to the same Adapter_a3 layout (the a3 is from Activity 3, so as not to get lost)

public class Adapter extends BaseAdapter {

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return null;
}

}

  

But he will not let me ... I do not know if I explained myself well

    
asked by Brenda Yanela Conzi 05.12.2017 в 02:59
source

1 answer

1

You can use a adapter in the same activity or create a new class depending on the complexity of the data to load. These two forms are implemented differently.

If you want to fill your listview in the same class:

ListView lista;
ArrayAdapter<String> listaAdapter; 

// Instancia del ListView.
lista = (ListView) root.findViewById(R.id.lista_1);

String[] lista_Nombres = { // creamos una cadena para llenar el adaptador
        "ejemplo_1",
        "ejemplo_2",
        "ejemplo_3",
        "ejemplo_4"

listaAdapter = new ArrayAdapter<>( // llenamos el adaptador con nuestro arreglo de String's
        getActivity(),
        android.R.layout.simple_list_item_1,
        lista_Nombres);

lista.setAdapter(listaAdapter); // finalmente vincular tu listview con el adaptador.

Emphasize that this is a basic example of a listivew , if you want to customize this control it is better to use a class independent of the activity, it will be easier for you ..

If you want to learn more you can check the following link , it contains complete information.

    
answered by 05.12.2017 в 06:25