Android: BaseAdapter does not show items

The application I'm trying to do with has a simple list and a button at the bottom, no matter what. My problem is that my custom BaseAdapter does not show elements. I know that since my elements are just a string, I can use the ArrayAdapter, but this requires an assignment. Code:

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"); } } 

My XML files are as follows:

 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> 

The down button opens a dialog box that adds items to my list. Obviously this will not work, but I think it is BaseAdapter (ListaOrase) itself, since intentions do not raise any exceptions. I would be grateful that objects that I hard-coded (Bucuresti and Sibiu) would be detected.

What am I doing wrong? Thank you very much!:)

+4
source share
2 answers

Your code is almost perfect - this is to return the orase.size () method to getcount (), you return 0, add unrealized methods to the ListaOrase class. your ListaOrase should look like this:

 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; } } 

hope this helps

+7
source

getCount method actually returned 0, so getView() not called. When I changed to:

 getcount() { return arrayObject.Size(); } 
+1
source

Source: https://habr.com/ru/post/1384480/


All Articles