实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.samir; import java.util.ArrayList; import java.util.Collections; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity implements TextWatcher, OnItemClickListener { private static final String rssFeed = "https://dl.dropbox.com/s/ubezjbp553u750k/name.xml?dl=1"; ListView listView; List<Items> items; List<Items> filterArray = new ArrayList<Items>(); ArrayList<Item> itemsSection = new ArrayList<Item>(); NamesAdapter objAdapter = null; EditText mySearch; String searchString; AlertDialog alert = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView) findViewById(R.id.listview); listView.setOnItemClickListener(this); mySearch = (EditText) findViewById(R.id.input_search_query); mySearch.addTextChangedListener(this); // XML Parsing Using AsyncTask... if (isNetworkAvailable()) { new MyTask().execute(); } else { showToast("No Netwrok Connection!!!"); this.finish(); } } // My AsyncTask start... class MyTask extends AsyncTask<Void, Void, Void> { ProgressDialog pDialog; @Override protected void onPreExecute() { pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Loading..."); pDialog.show(); super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { items = new NamesParser().getData(rssFeed); return null; } @Override protected void onPostExecute(Void result) { if (null != pDialog && pDialog.isShowing()) { pDialog.dismiss(); } if (null == items || items.size() == 0) { showToast("No data found from web!!!"); MainActivity.this.finish(); } else { setAdapterToListview(items); } super.onPostExecute(result); } } // Textwatcher's ovveride methods @Override public void afterTextChanged(Editable s) { filterArray.clear(); searchString = mySearch.getText().toString().trim() .replaceAll("\\s", ""); if (items.size() > 0 && searchString.length() > 0) { for (Items name : items) { if (name.getName().toLowerCase() .startsWith(searchString.toLowerCase())) { filterArray.add(name); } } setAdapterToListview(filterArray); } else { filterArray.clear(); setAdapterToListview(items); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } // Here Data is Filtered!!! @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } // setAdapter Here.... public void setAdapterToListview(List<Items> listForAdapter) { itemsSection.clear(); if (null != listForAdapter && listForAdapter.size() != 0) { Collections.sort(listForAdapter); char checkChar = ' '; for (int index = 0; index < listForAdapter.size(); index ) { Items objItem = (Items) listForAdapter.get(index); char firstChar = objItem.getName().charAt(0); if (' ' != checkChar) { if (checkChar != firstChar) { ItemsSections objSectionItem = new ItemsSections(); objSectionItem.setSectionLetter(firstChar); itemsSection.add(objSectionItem); } } else { ItemsSections objSectionItem = new ItemsSections(); objSectionItem.setSectionLetter(firstChar); itemsSection.add(objSectionItem); } checkChar = firstChar; itemsSection.add(objItem); } } else { showAlertView(); } if (null == objAdapter) { objAdapter = new NamesAdapter(MainActivity.this, itemsSection); listView.setAdapter(objAdapter); } else { objAdapter.notifyDataSetChanged(); } } // Toast is here... private void showToast(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } // OnListClick,Get Name... @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Item item = itemsSection.get(position); if (view.getTag().getClass().getSimpleName().equals("ViewHolderName")) { Items objSchoolname = (Items) item; showToast(objSchoolname.getName()); } else { ItemsSections objSectionsName = (ItemsSections) item; showToast("Section :: " String.valueOf(objSectionsName.getSectionLetter())); } } // Check Internet Connection!!! public boolean isNetworkAvailable() { ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i ) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } // OnBackPressed... @Override public void onBackPressed() { AlertDialog alert_back = new AlertDialog.Builder(this).create(); alert_back.setTitle("Quit?"); alert_back.setMessage("Are you sure want to Quit?"); alert_back.setButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert_back.setButton2("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MainActivity.this.finish(); } }); alert_back.show(); } private void showAlertView() { if (null == alert) alert = new AlertDialog.Builder(this).create(); if (alert.isShowing()) { return; } alert.setTitle("Not Found!!!"); alert.setMessage("Can not find name Like '" searchString "'"); alert.setButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.show(); } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论