android gridview和来自webservice数据的适配器类
这是我的gridview.java类android gridview和来自webservice数据的适配器类,android,arrays,web-services,gridview,adapter,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
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();
} }
}
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变量中,并由;分隔。我已经为所有的数据和它的进入干杯,卡塔德,卡塔德,卡塔德,卡塔编码,并为确保它的到来而干杯
现在我的问题是强制关闭CreateViews方法。。在那个模型中,n网格是clledd。请告诉我调用此方法时我做错了什么,因为通信是在UI线程上进行的。从安卓4开始
对于所有长时间或未知时间操作,请在非UI线程的线程上使用它们,并仅在需要时更新UI线程
有关更多信息,请阅读:
为了避免这种情况,您可以使用严格模式:
ts不符合ANR。此外,错误出现在CREATEVIEWS方法中…….当我调用此方法时,它会出现..否则我可以成功toast数据,但无法将数据嵌入gridview..请响应其紧急请求您有哪个android版本?我认为在新版本中,甚至不允许通过UI线程进行通信。另外,在CreateView中,您有一个名为fillTitle的函数,它似乎是进行通信的函数,它位于UI线程中。我说的对吗?我在ui线程中用相同代码的其他模块进行了web通信,它们都很好地工作。这是两个独立的函数filltitle n createview,如果我对mAdapter做了一些错误,可以在onCreate中调用我的适配器调用吗