============问题描述============
运行程序,没有显示出数据,而是弹出Toast"没有更多数据了...."求解
public class MainActivity extends Activity {
private ListView listView ;
private ListAdapter adapter ;
static int pageNo =1 ;
String url = "http://dingtao2.gotoip2.com/publish-info.html?type=1&pageLines=10&pageNo=" ;
boolean isScroll = false ;
List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>() ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
adapter = new ListAdapter(this) ;
listView = (ListView) findViewById(R.id.listView1) ;
new DownloadTask().execute(url,Integer.toString(pageNo) ) ;
//listView.setAdapter(adapter) ;
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(isScroll && (scrollState == SCROLL_STATE_IDLE)){ // 状态:0 滑动到底部
new DownloadTask().execute(url,Integer.toString(pageNo) ) ;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
isScroll = ((firstVisibleItem + visibleItemCount) == totalItemCount) ;
System.out.println("------->"+totalItemCount) ;
}
}) ;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class DownloadTask extends AsyncTask<String, Void, List<Map<String, Object>>>{
@Override
protected List<Map<String, Object>> doInBackground(String... params) {
// TODO Auto-generated method stub
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>() ;
HashMap<String, Object> map ;
HttpClient client = new DefaultHttpClient() ;
HttpGet get = new HttpGet(params[0]+params[1]) ;
try {
HttpResponse response = client.execute(get) ;
if(response.getStatusLine().getStatusCode() == 200){
String jsonData = EntityUtils.toString(response.getEntity(), "UTF-8") ;
try {
JSONObject jsonObject = new JSONObject(jsonData) ;
JSONArray jsonArray = jsonObject.getJSONArray("list") ;
if(!jsonArray.isNull(0)){
for(int i = 0 ;i<jsonArray.length() ;i++){
JSONObject jsonObject2 = jsonArray.getJSONObject(i) ;
map = new HashMap<String, Object>() ;
Iterator iterator = jsonObject2.keys() ;
while(iterator.hasNext()){
String key = (String) iterator.next() ;
Object value = jsonObject2.get(key) ;
map.put(key, value) ;
}
list.add(map) ;
}
return list ;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("---1--->"+list.size());
return list ;
}
@Override
protected void onPostExecute(List<Map<String, Object>> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result.size()>0){
lists.addAll(result) ;
adapter.setData(lists) ;
if(pageNo == 1){
listView.setAdapter(adapter) ;
}
adapter.notifyDataSetChanged() ;
pageNo ++ ;
}else{
Toast.makeText(getApplicationContext(), "没有更多数据了....", 0).show() ;
}
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
// System.out.println("--pageNo---"+pageNo);
super.onPreExecute();
}
}
}
=======================================================================================
public class ListAdapter extends BaseAdapter {
List<Map<String,Object>> list ;
LayoutInflater inflater ;
public void setData(List<Map<String,Object>> data){
this.list = data ;
}
public ListAdapter(Context context) {
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>() ;
inflater = LayoutInflater.from(context) ;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size() ;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position) ;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position ;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder vhHolder ;
if(convertView == null){
vhHolder = new ViewHolder() ;
convertView = inflater.inflate(R.layout.list_item, null) ;
vhHolder.tv1 = (TextView) convertView.findViewById(R.id.textView1) ;
vhHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2) ;
convertView.setTag(vhHolder) ;
}else{
vhHolder = (ViewHolder) convertView.getTag() ;
}
vhHolder.tv1.setText(list.get(position).get("id")+"、") ;
vhHolder.tv2.setText(list.get(position).get("title").toString()) ;
return convertView ;
}
static class ViewHolder{
TextView tv1 ;
TextView tv2 ;
}
}
============解决方案1============
引用 2 楼 NASA_001 的回复:
Quote: 引用 1 楼 sagittarius1988 的回复:
说明你doInBackground里获取到的List数据就是空的啊
前辈能帮我修改修改吗?
这个还真不好改,你要看你String jsonData = EntityUtils.toString(response.getEntity(), "UTF-8") ;这里得到的是什么
============解决方案2============
检查下网络请求是否成功,成功后数据返回是否正确,数据返回正确后检查json转化成list是否出错