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

前端 未结 3 799
天命终不由人
天命终不由人 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)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView android:id="@android:id/list" android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <TextView android:id="@android:id/empty" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Empty set" />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-13 20:46

    make your Activity extends ListActivity as well.

    Then this should help you get started.

    Object[] sArray = {"This", "is", 3.5, true, 2, "for", "bla"};
    ArrayAdapter adp = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sArray);
    setListAdapter(adp);
    

    The second parameters can be change to your preferred layout. See API document for further information.

    0 讨论(0)
  • 2020-12-13 20:46

    You can also attach an AlertDialog to the ListView when user clicks on a ListItem. I am including a working code below in case anyone is trying the same thing,

    final ListView v = getListView();
    v.setOnItemClickListener(new OnItemClickListener() {
    
    public void onItemClick(final AdapterView<?> parentView, View view, int position,
            long id) {
        final String fileName = (String) parentView.getItemAtPosition(position);
        new AlertDialog.Builder(parentView.getContext()).setTitle("Upload").setMessage("Upload " + fileName +"?")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface di, int arg1) {
                    //do something here...
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface di, int arg1) {
                    di.dismiss();                           
                }
            }).show();
    }   
    });
    

    The key is that you need to pass your enclosing activity's context to the dialog. I found that out when getApplicationContext() failed with a strange error message.

    0 讨论(0)
提交回复
热议问题