1
I have the class answer
public class Resposta {
private String Nome;
private boolean Certo_Errado;
public Resposta(String nome, boolean certo_Errado) {
Nome = nome;
Certo_Errado = certo_Errado;
}
public String getNome() {
return Nome;
}
public void setNome(String nome) {
Nome = nome;
}
public boolean isCerto_Errado() {
return Certo_Errado;
}
public void setCerto_Errado(boolean certo_Errado) {
Certo_Errado = certo_Errado;
}
The custom Adapter
public class Resposta_Adapter extends ArrayAdapter<Resposta> {
private final Context context;
private final List<Resposta> Respostas;
public Resposta_Adapter(Context context, List<Resposta> respostas) {
super(context, R.layout.list_respostas);
this.context = context.getApplicationContext();
this.Respostas = respostas;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_respostas, parent, false);
Resposta rsp = Respostas.get(position);
TextView tc = view.findViewById(R.id.txt);
tc.setText(rsp.getNome());
return rowView;
}
}
And in the main activity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Resposta> oRespostas = new ArrayList<Resposta>();
for (int i = 0; i < 5; i++) {
boolean a = false;
if (i == 2)
a = true;
else
a = false;
oRespostas.add(new Resposta("Resposta " + i, a));
}
ListView lista = (ListView) findViewById(R.id.lista);
Resposta_Adapter adapter;
adapter = new Resposta_Adapter(getApplicationContext(), oRespostas);
lista.setAdapter(adapter);
}
}
But listview is not being filled in. I can’t see where it might be wrong
Edit: XML of the main screen: `
<TextView
android:id="@+id/txt_Pergunta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Padrao"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="italic" />
<ListView
android:id="@+id/lista"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/txt_Pergunta"
/>
`
TextView tc = rowView.findViewById(R.id.txt);
should resolve. Note that what you are doing in the methodgetView()
is inefficient. Consider using Viewholder Pattern– ramaral
@ramaral did not work
– Marceloawq
put the listview screen code
– Weslley Barbosa
try using this instead of getApplicationContext()
– Weslley Barbosa
@Weslleybarbosa tried and it didn’t work.
– Marceloawq