Cpt.Hype
Cpt.Hype

Reputation: 1

Appealing an AsyncTask from a class into MainActivity

I am new at android developing and i need a little help with my code. I have to use an AsyncTask from a class into my main activity. And how do I appeal it in the main function?

My main activity:

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

}

And my AsyncTask code is:

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

Upvotes: 0

Views: 52

Answers (2)

Guillaume Barr&#233;
Guillaume Barr&#233;

Reputation: 4218

Try :

new DataReturn().execute("your random ip");

Upvotes: 1

Allay Khalil
Allay Khalil

Reputation: 693

If you are having a new class then the way you are doing this is wrong. if it is inner class of Mainactivity then doing that was right .

Try the following

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

Upvotes: 1

Related Questions