如何为此listView代码实现ImageAdapter?

时间:2011-11-21 09:46:24

标签: android android-layout android-emulator android-widget

列表视图代码如下:

public final static String ITEM_TITLE = "title";   
    public final static String ITEM_CAPTION = "caption";   
    public Resources resources;
    public static Uri path;
    public static File file;
    public static ProgressDialog  m_progressDialog;
    public static ListView list;

    @Override  
    public void onCreate(Bundle icicle) {   
        super.onCreate(icicle);  
        setContentView(R.layout.main);

        // create our list and custom adapter   
        SeparatedListAdapter adapter = new SeparatedListAdapter(this);   
        adapter.addSection("Local documents:", new ArrayAdapter<String>(this,   
            R.layout.list_item, new String[] { "WindowsONE Mobile PK", "WindowsorONE Moldings","Filet for a burger video" }));
        adapter.addSection("Non-local resources:", new ArrayAdapter<String>(this,   
                R.layout.list_item, new String[] { "Launch Photo slideshow link", "Dealer locator link" }));
        adapter.addSection("Send emails:", new ArrayAdapter<String>(this,   
                R.layout.list_item, new String[] { "Send Dealer Locator email", "Send Catalog email","Send install instrucation link" })); 
        //For extra Information in Listview    
        //adapter.addSection("Non-local resources:", new SimpleAdapter(this, security, R.layout.list_complex,   
        //new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));        
        list = getListView();
        list.setAdapter(adapter);   
        list.setTextFilterEnabled(true);
        list.setOnItemClickListener(this); 
     }

}

2 个答案:

答案 0 :(得分:1)

public class ListContacts extends ListActivity {

ListAdapter lAdapter;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


Cursor cursor = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null,
                ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null,
                "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");

        startManagingCursor(cursor);


/** start mapping */
        String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME };
        int[] names = new int[] { R.id.contact_name };
        lAdapter = new ImageCursorAdapter(this, R.layout.contact_listview,
                cursor, columns, names);
        setListAdapter(lAdapter);


}





public class ImageCursorAdapter extends SimpleCursorAdapter

{

public ImageCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);

}

* @param pos
     *            : the position in the list/cursor,
     * @param inView
     *            : The View object of the row that was last created, null if
     *            its the first row
     * @param parent
     *            : The ViewGroup object of the parent view return View :
     *            returns a View object when called
     * @see android.widget.CursorAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(int pos, View inView, ViewGroup parent) {

View v = inView;

}

}

答案 1 :(得分:1)

我需要突出显示所选列表项,然后使用:

android:listSelector="@drawable/selector_list"



<ListView android:id="@id/android:list" android:background="@android:color/transparent"
            android:cacheColorHint="@android:color/transparent"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:listSelector="@drawable/selector_list"
android:layout_weight="1"
            android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
            android:drawSelectorOnTop="false">

selector_list.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"
          android:constantSize="true">

  <item  android:state_focused="false" android:state_pressed="false">

    <shape android:shape="rectangle">   
    <solid android:color="@android:color/transparent"/>
            <stroke android:width="2dp" android:color="@android:color/transparent" />
    <padding android:left="1dp" android:top="1dp" android:right="1dp"
        android:bottom="1dp" />
    </shape>
  </item>

   <item  android:state_focused="true" android:state_pressed="false">

    <shape android:shape="rectangle">
            <solid android:color="@color/listitemfocus"/>
    <stroke android:width="4dp" android:color="@android:color/transparent" />
    <padding android:left="1dp" android:top="1dp" android:right="1dp"
        android:bottom="1dp" />
    </shape>
  </item>

    <item android:state_pressed="true">

    <shape android:shape="rectangle">
            <solid android:color="@color/listitempress"/>
    <stroke android:width="4dp" android:color="@android:color/transparent" />
    <padding android:left="1dp" android:top="1dp" android:right="1dp"
        android:bottom="1dp" />
    </shape>
  </item>

</selector>