为什么我listview [email protected]
public int getCount() {
return 3;
这个return数字小于5. listview能显示,,,,,,,,,当return大于5时。查询的时候只有一部分能显示
就搜索这一块
private ListView listview;
private List<News> newsList;
private AppContext appContext;// 全局Context
private MainListViewAdapter listViewAdapter;
private ProgressDialog selectorDialog;
private Button mBtn1;
private AutoCompleteTextView mA;
private String path;
private NewsList list;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
selectorDialog.cancel();
if (msg.what == 1) {
newsList = (List<News>) msg.obj;
listViewAdapter = new MainListViewAdapter(MyExamActivity.this,
newsList);
listview.setAdapter(listViewAdapter);
}else if(msg.what == 5){
newsList = (List<News>) msg.obj;
listViewAdapter = new MainListViewAdapter(MyExamActivity.this,
newsList);
listViewAdapter.notifyDataSetChanged();
listview.setAdapter(listViewAdapter);
} else if (msg.what == -1) {
UIHelper.ToastMessage(MyExamActivity.this, "没有您输入的地址,请重新输入");
} else if (msg.what == -2) {
UIHelper.ToastMessage(MyExamActivity.this,
R.string.xml_parser_failed);
}
super.handleMessage(msg);
}};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appContext = (AppContext) getApplication();
// 网络连接判断
if (!appContext.isNetworkConnected())
UIHelper.ToastMessage(this, R.string.network_not_connected);
init();
initData();
}
private void init() {
//mAt=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
mBtn1=(Button) findViewById(R.id.button1);
mA=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
listview = (ListView) findViewById(R.id.listView1);
selectorDialog = ProgressDialog.show(this, null, "正在加载,请稍候...", true,
false);
mA=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
String[] names = {"北京", "北海道", "北戴河", "北门","beijing","beidaihe","moerben" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, names);
mA.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MyExamActivity.this,
JianjieActivity.class);
//intent.putExtra(MYFOOD, (Food)list.get(position));
startActivity(intent);
}
});
mBtn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String name=mA.getText().toString();
try {
String name1 = URLEncoder.encode(name, "UTF-8");
path="http://juntravel.cn/api/hotels.php?dest="+name1;
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
new Thread( new Runnable() {
public void run() {
Message msg = new Message();
boolean isRefresh = false;
try {
list= NewsList.parse(StringUtils.toJSONObject(JSONProvider.getJSONData(path)));
if (list.getNewsCount() > 0) {
msg.what = 5;
msg.obj = list.getNewslist();
appContext.saveObject(list, "newslist_");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AppException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendMessage(msg);
}
}).start();
}
});
}
//Handler mHandler = new Handler() {
// public void handleMessage(Message msg) {
//};
private void initData() {
selectorDialog.show();
new Thread( new Runnable() {
public void run() {
Message msg = new Message();
boolean isRefresh = false;
try {
list = appContext.getNewsList();
if (list.getNewsCount() > 0) {
msg.what = 1;
msg.obj = list.getNewslist();
appContext.saveObject(list, "newslist_");
} else {
msg.what = -1;
}
} catch (AppException e) {
e.printStackTrace();
msg.what = -2;
msg.obj = e;
}
handler.sendMessage(msg);
}
}).start();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}这是这个页面代码
------解决思路----------------------
Adapter里的getCount() 这个方法就是要显示的数目,一般都是return list.size(),也就是显示全部数据
------解决思路----------------------
获取完数据再刷新适配器看看!
------解决思路----------------------
你的数据有多少条。就显示多少条啊。
要是你的list里面要是没有5条,return 5 也是不行的。