0

有人可以解释一下我做错了什么/我必须做什么?

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

谢谢

这是给定活动的代码: public class DisponiveisActivity extends 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);


            }
        });
    }



    }

}

4

1 回答 1

0

我刚刚在谷歌上找到的一个很好的例子:在这种情况下,你只有一张图片,我真的希望你能理解如何为连续 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());
}
}
于 2012-06-12T21:32:03.510 回答