Android动态设置Imageview src为listview

标签 android mysql listview android-listview

我正在开发一个android项目,我应该列出来自mysql数据库的数据,并在ListView内部设置图像的src,图像的名称取自可绘制资源。这是设计ListView的代码,数据库中的TAG_DRAP显示如下@drawable/equip1:

protected void onPostExecute(String file_url) {

// dismiss the dialog after getting all products
    pDialog.dismiss();
    // updating UI from Background Thread
    runOnUiThread(new Runnable() {
        public void run() {
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    AllProductsActivity.this, productsList,
                    R.layout.list_item_equipes, new String[] { TAG_PID,
                            TAG_NAME,TAG_DRAP,TAG_DRAP},
                    new int[] { R.id.pid, R.id.name,R.id.surname,R.id.icon });


            //getResources().getIdentifier("TAG_DRAP", "drawable", context.getPackageName())
            // updating listview

            setListAdapter(adapter);
        }
    });

}

最佳答案

您只需在项目中创建并使用自定义列表适配器。 在自定义列表适配器中,您可以从每个列表项动态获取 ImageView 并设置您想要的内容。

这里是示例:

public class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int resource, List<Item> items) {
     super(context, resource, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;

if (v == null) {

    LayoutInflater vi;
    vi = LayoutInflater.from(getContext());
    v = vi.inflate(R.layout.itemlistrow, null);

}

Item p = getItem(position);

if (p != null) {

    TextView tt = (TextView) v.findViewById(R.id.id);
    TextView tt1 = (TextView) v.findViewById(R.id.categoryId);
    TextView tt3 = (TextView) v.findViewById(R.id.description);

    if (tt != null) {
        tt.setText(p.getId());
    }
    if (tt1 != null) {

        tt1.setText(p.getCategory().getId());
    }
    if (tt3 != null) {

        tt3.setText(p.getDescription());
    }
}

return v;

}

这是链接:list-adapter custom

关于Android动态设置Imageview src为listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002711/

相关文章:

java - VoIP RTP 流从/到服务器(在 Java 中)到/从 android

android - 更改 Gridview 的图像

android - 编写连接到多个社交网站的移动应用程序

java - NoClassDefFoundError when running Facebook 官方例子

PHP htmlentities 或 htmlspecialchars = 重音错误

Mysql SELECT 行不在 AND 组中

java - 在 Android ListView 中显示空行

c# - WPF如何更改gridview的标题颜色

php - 将字段数据与另一个表进行比较

java - ListView 未按预期生成