Android将Imageview src动态地设置为listview

时间:2015-01-17 18:19:07

标签: android mysql listview android-listview

我正在开发一个android项目,我应该从mysql数据库中列出数据,并在ListView内设置图像的src,图像中的名称取自drawable资源。以下是设计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);
        }
    });

}

1 个答案:

答案 0 :(得分:0)

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

这是例子:

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

相关问题