Is there a super simple List / ListAdapter example out there for android

前端 未结 3 801
天命终不由人
天命终不由人 2020-12-13 20:24

I have a web service that returns a super simple list of objects

MyObject[] data = webServiceCall();

MyObject has 1 field i want to display, \"Name\"

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 20:38

    so i think i figured it out with a little inspiration from RobGThai's answer, i'm posting the code for anyone else to see, i guess i didn't really use a custom adapter

    This is the super simple example that got me started, so once i had this, I made sure my "MyObject" had a toString() method on it to show properly in the list and i passed the MyObject[] array into the "new ArrayAdapter" constructor instead of listItems

    FooList.java

    import android.app.ListActivity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    
    public class FooList extends ListActivity {
        String[] listItems = {"item 1", "item 2 ", "list", "android", "item 3", "foobar", "bar", }; 
        @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.temp);
             setListAdapter(new ArrayAdapter(this,  android.R.layout.simple_list_item_1, listItems));
         }
    
    }
    

    the layout xml i used (temp.xml)

    
    
        
        
    
    

提交回复
热议问题