我有一个listview,其中onCreate方法我正在执行URL,它给出了我在Listview中显示的应用程序列表。当我使用刷卡刷新时,我想要清除所有列表视图项目并再次执行该URL在Listview中获取响应。我可以这样做..
这是我的代码: -
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
m_Main = inflater.inflate(R.layout.deal_listing, container, false);//intialize mainLayout
new CDealDataSent().execute(m_DealListingURL);// execute Url to fetch Deal list from serevr.
}
private void init() {// initialize controls
mSwipeRefresh = (SwipeRefreshLayout) m_Main.findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// here I want to clear all items in listview and again execute That Url and fetech new DATA/..
}
});
m_ListView = (ListView) m_Main.findViewById(R.id.dealList);// findind Id of Listview
}
@SuppressWarnings("deprecation")
private String DealListing(String url) {// this method POST details of user to server
InputStream inputStream;
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json;
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();// making object of Jsons.
jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
jsonObject.put("pin", m_szEncryptedPassword);// put password
jsonObject.put("recordcount", sz_RecordCount);// put record count
jsonObject.put("lastcountvalue", sz_LastCount);// put last count
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();// convert Json object to string
if (BuildConfig.klogInfo)// print json request to server
Log.d(TAG, "Server Request:-" + json);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
// 9. receive response as inputStream
inputStream = entity.getContent();
if (BuildConfig.klogInfo)
Log.d(TAG, "InputStream:-" + inputStream.toString());
Log.d(TAG, "Response:-" + httpResponse.toString());
StatusLine statusLine = httpResponse.getStatusLine();
if (BuildConfig.klogInfo) {// print server status line code
Log.d(TAG, "Status Code:-" + statusLine);
}
////Log.d("resp_body", resp_body.toString());
int statusCode = statusLine.getStatusCode();
// 10. convert inputstream to string
if (statusCode == 200) {
// 10. convert inputstream to string
s_szresult = CJsonsResponse.convertInputStreamToString(inputStream);
if (BuildConfig.klogInfo)// print server response
Log.d(TAG, "Server Response:-" + s_szresult);
} else
s_szresult = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
Log.d(TAG, "Response Password:" + m_szEncryptedPassword);
Log.d(TAG, "Record Count:" + sz_RecordCount);
Log.d(TAG, "Last count:-" + sz_LastCount);
// 11. return s_szResult
return s_szresult;
}
@SuppressWarnings("StatementWithEmptyBody")
private void getResponse() throws JSONException {// getting response from serevr ..................
if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
} else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
}
}
// sending deal data to server and retreive Deal list......
class CDealDataSent extends AsyncTask<String, Void, String> {
public CDealAppDatastorage item;// declaring DealAppdataStorage
@Override
protected void onPreExecute() {
super.onPreExecute();
m_ProgressView.setVisibility(View.VISIBLE);// make rogressview Visible while doing background process
}
@Override
protected String doInBackground(String... urls) {
return DealListing(urls[0]);// sending data to server...
}
// onPostExecute displays the results of the AsyncTask.
@SuppressWarnings("deprecation")
@SuppressLint("SetTextI18n")
@Override
protected void onPostExecute(final String result) {// this method Update UI
m_ProgressView.setVisibility(View.GONE);// make progressview Gone when background process completes...
mSwipeRefresh.setRefreshing(false);
try {
m_oResponseobject = new JSONObject(result);// getting response from server
JSONArray posts = m_oResponseobject.optJSONArray("dealList");// get Deal list in array from response
for (int i = 0; i < posts.length(); i++) {// loop for counting deals from server
JSONObject post = posts.getJSONObject(i);// counting deal based on index
item = new CDealAppDatastorage();// creating object of DealAppdata storage
item.setM_szHeaderText(post.getString("dealname"));// get deal name from response
item.setM_szsubHeaderText(post.getString("dealcode"));// get dealcode from response
item.setM_szDealValue(post.getString("dealvalue"));// get deal value from response
item.setM_n_Image(m_n_FormImage[i]);//set Image Index wise(Dummy)
s_oDataset.add(item);// add all items in ArrayList
}
if (!hasLoadMore) {
// LoadMore button
btnLoadMore = new Button(getActivity());// creating button
btnLoadMore.setText("LOAD MORE DEALS");// set Text in Button
btnLoadMore.setBackgroundResource(R.drawable.button_boarder);// set Background Resource
btnLoadMore.setTextAppearance(getActivity(), android.R.style.TextAppearance_DeviceDefault_Small);// setting Text appearence of button text
btnLoadMore.setTextColor(Color.WHITE);// set Color of button text
btnLoadMore.setGravity(Gravity.CENTER);// set Gravity of button text
hasLoadMore=true;
}
if (!s_oDataset.isEmpty()) {// condition if data in arraylist is not empty
// Adding Load More button to lisview at bottom
m_ListView.addFooterView(btnLoadMore);// add footer in listview
m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
m_oAdapter.notifyDataSetChanged();// notify adapter about changes in List
} else {
btnLoadMore.setVisibility(View.GONE);// else Load buttonvisibility set to Gone
}
btnLoadMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {// load more button onclick listener
m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data
m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above
sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
new DealNext().execute(m_DealListingURL);// POST DATA TO SERVER TO LOAD MORE DATA......
}
});
getResponse();// get response from server....
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
您需要导入一些库。也许你可以看到这个:https://github.com/erikwt/PullToRefresh-ListView
答案 1 :(得分:0)
像这样更新您的代码
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
m_Main = inflater.inflate(R.layout.deal_listing, container, false);//intialize mainLayout
new CDealDataSent().execute(m_DealListingURL);// execute Url to fetch Deal list from serevr.
}
private void init() {// initialize controls
mSwipeRefresh = (SwipeRefreshLayout) m_Main.findViewById(R.id.activity_main_swipe_refresh_layout);
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
m_ListView.removeFooterView(loadMoreButton); // call this
s_oDataset.clear();
new CDealDataSent().execute(m_DealListingURL);// execute Url to fetch Deal list from serevr.
}
});