0

我想创建一个列表视图,在 onCreate() 方法中初始化时立即将图像添加到某些字段,我不知道该怎么做。

这是我的 onCreate() 方法的必要部分:

((ListView) findViewById(android.R.id.list)).setClickable(true);
    for(int i=0; i<num_enter; i++){
    adapter=new SimpleAdapter(this, listItems, R.layout.custom_row_view,new String[]{"name", "current", "image"},  new int[] {R.id.text1, R.id.text2, R.id.lockImage});
    setListAdapter(adapter);
    HashMap<String,String> temp = new HashMap<String,String>();
    temp.put("name", name[i]);

    SetSql getport = new SetSql(this);
    getport.open();
    int port = getport.getSingleProtect(i);
    getport.close();
    if(port==1){
        //THIS IS WHERE I WANT TO SET THE IMAGE

        temp.put("current", Integer.toString(current[i]));
    }else{
    temp.put("current", Integer.toString(current[i]));
    }
    listItems.add(temp);
    adapter.notifyDataSetChanged();
    }
    }else{
        ((ListView) findViewById(android.R.id.list)).setClickable(false);
        adapter=new SimpleAdapter(this, listItems, R.layout.custom_row_view,new String[]{"name", "current"},  new int[] {R.id.text1, R.id.text2});
        setListAdapter(adapter);
        HashMap<String,String> temp = new HashMap<String,String>();
        temp.put("name", "You have no enteries yet!");
        temp.put("current", "");
        listItems.add(temp);
        adapter.notifyDataSetChanged();
    }

这是我的custome_row_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ImageView 
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/lockImage"
/>

<TextView android:id="@+id/text1" 
android:textSize="25dp"  
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

<TextView android:id="@+id/text2"
android:textSize="25dp" 
android:layout_marginTop="5dp"
android:layout_width="wrap_content" 
android:layout_height="fill_parent"
android:layout_alignParentRight="true"/>

</RelativeLayout> 

谢谢你的帮助!!!

4

1 回答 1

0

我认为您应该在要添加图像的记录中创建新的 xml 文件。你的listView喜欢category ListView。我对你有一个理想

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
     convertView = null;
    holder = new ViewHolder();
    int port = getport.getSingleProtect(position);
    if(port==1){
    //INFLATE YOUR IMAGE
        convertView = inflater.inflate(INFLATE_YOUR_IMAGES, null);
        convertView.setTag(holder);
        .......................




    }else {
        .................
        //INFLATE AS NORMAL
    }


    return convertView;
}
于 2012-06-09T10:09:41.027 回答