【问题标题】:GetView method is not getting called in android list viewGetView 方法未在 android 列表视图中调用
【发布时间】:2015-04-19 20:00:53
【问题描述】:

我是 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);

    }

}

【问题讨论】:

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


    【解决方案1】:

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

     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

    【讨论】:

    • 嗨,我不明白你使用的for循环。你能解释一下代码
    • 我正在使用循环来添加项目文本和图片。您需要添加适配器内容。
    【解决方案2】:

    你不需要populateListView()in 循环。循环完成后。致电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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多