Question

i have a problem with displaying listview. i have tabbed activity and one of them contains listview. here's the code:

result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:id="@+id/Layout_result"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:background="@drawable/papero3">   
    <ListView 
      android:id="@+id/resultListView" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent">

        <TextView android:text="@string/main_no_items" 
            android:id="@+id/empty" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
        </TextView>

    </ListView>
</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/listItem"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:orientation="horizontal" 
  android:paddingLeft="10px" 
  android:paddingRight="10px" 
  android:paddingTop="5px" 
  android:paddingBottom="5px">


    <TextView android:text="This is object's name" 
    android:id="@+id/result_item_name_object" 
    android:layout_height="wrap_content" 
    android:singleLine="false"
    android:gravity="left"
    android:textSize="18sp" android:layout_width="260px">
    </TextView>


    <TextView android:text="null" 
    android:id="@+id/result_item_distance" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:gravity="right"
    android:textSize="15sp" android:paddingLeft="10px">
    </TextView>         

</LinearLayout>

main code:

ListView resultList = (ListView)findViewById(R.id.resultListView);
ItemAdapter m_adapter = new ItemAdapter(this, R.layout.list_item, itemsFound);  
resultList.setAdapter(m_adapter);

viewItems = new Runnable(){  
    @Override  
    public void run() {
        searchItems();     
    }  
}; 

Thread thread =  new Thread(null, viewItems, "Searching");  
thread.start();



private class ItemAdapter extends ArrayAdapter<Item>{

        private ArrayList<Item> items;

        public ItemAdapter(Context context, int textViewResourceId,
                ArrayList<Item> objects) {
            super(context, textViewResourceId, objects);
            this.items = objects;
        }

        public View getView(int position, View convertView, ViewGroup parent){
            View v = convertView;

            if(v == null){
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                v = vi.inflate(R.layout.list_item, null);
            }

            Item item = (Item) items.get(position);

            if (item != null){
                TextView resName = (TextView) v.findViewById(R.id.result_item_name_object);
                TextView resDistance = (TextView) v.findViewById(R.id.result_item_distance);  

                if (resName != null){
                    resName.setText(item.name);
                }

                if (resDistance != null){
                    resDistance.setText(item.distance);
                }           
            }       
            return v;           
        }           
    }

private Runnable returnRes = new Runnable() {
    @Override
    public void run() {
        if(itemsFound != null && itemsFound.size() > 0){
            m_adapter.notifyDataSetChanged();  
            int count = itemsFound.size();
            for(int i=0; i < count; i++)  
                m_adapter.add(itemsFound.get(i));  
        }
        m_ProgressDialog.dismiss();
        m_adapter.notifyDataSetChanged();  
    }
};

for the first i get "UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView" in result.xml layout window, but when i clean up textview from listview in resut.xml that problem disappear, but comes new one:

11-10 10:39:42.623: ERROR/AndroidRuntime(720): android.content.res.Resources$NotFoundException: String resource ID #0x304
11-10 10:39:42.623: ERROR/AndroidRuntime(720):     at android.content.res.Resources.getText(Resources.java:200)
/*and so on*/
Was it helpful?

Solution 2

fixed :) dont forget that (textview).settext() requires resource id, so dont forget to convert your numerical data to string :)

OTHER TIPS

Did you define your string main_no_item in strings.xml ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top