android gridview,带有来自webservice的数据的适配器类

时间:2012-06-12 21:50:09

标签: android arrays web-services gridview adapter

这是我的gridview.java类

public class TextGridActivity extends Activity {

String[] _cataId=new String[100] ;
String[] _cataName=new String[100];
  String[] _cataDes=new String[100];
  String[] _cataCode=new String[100];
      int num,k=0;
         int len=10;
      String[] temp ;
DataAdapter mAdapter;

GridView gridView;
private static String SOAP_ACTION = "http://tempuri.org/GetItemCategory";

 private static String NAMESPACE = "http://tempuri.org/";

     String METHOD_NAME = "GetItemCategory";



 String res="";
  private static String URL = "http://10.0.2.2:63395/Service1.asmx";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

fillTitle();

    createViews();
}

    public void createViews()
      { 

         mAdapter = new DataAdapter(this,_cataId,_cataName,_cataDes,_cataCode,len);
      GridView gridview = (GridView) findViewById(R.id.gridview);  

    gridview.setAdapter(mAdapter);}
//method for soap call
       public void fillTitle(){
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {

     androidHttpTransport.call(SOAP_ACTION, envelope);
     KvmSerializable response = (KvmSerializable)envelope.bodyIn;

    res= response.getProperty(0).toString();
    Toast.makeText(this, res,Toast.LENGTH_LONG).show();

    String delimiter = ";";
      temp= res.split(delimiter);


    for(int i=0;i<len; i++)
    {
        _cataId[i]=temp[i].substring(29);
    //Toast.makeText(this, _cataId[i],Toast.LENGTH_LONG).show();

        _cataName[i]=temp[i+1].substring(9);
        //Toast.makeText(this, _cataName[i],Toast.LENGTH_LONG).show();

        _cataDes[i]=temp[i+2].substring(9);
        //Toast.makeText(this, _cataDes[i],Toast.LENGTH_LONG).show();

        _cataCode[i]=temp[i+3].substring(10);
        //Toast.makeText(this, _cataCode[i],Toast.LENGTH_LONG).show();

     i=i+4;



    }


} 
    catch (Exception e) {

    Toast.makeText(this, e.toString(),
    Toast.LENGTH_LONG).show();
    } }
    }

这是我的DataAdapter.java类

public class DataAdapter extends BaseAdapter
{                  
       Context mContext;

      private String[] _cataId ;
     private  String[] _cataName;
      private String[] _cataDes;
      private String[] _cataCode;
       int len;

     public DataAdapter(Context mContext, String [] cataId, String [] cataName, String[] cataDes, String[] cataCode,int len) {

         super();
          this.mContext=mContext;
         this._cataId=cataId;
         this._cataName = cataName;
         this._cataDes = cataDes;
         this._cataCode = cataCode;
           this.len = len;
         }

       private LayoutInflater mInflater;
       public DataAdapter(Context c)
       {
              mContext=c;
              mInflater = LayoutInflater.from(c);
       }
       public int getCount()
       {
              return len;
       }


       public Object getItem(int position)
       {


           return  position;

       }


    public long getItemId(int position)
       {
              return position;
       }


       public View getView(int position, View convertView, ViewGroup parent)
       {
              ViewHolder holder=null;
              if(convertView==null)
              {
                     convertView = mInflater.inflate(R.layout.customgrid, null);
                     holder = new ViewHolder();
                     holder.txtId=(TextView)convertView.findViewById(R.id.txtId);
                    // holder.txtId.setPadding(30, 10,10 , 10);
                     holder.txtName=(TextView)convertView.findViewById(R.id.txtName);
                    //holder.txtName.setPadding(30, 10, 10, 10);
                     holder.txtDes=(TextView)convertView.findViewById(R.id.txtDes);
                   // holder.txtDes.setPadding(30, 10, 10, 10);
                     holder.txtCode=(TextView)convertView.findViewById(R.id.txtCode);
                   // holder.txtCode.setPadding(30, 10, 10, 10);
                     if(position==0)
                     {                             
                           convertView.setTag(holder);
                     }
              }
              else
              {
                     holder = (ViewHolder) convertView.getTag();
              }
              holder.txtId.setText(_cataId[position]);
              holder.txtName.setText(_cataName[position]);
              holder.txtDes.setText(_cataDes[position]);
              holder.txtCode.setText(_cataCode[position]);

              return convertView;
       }
       static class ViewHolder
       {        

            TextView txtId;        
              TextView txtName; 
              TextView txtDes;
            TextView txtCode;
       }
}

数据来自res变量。它由(;)分隔。我已经烘烤了所有的数据及其进入,_cataId,_cataName,_cataDes,_cataCode,并且为了确保它的到来而敬酒。

现在我的问题是我在gempeting force关闭CreateViews()方法..在使用n grid的madapter被clledd。请告诉我在调用此方法时我做错了什么

1 个答案:

答案 0 :(得分:0)

由于通信在UI线程上,因此得到ANR。从android 4开始。

对于所有长(或未知时间)操作,在非线程的线程上使用它们,并仅在需要时更新UI线程。

阅读本文以获取更多信息:

http://developer.android.com/resources/articles/painless-threading.html

为了避免它,你可以使用严格模式:

http://developer.android.com/reference/android/os/StrictMode.html