我有一个EditText这需要从用户和searchButton一个字符串。
当searchButton被点击时,它会通过搜索XML文件,并在ListView中显示。
我能够把来自用户的输入,通过搜索XML文件,并显示在ListView也usersearched值。
我要的是显示了searchButton被点击,如请稍候...检索数据...或者类似的东西,并关闭它时,数据是 ProgressDialog
之后如图所示。
公共类Tab1Activity扩展ListActivity {
私人按钮okButton;
私人按钮searchButton;
吐司面包;
XML字符串;
私人TextView的searchText;
私人字符串searchTextString;
HashMap的<字符串,字符串> O;
ArrayList的< HashMap的<字符串,字符串>> mylist中=新的ArrayList< HashMap的<字符串,字符串>>();
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.tab1);
searchButton =(按钮)findViewById(R.id.search_button);
searchButton.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
// TODO自动生成方法存根
System.out.print(你好);
searchText =(TextView中)findViewById(R.id.search_text);
。searchTextString = searchText.getText()的toString();
ReadXML的(searchTextString);
}
});
}
私人无效的ReadXml(字符串searchTextString1){
ArrayList的< HashMap的<字符串,字符串>> mylist中=新的ArrayList< HashMap的<字符串,字符串>>();
XML字符串= XMLfunctions.getXML();
//这里XMLfunctions是解析XML的类名
文档DOC = XMLfunctions.XMLfromString(XML);
INT numResults = XMLfunctions.numResults(DOC);
如果((numResults&所述; = 0)){
Toast.makeText(Tab1Activity.this,测试XMLParser的,
Toast.LENGTH_LONG).show();
完();
}
节点列表节点= doc.getElementsByTagName(结果);
的for(int i = 0; I< nodes.getLength();我++){
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
元素e =(元)nodes.item(我);
字符串nameMapString = XMLfunctions.getValue(即名);
如果(nameMapString.toLowerCase()。的indexOf(searchTextString1.toLowerCase())!= - 1)//!= -1表示字符串是present在搜索字符串
{
map.put(ID,XMLfunctions.getValue(即,ID));
map.put(名,XMLfunctions.getValue(即名));
map.put(分数,XMLfunctions.getValue(即分数));
mylist.add(图)
}
}
ListAdapter适配器=新SimpleAdapter(这一点,mylist中,
R.layout.parsexml,新的String [] {姓名,分数},新的INT [] {
R.id.item_title,R.id.item_subtitle});
setListAdapter(适配器);
最终的ListView LV = getListView();
lv.setTextFilterEnabled(真正的);
lv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
@燮pressWarnings(未登记)
HashMap的<字符串,字符串> O =(HashMap的<字符串,字符串>)LV
.getItemAtPosition(位置);
Toast.makeText(Tab1Activity.this,
姓名+ o.get(姓名)+点击,Toast.LENGTH_LONG)
。显示();
}
});
}
解决方案
声明你的进度对话框:
ProgressDialog进展情况;
当你准备开始进度对话框:
进度= ProgressDialog.show(这一点,对话框标题
对话消息,真正的);
和使之消失时,你就大功告成了:
progress.dismiss();
这里是你一个小螺纹例如:
//注意:声明ProgressDialog进度,在你的类中的字段。
进度= ProgressDialog.show(这一点,对话框标题
对话消息,真正的);
新主题(新的Runnable(){
@覆盖
公共无效的run()
{
//做到这一点需要很长时间的东西
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run()
{
progress.dismiss();
}
});
}
})。开始();
I have an EditText which takes a String from the user and a searchButton.
When the searchButton is clicked, it will search through the XML file and display it in the ListView.
I am able to take input from the user, search through the XML file and display the usersearched value in the ListView also.
What I want is to display a ProgressDialog
after the searchButton is clicked like "PLEASE WAIT...RETRIEVING DATA..." or something like that and dismiss it when the data is shown.
public class Tab1Activity extends ListActivity {
private Button okButton;
private Button searchButton;
Toast toast;
String xml;
private TextView searchText;
private String searchTextString;
HashMap<String, String> o;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1);
searchButton = (Button) findViewById(R.id.search_button);
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.print("hello");
searchText = (TextView) findViewById(R.id.search_text);
searchTextString = searchText.getText().toString();
readXml(searchTextString);
}
});
}
private void readXml(String searchTextString1) {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML();
//Here XMLfunctions is class name which parse xml
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if ((numResults <= 0)) {
Toast.makeText(Tab1Activity.this, "Testing xmlparser",
Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nodes.item(i);
String nameMapString = XMLfunctions.getValue(e, "name");
if ( nameMapString.toLowerCase().indexOf(searchTextString1.toLowerCase()) != -1 ) // != -1 means string is present in the search string
{
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("name", XMLfunctions.getValue(e, "name"));
map.put("Score", XMLfunctions.getValue(e, "score"));
mylist.add(map);
}
}
ListAdapter adapter = new SimpleAdapter(this, mylist,
R.layout.parsexml, new String[] { "name", "Score" }, new int[] {
R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv
.getItemAtPosition(position);
Toast.makeText(Tab1Activity.this,
"Name "+o.get("name")+" Clicked", Toast.LENGTH_LONG)
.show();
}
});
}
解决方案
Declare your progress dialog:
ProgressDialog progress;
When you're ready to start the progress dialog:
progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);
and to make it go away when you're done:
progress.dismiss();
Here's a little thread example for you:
// Note: declare ProgressDialog progress as a field in your class.
progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);
new Thread(new Runnable() {
@Override
public void run()
{
// do the thing that takes a long time
runOnUiThread(new Runnable() {
@Override
public void run()
{
progress.dismiss();
}
});
}
}).start();