在自定义列表视图中动态设置图像

时间:2012-06-12 21:23:20

标签: android android-listview android-adapter

有人可以解释一下我做错了什么/我该怎么办?

我有一个listview,它从json数组中获取信息。但它是一个自定义列表视图,每行有5个图像和一些文本。根据启用/禁用,每个图像具有/ drawable / image_selector,具有不同的图像。我可以使用hashmap和ListAdapter动态设置每一行的文本。但我试图对图像做同样的事情,我无法做到这一点。正在使用自定义适配器的正确选择?如果是这样怎么样?我没有从例子中了解如何使用它们。

由于

这是给定活动的代码:     公共类DisponiveisActivity扩展ListActivity { ;     // para os icones做布局

private ProgressDialog pDialog;

//Object of class JSON Parser
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> disponiveislist;


public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);


    disponiveislist = new ArrayList<HashMap<String, String>>();
    new Loaddisp().execute();

}
//Background ASYNC task to load

class Loaddisp extends AsyncTask<String, String, String>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(DisponiveisActivity.this);
        pDialog.setMessage("Carregando");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }



    protected String doInBackground(String... args) {


                String nome = l.getString("NOME");
                String preco = l.getString("PRECO");
                String cfotografia = l.getString("COMFOTOGRAFIA");
                String cdados = l.getString("COMDADOS");
                String copiniao = l.getString("COMOPINIAO");

                String cdeslocacao = l.getString("COMDESLOCACAO");



                String distancia = l.getString("DISTANCIA");

                //create new HASHMAP                    
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("NOME", nome);  
                map.put("PRECO", preco);
                map.put("DISTANCIA", distancia);
                map.put("COMOPINIAO", copiniao);
                map.put("COMDADOS", cdados);
                map.put("COMFOTOGRAFIA", cfotografia);
                //------------------------------


                disponiveislist.add(map);

            }




    @Override
    protected void onPostExecute(String file_url) {
        // TODO Auto-generated method stub

        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */



                    Log.d("PP",disponiveislist.toString());
                ListAdapter adapter = new SimpleAdapter(DisponiveisActivity.this, disponiveislist, R.layout.listrow,new String[]{"NOME","PRECO","DISTANCIA"}, new int[]{R.id.mission_name, R.id.preco, R.id.cdistancia});




                setListAdapter(adapter);


            }
        });
    }



    }

}

1 个答案:

答案 0 :(得分:0)

我刚在Google上找到的很好的例子: 在这种情况下,你只有一个图像,我真的希望你能理解如何连续复制5张图片的代码。

public class ItemsList extends ListActivity {

private ItemsAdapter adapter;

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

        setContentView(R.layout.items_list);

        this.adapter = new ItemsAdapter(this, R.layout.items_list_item, ItemManager.getLoadedItems());
        setListAdapter(this.adapter);
}

private class ItemsAdapter extends ArrayAdapter<Item> {

        private Item[] items;

        public ItemsAdapter(Context context, int textViewResourceId, Item[] items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.items_list_item, null);
                }

                Item it = items[position];
                if (it != null) {
                        ImageView iv = (ImageView) v.findViewById(R.id.list_item_image);
                        if (iv != null) {
                                iv.setImageDrawable(it.getImage());
                        }
                }

                return v;
        }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
        this.adapter.getItem(position).click(this.getApplicationContext());
}
}