【问题标题】:How to Implement the ImageAdapter for this listView Code?如何为此 listView 代码实现 ImageAdapter?
【发布时间】:2012-01-02 20:05:30
【问题描述】:

列表视图的代码如下:

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

}

【问题讨论】:

  • 表示你要实现带图片的ListView?
  • 不,不是图片。但是我想为此列表视图设置 ImageAdapter 因为在我的代码中,当我要选择任何列表视图时,它不会显示列表项已被选中。我的意思是没有显示在该特定索引的背景上选择了橙色..
  • 如果你得到了答案,请将正确的标记为接受
  • @manu:我仍然没有得到我想要的任何合适的解决方案。

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


【解决方案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;

}

}

【讨论】:

    【解决方案2】:

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

    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>
    

    【讨论】:

    • 我已经按照你说的设置了布局。但是当我要选择任何 ListView 索引时仍然没有看到任何颜色。
    • 你在 selector.xml 中指定了颜色吗 android:color="@color/listitempress"/
    • 你可以在模拟器中检查它并滚动它..我的意思是使用鼠标滚动列表视图..上/下
    • 是的,我已将其指定为 android:color="#000000"
    猜你喜欢
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多