0

我是安卓新手。
我想使用同时包含图像和文本的 ListView。在调试时未调用
该方法。getView()

我需要一个解决方案。
我正在尝试将数据绑定到 ListView 项目。

这是我的代码:

public class ResultActivity extends ActionBarActivity {
    public List<String> itemList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        JSONObject obj = null;
        try {
            obj = new JSONObject(getIntent().getStringExtra("json"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            parseJsonData(obj);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }
    public void parseJsonData(JSONObject obj) throws JSONException {

        try {

            int j=0;
            String ack = (String) obj.get("ack");
            int itemCount =obj.getInt("itemCount");
            String ItemPrice="";

            for(j=0;j<itemCount;j++) {
                itemList = new ArrayList<String>();
                String itemObject = "item"+1;

                JSONObject itemObj = obj.getJSONObject(itemObject);

                JSONObject BasicInfoObj =itemObj.getJSONObject("basicInfo");
                JSONObject sellerInfoObj =itemObj.getJSONObject("sellerInfo");
                JSONObject shippingInfoObj = itemObj.getJSONObject("shippingInfo");
                String galleryURL = BasicInfoObj.getString("galleryURL");
                String pictureURLSuperSize =BasicInfoObj.getString("pictureURLSuperSize");
                int CurrentPrice=BasicInfoObj.getInt("convertedCurrentPrice");
                int shippingServiceCost =BasicInfoObj.getInt("shippingServiceCost");
                String title =BasicInfoObj.getString("title");

                if(CurrentPrice == 0)
                {
                     ItemPrice ="Price:"+" "+"$"+shippingServiceCost+" "+"(FREE Shipping)";
                }
                else
                {
                     ItemPrice = "Price:"+" "+"$"+shippingServiceCost+" "+ "(+$"+ CurrentPrice+ "for Shipping)";
                }
                itemList.add(0,galleryURL);
                itemList.add(1,pictureURLSuperSize);
                itemList.add(2,title);
                itemList.add(3,ItemPrice);
                populateListView();

            }
            }

        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void populateListView()
    {
        ArrayAdapter<String> adapter =new MyListAdapter();
        ListView list =(ListView) findViewById(R.id.ItemListView);
        list.setAdapter(adapter);
        adapter.getCount();

    }
    private class MyListAdapter extends ArrayAdapter<String> {
        public MyListAdapter()
        {
            super(ResultActivity.this,R.layout.item_view,itemList);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView = convertView;
            if(itemView==null) {
                itemView =getLayoutInflater().inflate(R.layout.item_view,parent,false);
            }
            String itemtowork =itemList.get(position);
            ImageView imageView =(ImageView)itemView.findViewById(R.id.item_icon);
            //String ImageUrl=itemtowork.;
            //imageView.setImageURI();

            return itemView;
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);

    }

}
4

2 回答 2

0

我喜欢这种方式,而且效果很好。更改您的代码:

 private void populateListView()
            {
                ArrayAdapter< Item > adapter =new MyListAdapter();
                ListView list =(ListView) findViewById(R.id.ItemListView);
                for(...){
                     adapter.add(new Item("item 1", R.drawable.ic_menu));
                }
                adapter.getCount();
                list.setAdapter(adapter);


            }

            //Object picture and text
            private class Item{
              private int picture;
              private String title;

              public Item(int picture, String title){
                 this.picture = picture;
                 this.title = title;
              }
            }

            //Custom array adapter
            private class MyListAdapter extends ArrayAdapter< Item > {
                public MyListAdapter()
                {
                    super(ResultActivity.this,R.layout.item_view,itemList);
                }
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View itemView = convertView;
                    if(itemView==null) {
                        itemView =getLayoutInflater().inflate(R.layout.item_view,parent,false);
                    }

                    //TextView in row
                    TextView title = (TextView)....
                    //set text in your textview by position
                    title.setText(getItem(position).title);
                    //ImageView in row
                    ImageView imageView =(ImageView)itemView.findViewById(R.id.item_icon);
                   //set image  
                   imageView.setImageDrawable(getResources().getDrawable(getItem(position).picture))

                    return itemView;
                }


            }

在此处查看更多信息:http: //developer.android.com/reference/android/widget/ArrayAdapter.html

于 2015-04-19T20:29:50.927 回答
0

You do not need to populateListView()in loop. After loop done. call populateListView(). Change your code:

try {

        int j=0;
        String ack = (String) obj.get("ack");
        int itemCount =obj.getInt("itemCount");
        String ItemPrice="";
        itemList = new ArrayList<String>(); //
        for(j=0;j<itemCount;j++) {
            //itemList = new ArrayList<String>();//remove it
            //.....
            itemList.add(0,galleryURL);
            itemList.add(1,pictureURLSuperSize);
            itemList.add(2,title);
            itemList.add(3,ItemPrice);
            //populateListView(); //no need here

        }
        populateListView(); //do it here
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
于 2015-04-20T04:49:08.617 回答