我是android开发的新手,我需要一点帮助我的代码。我必须使用类中的AsyncTask进入我的主要活动。我如何在主要功能中对其提出上诉?
我的主要活动:
public class ListActivity extends Activity implements OnItemClickListener {
ArrayList<Person> lista;
ProgressDialog progress;
private ListView lv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista);
lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(this);
new DataReturn().onPostExecute("random ip");
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Person p = (Person) arg0.getItemAtPosition(arg2);
new AlertDialog.Builder(ListActivity.this).setTitle("Information").setMessage(p.getLastName()).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setIcon(android.R.drawable.ic_dialog_alert).show();
}
我的AsyncTask代码是:
public class DataReturn extends AsyncTask<String,Void,String> {
ArrayList<Person> rlist;
Dialog progress;
ListView rlv;
Context context;
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog progress = new ProgressDialog(null);
progress.setCancelable(false);
progress.setMessage("Loading...");
progress.setTitle("Loading");
progress.show();
}
protected String doInBackground(String... arg0) {
return Utils.httpGetFromUrl(arg0[0]);
}
protected void onPostExecute(String result) {
ArrayList<Person> rlist = Utils.parseJson(result);
// ArrayAdapter<String> arrayAdapter = new
// ArrayAdapter<String>(ListActivity.this,
// android.R.layout.simple_list_item_1);
ArrayAdapter<Person> adapter = new ClassAdapter(context, rlist);
rlv.setAdapter(adapter);
progress.dismiss();
}
答案 0 :(得分:1)
如果你有一个新课程,那么你这样做的方式是错误的。如果它是Mainactivity的内部类,那么这样做是正确的。
尝试以下
//Asuming there is no parameters in the constructor but context other wise
//delete MainActivity.this
DataReturn dataReturn = new DataReturn(MainActivity.this);
dataReturn.execute(your random ip);
答案 1 :(得分:1)
尝试:
new DataReturn().execute("your random ip");