在Android列表视图中没有调用GetView方法

时间:2015-04-19 20:00:54

标签: android image android-listview android-custom-view getview

我是Android新手 我想使用包含图像和文本的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);

    }

}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您不需要循环populateListView()。循环完成后。致电populateListView()。更改您的代码:

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();
    }