android listView 滑動載入資料 該資料是服務端擷取的

來源:互聯網
上載者:User

標籤:android   android listview   資料   listview   

package com.sunway.works.applycash;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sunway.works.R;
import com.sunway.works.applycash.ApplyCashListActivity;
import com.sunway.works.cost.apply.EditExpenseActivity;
import com.sunway.works.cost.apply.ExpenseAuditActivity;
import com.sunway.works.entitys.ApplyCashEntity;
import com.sunway.works.entitys.IousEntity;
import com.sunway.works.network.HttpService;
import com.sunway.works.network.HttpServiceImpl;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.Service;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class EditOneCashActivity extends Activity implements
OnScrollListener {
private ListView listView;
private Context context;
private View top_title;
private TextView tvtitle;
private PopupWindow popupWindow;
private View view;
private View bar1;
private View bar2;
private Spinner sp_depart;
private EditText ed_iounnub;
private EditText ed_purpose;
private EditText ed_date_from;
private EditText ed_date_over;
private Button btn_date_from;
private Button btn_date_over;
private EditText ed_iousum;
private Spinner sp_checkstate;
private EditText ed_borrower;
private Button btn_query;
private Handler handler;
private static final int DATE_DIALOG_ID = 1;
private static final int SHOW_DATAPICK = 0;
private int mYear;
private int mMonth;
private int mDay;
private DeleteCashListAdapter adapter;
private List<ApplyCashEntity> listdata = new ArrayList<ApplyCashEntity>();
private InputMethodManager imm;
private Button btn_left;
private Button btn_right;
private List<ApplyCashEntity> loadDatas; // 載入資料
// private ChooseCheckmanAdapter adapters; //適配器
private LinearLayout loadingLayout;// 底部載入布局
private Map<Integer, Boolean> isCheckedMap = new HashMap<Integer, Boolean>();;
private boolean over;// 判斷資料是否已全部載入
private Thread mThread;// 載入線程
private int unb = 1;
// 設定布局顯示內容
private android.widget.LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// 設定布局顯示目標最大化屬性
private android.widget.LinearLayout.LayoutParams FFlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
private ProgressBar progressBar;
private PopupWindow window;
private Intent intent;
private ListView list2;
Handler mhandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:// 判斷是否資料已全部載入
if (!over) {
adapter.notifyDataSetChanged();
} else {
listView.removeFooterView(loadingLayout);
Toast.makeText(context, "資料載入完畢", Toast.LENGTH_SHORT)
.show();
}
break;
case 2:
adapter.notifyDataSetChanged();
break;
}
};
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_data_list);
context = getApplicationContext();
handler = new Handler();


// 初始化底部載入視圖
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
progressBar = new ProgressBar(context);
progressBar.setPadding(0, 0, 15, 0);
layout.addView(progressBar, mLayoutParams);
TextView textView = new TextView(context);
textView.setText("載入中...");
textView.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(textView, FFlayoutParams);
loadingLayout = new LinearLayout(context);
loadingLayout.addView(layout, mLayoutParams);
loadingLayout.setGravity(Gravity.CENTER);

// 初始化ListView並設定事件

        initView();
       getListener();

}

private void getListener() {
btn_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}

private void initView() {
// TODO Auto-generated method stub
top_title = this.findViewById(R.id.top_title);
tvtitle = (TextView) top_title.findViewById(R.id.tvtitle);
btn_left = (Button) top_title.findViewById(R.id.btn_title_left);
btn_right = (Button) top_title.findViewById(R.id.btn_title_right);
listView = (ListView) findViewById(R.id.data_enterlist);

listView.addFooterView(loadingLayout);

               //   listView.addFooterView()方法一定要在其setadapter()方法之前使用。不然報錯

adapter = new DeleteCashListAdapter(EditOneCashActivity.this,
listdata, true);
listView.setAdapter(adapter);
listView.setOnScrollListener(EditOneCashActivity.this);
over = false;
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (mThread == null || !mThread.isAlive()) {
mThread = new Thread() {

public void run() {

   //從網上讀取要獲得的資料   這是我自己的讀取server的資料,個人因需要自己寫自己的連網讀取資料

HttpService service = new HttpServiceImpl();

String i = (10 * (unb - 1) + 1) + "";   //   i 是下標,決定從哪一條資料開始讀,10代表每一次讀取資料的條數

 loadDatas = service.getApplyCashList(param1, i);
// 如果載入資料不為空白,則將載入得到的資料添加到已有的mData資料中.否則資料載入完全

if (loadDatas != null && loadDatas.size() > 0) {
listdata.addAll(loadDatas);
loadDatas.clear();
} else {
unb = unb - 1;
over = true;
}
unb++;
mhandler.sendEmptyMessage(1);//更新UI
};
};
mThread.start();
}
}

}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub

}

}

android listView 滑動載入資料 該資料是服務端擷取的

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.