我想在此代码中添加一个进度对话框,因此当我单击按钮加载此活动时,它不会挂起或在几秒钟内显示黑屏。它也必须在异步任务中吗?我愿意尝试使用Async任务,只是尝试了几次而且我没有让它工作
package net.thinkbin;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class entertainment extends ListActivity {
private static final String TITLE = "Title";
private static final String AUTHOR = "Author";
private static final String VIEWS = "Views";
private static final String RATES = "Rates";
private static final String CONTENT = "Content";
JSONArray ideas = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder3);
Button view = (Button) findViewById(R.id.button1);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("net.thinkbin.TUTORIAL1"));
overridePendingTransition(0, 0);
finish();
}
});
Button share = (Button) findViewById(R.id.button2);
share.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("net.thinkbin.SHARE"));
overridePendingTransition(0, 0);
finish();
}
});
Button menu = (Button) findViewById(R.id.buttonhome);
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("net.thinkbin.MENU"));
overridePendingTransition(0, 0);
finish();
}
});
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://www.thinkbin.net/include/api/index.php?cat=Entertainment&type=Views&i=10");
try{
ideas = json.getJSONArray("Ideas");
// looping through All Contacts
for(int i = 0; i < ideas.length(); i++){
JSONObject c = ideas.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TITLE);
String author = c.getString(AUTHOR);
String views = c.getString(VIEWS);
String rates = c.getString(RATES);
String content = c.getString(CONTENT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TITLE, "Title: " + title);
map.put(AUTHOR, "Author: " + author);
map.put(VIEWS, "Views: " + views);
map.put(RATES, "Rates: " + rates);
map.put(CONTENT, content);
// adding HashList to ArrayList
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main2,
new String[] { TITLE, AUTHOR, VIEWS, RATES, CONTENT },
new int[] { R.id.item_title, R.id.item_subtitle, R.id.item3, R.id.item4, R.id.item5 });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String Title2 = ((TextView) view.findViewById(R.id.item_title)).getText().toString();
String Author2 = ((TextView) view.findViewById(R.id.item_subtitle)).getText().toString();
String Content2 = ((TextView) view.findViewById(R.id.item5)).getText().toString();
Intent in = new Intent(getApplicationContext(), idea.class);
overridePendingTransition(0, 0);
in.putExtra(TITLE, Title2);
in.putExtra(AUTHOR, Author2);
in.putExtra(CONTENT, Content2);
startActivity(in);
}
});
}
}
答案 0 :(得分:0)
您不必AsyncTask
就可以Activity implements Runnable
Thread.start(this)
,然后从onClick
致电void run() {...}
,将您的代码放入您的Activity
Thread.start()
。然后在runOnUiThread(new Runnable() { public void run() { dismissDialog(YOUR_DLG)) }
之前显示对话框,并在任务结束时终止对话框。但是请注意,在杀死你时你必须做{{1}},因为只能从UI线程控制UI。
AsyncTask虽然更好。
答案 1 :(得分:0)
我使用以下代码显示ProgressDialog
,
progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);
new Thread ( new Runnable()
{
public void run()
{
// your code goes here
}
}).start();
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg1)
{
progDailog.dismiss();
}
}
答案 2 :(得分:0)
您问题的最佳解决方案是使用异步任务。 因为Async任务有很少的方法,如
onPreExecute()
- 在这里初始化你的任务。
doInBackground()
- 在这里,您可以获得网络点击。即从服务器获取数据。
onPostExecute()
- 在这里处理从服务器收到的响应以及与UI相关的任务。
除此之外,它还有一个方法
onProgressUpdate()
- 可用于发布已完成任务的进度。
这是显示有关如何使用AsyncTask的示例的Link。