显示 ProgressDialog Android [英] Show ProgressDialog Android

查看:22
本文介绍了显示 ProgressDialog Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 EditText,它从用户那里获取一个字符串和一个 searchButton.单击 searchButton 时,它将搜索 XML 文件并将其显示在 ListView 中.

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.

我能够接受用户的输入,搜索 XML 文件并在 ListView 中显示用户搜索的值.

I am able to take input from the user, search through the XML file and display the usersearched value in the ListView also.

我想要的是在单击搜索按钮后显示一个 ProgressDialog,例如请等待...检索数据..."或类似的内容,并在显示数据时关闭它.

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();                

        }
    });
}

推荐答案

声明你的进度对话框:

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();

这篇关于显示 ProgressDialog Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆