无法弄清楚如何动态更改自定义列表视图

时间:2016-02-17 09:31:15

标签: android listview

我有自定义列表视图。当调用listview时我希望它从我的数据库加载所有配置文件,这是有效的。但我也想检查配置文件的状态,并根据结果我想在它旁边显示一个特定的图标。但我似乎无法弄清楚如何正确完成这一点..这是我到目前为止所做的:

profiles_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:layout_marginBottom="10dp"
        android:layout_above="@+id/menuBtn"
        android:background="#2cffffff">
    </ListView>
<RelativeLayout/>

list_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TableRow>
        <ImageView
            android:id="@+id/statusIcon"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <TextView
            android:id="@+id/profile"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"/>

    </TableRow>
</TableLayout>

MainActivity.class

    public class ProfilesList extends CredenceOneActivity implements AdapterView.OnItemClickListener {

            static final int STATUS_SUCCES =1;
            static final int STATUS_WARNING =2;
            static final int STATUS_ERROR =3;

            private ListView list;
            private ArrayAdapter<String> adapter;
            private ArrayList<String> arrayList;

            private DB_Handler db;

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

                if (savedInstanceState == null) { 
                    setContentView(R.layout.profiles_list);
                }
                list = (ListView) findViewById(R.id.listView);
                arrayList = new ArrayList<String>();
                try {
                    db = new DB_Handler(this);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }  


            adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
            // Here, you set the data in your ListView
            list.setAdapter(adapter);
            list.setOnItemClickListener(this);

            loadRegisteredProfiles();
       }

    public void loadRegisteredProfiles() {
        List<DB_Profiles> storedProfiles = db.getAllProfiles();
        for (DB_Profiles profile : storedProfiles) {
            String name= profile.getNAME();
            arrayList.add(name);
            adapter.notifyDataSetChanged();

}

我想为列表视图中加载的每个配置文件执行类似的操作:

DB_Profiles profile;
if (profile.getStatus==STATUS_SUCCES{
statusIcon.setImageResource(R.drawable.succes);}
else{profile.getStatus==STATUS_ERROR{
statusIcon.setImageResource(R.drawable.error);}}
        }

任何建议都将不胜感激!

2 个答案:

答案 0 :(得分:2)

在自定义适配器的getView()中编写下面的代码。

   ViewHolder holder;
    if (convertView == null) {

            convertView = mInflater.inflate(R.layout.rowLayout, null);

        holder = new ViewHolder();
        holder.imagView = (ImageView) convertView.findViewById(R.id.img);


        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (profile.getStatus==STATUS_SUCCES)
        {
                 holder.imagView.setImageResource(R.drawable.succes);
        }
    else if(profile.getStatus==STATUS_ERROR)
        {
                holder.imagView.setImageResource(R.drawable.error);
         }

    return convertView;

并在自定义适配器中声明静态内部类,如此

       static class ViewHolder {
            ImageView img;

}

答案 1 :(得分:0)

在listview适配器的 getView()中,您可以在list_item布局中使用 imageView setImageResource()