Android: Basadapter non mostra elementi
-
27-10-2019 - |
Domanda
L'app che sto lottando per fare ha un semplice elenco e un pulsante presente in fondo, non importa quale. Il mio problema è che il mio Basadapter personalizzato non mostra elementi. So che poiché i miei elementi sono solo una stringa, potrei usare un ArrayAdapter, ma l'incarico lo richiede. Il codice:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
element = inflater.inflate(R.layout.lista, null);
}
else element = convertView;
TextView elementLista=(TextView)element.findViewById(R.id.elementLista);
elementLista.setText(orase.get(position));
return element;
}
}
public class WeatherAppActivity extends ListActivity {
Button buton;
ListaOrase lista;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lista=new ListaOrase(this);
buton=(Button)findViewById(R.id.buton);
setListAdapter(lista);
lista.add("Bucuresti");
lista.add("Sibiu");
}
}
I miei file XML vanno così:
main.xml -- for the Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_alignParentTop="true"/>
<RelativeLayout
android:id="@+id/relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/buton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="add"
android:text="Add"
android:layout_gravity=""
/>
</RelativeLayout>
</RelativeLayout>
lista.xml -- for the list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/elementLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Il pulsante "al piano di sotto" apre una finestra di dialogo che aggiunge elementi alla mia lista. Ovviamente non funziona, ma penso che sia lo stesso Basadapter (ListAorase), poiché gli intenti non lanciano eccezioni. Sarei grato se gli oggetti in cui ho codificato si sono presentati (Bucuresti e Sibiu).
Che cosa sto facendo di sbagliato? Grazie mille! :)
Soluzione
Il tuo codice quasi perfetto è return orase.size () Nel metodo getCount (), stai restituendo 0, aggiungi metodi non implementati nella classe Listaorasi. La tua lista dovrebbe essere così:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
element = inflater.inflate(R.layout.lista, null);
}
else element = convertView;
TextView elementLista=(TextView)element.findViewById(R.id.elementLista);
elementLista.setText(orase.get(position));
Log.e("",orase.get(position));
return element;
}
public int getCount() {
// TODO Auto-generated method stub
return orase.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
spero che sia di aiuto
Altri suggerimenti
getCount
Il metodo stava realmente restituendo 0 in modo talente getView()
non era stato chiamato. Quando sono cambiato in:
getcount() {
return arrayObject.Size();
}