从webservice填充列表视图android

时间:2011-11-08 08:41:38

标签: android web-services android-listview listview-adapter

我的代码中出现错误,而我尝试使用json显示我从服务器收到的与wcf webservices通信的数据列表。

我收到来自webservice的回复,数据也被添加到列表中,但我不知道为什么我收到此错误。请指导我解决错误

这是logcat输出:

11-08 13:57:29.668: ERROR/AndroidRuntime(1386): Uncaught handler: thread main exiting due to uncaught exception

11-08 13:57:29.678: ERROR/AndroidRuntime(1386): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.AdapterView.addView(AdapterView.java:461)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.LayoutInflater.inflate(LayoutInflater.java:415)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at com.ecommerce.balajeebazaar.CategoryListAdapter.getView(CategoryListAdapter.java:64)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.AbsListView.obtainView(AbsListView.java:1273)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     
                             at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.ListView.onMeasure(ListView.java:1060)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.View.measure(View.java:7703)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.View.measure(View.java:7703)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     
                             at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.View.measure(View.java:7703)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.View.measure(View.java:7703)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2989)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at android.view.View.measure(View.java:7703)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):    
                             at android.view.ViewRoot.performTraversals(ViewRoot.java:747)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     
                             at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.os.Looper.loop(Looper.java:123)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386): at android.app.ActivityThread.main(ActivityThread.java:4203)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):  at java.lang.reflect.Method.invokeNative(Native Method)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):  at java.lang.reflect.Method.invoke(Method.java:521)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)

11-08 13:57:29.678: ERROR/AndroidRuntime(1386):     at dalvik.system.NativeStart.main(Native Method)

我的java文件的代码是:

CategoryArray = CW.CategoryWebservice("http://www.balajeebazaar.com/RestServiceImpl.svc/categorydetails");

for(int i = 0;i<=CategoryArray.length()-1;i++){
            try {
                Log.v("Categories :- ", CategoryArray.getString(i)+"");
                String[] val = new String[3];
                val = CategoryArray.getString(i).split(",");
                Log.v("val[0] :  ", val[0]);
                Log.v("val[1]  :", val[1]);
                CategoryList.add(i, val[0]+" "+val[1]);
                val = null;
                Log.v("CATEGORY LIST", CategoryList.get(i).toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //CategoryList.size();
        adapter = new CategoryListAdapter(CategoryActivity.this, CategoryList);

我的CategoryListAdapter类的代码是:

public class CategoryListAdapter extends BaseAdapter {
    private Activity activity;
    // private String[] data;
    private ArrayList<String> CategoryList = new ArrayList<String>();
    private static LayoutInflater inflater = null;

    // public ImageLoader imageLoader;

    public CategoryListAdapter(Activity a, ArrayList<String> categorylist) {
        activity = a;
        // data = d;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        CategoryList = categorylist;
        // imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return CategoryList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public static class ViewHolder {
        public TextView text;
        public ImageView image;
    }

代码编辑:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi;
        ViewHolder holder;
        if (convertView == null) {
            vi = new View(activity);
            LayoutInflater inflater = activity.getLayoutInflater();
            vi = inflater.inflate(R.layout.item, null, true);
            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.text);
            holder.image = (ImageView) vi.findViewById(R.id.image);
            vi.setTag(holder);
        } else {
            vi = convertView;
            holder = (ViewHolder) vi.getTag();
            holder.text.setText(CategoryList.get(position).toString());
            holder.image.setTag(R.drawable.go_arrow);
        }
        holder.text.setText(CategoryList.get(position).toString());
        holder.image.setTag(R.drawable.go_arrow);
        vi.setTag(holder);
        return vi;
    }
}

我将getView函数编辑为ablove(参见我的代码编辑) 并且它对每个人的回复起作用

2 个答案:

答案 0 :(得分:0)

尝试以下代码

runOnUiThread(new Runnable()
{           
    @Override
    public void run()
    {
        adapter = new CategoryListAdapter(CategoryActivity.this, CategoryList);

    }
});

答案 1 :(得分:0)

尝试使用此CategoryListAdapter替换你的,并让我知道发生了什么,

修改

   public class CategoryListAdapter extends BaseAdapter {
    private Activity activity;
    // private String[] data;
    private ArrayList<String> CategoryList = new ArrayList<String>();

    public CategoryListAdapter(Activity a, ArrayList<String> categorylist) {
        activity = a;
        CategoryList = categorylist;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return CategoryList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public static class ViewHolder {
        public TextView text;
        public ImageView image;
    }

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi;
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater =getLayoutInflater();
            vi = inflater.inflate(R.layout.item, parent);
            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.text);
            holder.image = (ImageView) vi.findViewById(R.id.image);
            vi.setTag(holder);
        } else{
                  vi = convertView;
                   holder = (ViewHolder) vi.getTag();
                  }
        String texttoappend = CategoryList.get(position);
        Log.v("texttoappend", texttoappend);
        holder.text.setText(CategoryList.get(position).toString());
        holder.image.setTag(R.drawable.go_arrow);
        // imageLoader.DisplayImage(data[position], activity, holder.image);
        return vi;
    }
}