我在从mySQL检索数据后尝试设置TextView
的文本时,我的适配器遇到问题,
Doinbackground()
+ jsonParsing成功
当它进入onPostexecute
时,我会尝试将价格文本设置为“任何事物”,并在那里显示null pointer exception
。
如果我评论price.setText("anything");
应用程序将正常运行,我从URL获取2个json对象实例并在我的listview
中创建两个对象,但当然没有检索到的数据。
所有TextView
s setText()
也会发生这种情况。
代码在这里:
public class Search extends Activity {
public static String IP=Mysynch.IP;
static String url="http://"+IP+":80/senior/getStoreItemtoJson.php";
String Scatid;
String Sitemid;
String Suserid;
String Sdescription;
String Sprice;
String Squantity;
String Showmanyorders;
String Sprocessingtime;
String Sdeliverytime;
String Sdateadded;
String Sbrand;
String Ssale;
String Sbrandnew;
String Ssecondhand;
Activity activity;
List<Itemclass> list=new ArrayList<Itemclass>();
TextView price;
TextView desc;
TextView quantity;
ListView listview;
ViewGroup viewgroup;
Itemclass itemclass;Itemclass currentitem ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println(url);
setContentView(R.layout.search);
(new myAsynctask()).execute();
System.out.println("Create");
}
public class myAsynctask extends AsyncTask<String, Void, Boolean>
{
protected void onPreExecute()
{
System.out.println("pre");
}
protected Boolean doInBackground(final String... args) {
System.out.println("Enter background");
//this client handles the download and upload of the info
DefaultHttpClient httpclient=new DefaultHttpClient(new BasicHttpParams());
// Here we pass the url
HttpPost httppost=new HttpPost(url);
httppost.setHeader("Content-type","application/json");
InputStream inputstream=null;
String result=null;
try{
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
inputstream=entity.getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputstream,"UTF- 8"),8);
StringBuilder Sbuilder=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null){
Sbuilder.append(line+'\n');
}
result=Sbuilder.toString();
}
catch(Exception e){
e.printStackTrace();
}finally{
try{if(inputstream !=null )
inputstream.close();
}
catch(Exception e){
e.printStackTrace();
}
};
System.out.println("finish conx + json");
JSONArray jsonArray;
try{
//jibit awal json object ili howi b2albo kil shi
JSONObject jsonobj=new JSONObject(result);
// String s= jsonobj.getString("success");
//System.out.println(s);
//String e= jsonobj.getString("error");
//System.out.println(e);
jsonArray=jsonobj.getJSONArray("item");
//how to get all objects into a json array
// query array fiya kil el names ta3on el quote object eli jibti fo2
//create a list
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonline=jsonArray.getJSONObject(i);
Scatid=jsonline.getString("catid");
Sitemid=jsonline.getString("itemid");
Suserid=jsonline.getString("userid");
Sdescription=jsonline.getString("description");
Sprice=jsonline.getString("price");
Squantity=jsonline.getString("quantity");
Showmanyorders=jsonline.getString("howmanyorders");
Sprocessingtime=jsonline.getString("processingtime");
Sdeliverytime=jsonline.getString("deliverytime");
Sdateadded=jsonline.getString("dateadded");
Sbrand=jsonline.getString("brand");
Ssale=jsonline.getString("sale");
Sbrandnew=jsonline.getString("brandnew");
Ssecondhand=jsonline.getString("secondhand");
Itemclass item=new Itemclass(Scatid, Sitemid, Suserid, Sdescription, Sprice, Squantity, Showmanyorders,
Sprocessingtime, Sdeliverytime, Sdateadded, Sbrand, Ssale, Sbrandnew, Ssecondhand);
list.add(item);
System.out.println("*******"+Sprice);
}
}catch(JSONException e ){
e.printStackTrace();
}
System.out.println("*****************************************");
return true;
}
protected void onPostExecute(final Boolean success)
{
System.out.print("enter post");
if(success){
System.out.print(" enter initialize list");
listview=(ListView)findViewById(R.id.listView1);
ArrayAdapter<Itemclass> adapter = new MyListAdapter();
listview.setAdapter(adapter);
System.out.print("initialize list"); }
}
}
private class MyListAdapter extends ArrayAdapter<Itemclass> {
public MyListAdapter() {
super(Search.this, R.layout.searchlistitems, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView =getLayoutInflater().inflate(R.layout.searchlistitems,
parent, false);
}
currentitem = list.get(position);
price=(TextView)findViewById(R.id.tvSearchprice);
desc=(TextView)findViewById(R.id.tvsearchDescr);
quantity=(TextView)findViewById(R.id.tvSearchquantity);
System.out.println("position===>>>>"+position);
// price.setText("any thing");//<======== my problem is here
// Fill the view
//ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
//imageView.setImageResource(currentCar.getIconID());
//desc.setText(currentitem.description);
// quantity.setText(currentitem.quantity);
System.out.println("desc===>>>>"+currentitem.description);
return itemView;
}
}
}
答案 0 :(得分:1)
当你用getView
方法夸大布局时:
itemView = getLayoutInflater().inflate(R.layout.searchlistitems, parent, false);
您使用此膨胀视图检索您的子视图,如下所示:
price = (TextView) itemView.findViewById(R.id.tvSearchprice); // use itemView
desc = (TextView) itemView.findViewById(R.id.tvsearchDescr);
quantity = (TextView) itemView.findViewById(R.id.tvSearchquantity);
但是,我建议您使用ViewHolder
模式为您的适配器提供更好的性能。这是一个很棒的主题:Performance Tips for Android’s ListView和一个简单的教程:Android ViewHolder Pattern Example。它将避免在ListView滚动期间频繁调用findViewById()
。