问题描述
- 安卓中的问题,大家帮忙看看
-
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.util.Log;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;public class LieBiao extends Activity {
ListView listView;
String [] titles={"汽车1","汽车2","汽车3","汽车4"};
String [] texts={"详细信息A","详细信息B","详细信息C","详细信息D"};
String [] mm={"1","2","2","2"};// String [] titles2;
// String [] texts2;
// String [] mm2;int [] resIds={R.drawable.mycar,R.drawable.mycar,R.drawable.mycar,R.drawable.mycar}; public String ServerUrl = "http://192.168.11.245:1234/sss/Remote/"; public ProgressDialog progressDialog1; private SharedPreferences preferences; private Editor editor; private String loginName;
// public int test ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//防止线程阻塞
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());
progressDialog1 = ProgressDialog.show(LieBiao.this, "", "服务器连接中...", true, false); new Thread() { @Override public void run() { // 向handler发消息 handler.sendEmptyMessage(0); } }.start();
// if (test == 1) {
// this.setTitle("您的汽车信息如下");
// //拿到listview对象
// listView=(ListView)this.findViewById(R.id.MyListView);
//// //设置实时更新数据
//
// listView.setAdapter(new ListViewAdapter(titles,texts,resIds));
// for(int i=0;i<titles.length;i++){//循环数组,arr.length是数组长度
// System.out.println(titles[i]);//打印数组中的元素
// }
// }this.setTitle("您的汽车信息如下"); //拿到listview对象 listView=(ListView)this.findViewById(R.id.MyListView); //设置实时更新数据 listView.setAdapter(new ListViewAdapter(titles,texts,resIds)); for(int i=0;i<titles.length;i++){//循环数组,arr.length是数组长度 System.out.println(titles[i]);//打印数组中的元素 } } private Handler handler = new Handler() { @SuppressLint("HandlerLeak") @Override public void handleMessage(Message msg) { // 查询操作 searchcar(); } }; private void searchcar() { String brand = null; String color = null; String style = null; String oilMass = null; String plateNumber = null; // 打开网络连接 HttpClient client = new DefaultHttpClient(); StringBuilder builder = new StringBuilder(); // 服务器提交地址 HttpGet myget = new HttpGet(ServerUrl + "MyCar.ashx?type=mycar&LoginName=123" //+ loginName //+ editText1.getText().toString() ); try { HttpResponse response = client.execute(myget); BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent())); // 填充数据流 for (String s = reader.readLine(); s != null; s = reader.readLine()) { builder.append(s); } Log.i("cat", ">>>>>>" + builder.toString()); // 读取json返回数组 JSONObject jsonObject = new JSONObject(builder.toString()); JSONArray jsonArray = jsonObject.getJSONArray("rows"); ArrayList<String> brandlist = new ArrayList<String>(); ArrayList<String> colorlist = new ArrayList<String>(); ArrayList<String> stylelist = new ArrayList<String>(); ArrayList<String> oilMasslist = new ArrayList<String>(); ArrayList<String> plateNumberlist = new ArrayList<String>(); for(int i=0;i<jsonArray.length();i++){ JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i); brand = jsonObject2.getString("JSON_品牌"); brandlist.add(brand); color = jsonObject2.getString("JSON_颜色"); colorlist.add(color); style = jsonObject2.getString("JSON_车型"); stylelist.add(style); oilMass = jsonObject2.getString("JSON_油量"); oilMasslist.add(oilMass); plateNumber = jsonObject2.getString("JSON_车牌号"); plateNumberlist.add(plateNumber); } Log.i("cat", ">>>>>>" + brandlist.toString()); Log.i("cat", ">>>>>>" + colorlist.toString()); Log.i("cat", ">>>>>>" + stylelist.toString()); Log.i("cat", ">>>>>>" + oilMasslist.toString()); Log.i("cat", ">>>>>>" + plateNumberlist.toString()); titles = (String[]) brandlist.toArray(new String[brandlist.size()]); texts = (String[]) colorlist.toArray(new String[colorlist.size()]); mm = (String[]) stylelist.toArray(new String[stylelist.size()]); for(int i=0;i<titles.length;i++){//循环数组,arr.length是数组长度 System.out.println(titles[i]);//打印数组中的元素 } Toast.makeText(LieBiao.this, "查询成功", Toast.LENGTH_SHORT) .show();
// test = 1;
progressDialog1.dismiss(); } catch (Exception e) { //Log.v("url返回", "false"); e.printStackTrace(); Toast.makeText(LieBiao.this, "服务器数据查询失败", Toast.LENGTH_SHORT) .show(); progressDialog1.dismiss(); } } //定义适配器 public class ListViewAdapter extends BaseAdapter{ View [] itemViews; public ListViewAdapter(String [] itemTitles, String [] itemTexts, int [] itemImageRes){ itemViews = new View[itemTitles.length]; for (int i=0; i<itemViews.length; ++i){ itemViews[i] = makeItemView(itemTitles[i], itemTexts[i], itemImageRes[i]); } } public int getCount() { return itemViews.length; } public View getItem(int position) { return itemViews[position]; } public long getItemId(int position) { return position; } private View makeItemView(String strTitle, String strText, int resId) { LayoutInflater inflater = (LayoutInflater)LieBiao.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 使用View的对象itemView与R.layout.item关联 View itemView = inflater.inflate(R.layout.listview_item, null); // 通过findViewById()方法实例R.layout.item内各组件 TextView title = (TextView)itemView.findViewById(R.id.text0); title.setText(strTitle); TextView text = (TextView)itemView.findViewById(R.id.text1); text.setText(strText); ImageView image = (ImageView)itemView.findViewById(R.id.mycar); image.setImageResource(resId); return itemView; } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) return itemViews[position]; return convertView; } }
}
我想问一下titles数组在这一句
titles = (String[]) brandlist.toArray(new String[brandlist.size()]);
我又重新赋值了,为什么这一句listView.setAdapter(new ListViewAdapter(titles,texts,resIds));
里面的titles的值还是一开始赋的值 String [] titles={"汽车1","汽车2","汽车3","汽车4"};到底是什么问题,我不清楚,新人求教各位大神!
谢谢
解决方案
1、titles = (String[]) brandlist.toArray(new String[brandlist.size()]);重新赋值,对adatper中的itemTitles是不会产生任何影响的,改变titles后,这2个变量指向2个不同的数组。
2、itemViews[i] = makeItemView(itemTitles[i],在传入数组后,还没getView,直接生产了TextView数组,实际已经抛弃了itemViews,即使titles不改变指向,只改变里面的元素,对界面也没有影响。
改改
activity中声明一个ListViewAdapter lstAdapter;
listView.setAdapter(new ListViewAdapter(titles,texts,resIds));
改成
lstAdapter = new ListViewAdapter(titles,texts,resIds);
listView.setAdapter(lstAdapter);
ListViewAdapter改
public class ListViewAdapter extends BaseAdapter{
View [] itemViews;
public ListViewAdapter(String [] itemTitles, String [] itemTexts,
int [] itemImageRes){
init(itemTitles,itemTexts,itemImageRes);
}
public void init(String [] itemTitles, String [] itemTexts,
int [] itemImageRes){//增加这个方法
itemViews = new View[itemTitles.length];
for (int i=0; i<itemViews.length; ++i){
itemViews[i] = makeItemView(itemTitles[i], itemTexts[i],
itemImageRes[i]);
}
}
public int getCount() {
return itemViews.length;
}
public View getItem(int position) {
return itemViews[position];
}
public long getItemId(int position) {
return position;
}
private View makeItemView(String strTitle, String strText, int resId) {
LayoutInflater inflater = (LayoutInflater)LieBiao.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 使用View的对象itemView与R.layout.item关联
View itemView = inflater.inflate(R.layout.listview_item, null);
// 通过findViewById()方法实例R.layout.item内各组件
TextView title = (TextView)itemView.findViewById(R.id.text0);
title.setText(strTitle);
TextView text = (TextView)itemView.findViewById(R.id.text1);
text.setText(strText);
ImageView image = (ImageView)itemView.findViewById(R.id.mycar);
image.setImageResource(resId);
return itemView;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
return itemViews[position];
return convertView;
}
}
}
在
titles = (String[]) brandlist.toArray(new String[brandlist.size()]);
texts = (String[]) colorlist.toArray(new String[colorlist.size()]);
mm = (String[]) stylelist.toArray(new String[stylelist.size()]);
增加
lstAdapter.init(titles,texts,mm);//更新值
listAdapter.notifyDataSetChanged();//触发更新事件