使用AsyncTask中的onPostExecute方法列表项click事件在Android中调用另一个活动

时间:2018-10-05 07:02:20

标签: android android-intent android-asynctask android-context

我正在一个项目,该项目填充SQLite数据库表以用于列表视图并使用简单的数组适配器。

我正在为此目的使用Asyntask,但在以下情况下出现问题:

  1. 我要呼叫其他活动,并且
  2. 传递我从setOnItemClickListener
  3. 获得的一些值

我需要使用onPostExecute setOnItemClickListener方法将这两件事存档。这是我的代码。

        package com.me.doctor.doctor_me;

        import android.app.Activity;
        import android.app.Application;
        import android.content.Context;
        import android.content.Intent;
        import android.database.Cursor;
        import android.database.sqlite.SQLiteDatabase;
        import android.os.AsyncTask;
        import android.view.View;
        import android.widget.AdapterView;
        import android.widget.ListView;
        import android.widget.Toast;

        public class BackgroundTask extends AsyncTask<String,Doctor,String> {

            Context ctx;
            DoctorAdapter doctorAdapter;
            Activity activity;
            ListView listView;
            Doctor doctor;
            DisplayDoctor displayDoctor;

            BackgroundTask(Context ctx){
                this.ctx = ctx;
                activity = (Activity) ctx;
                doctor = new Doctor();
                displayDoctor = new DisplayDoctor();
            }
            @Override
            protected String doInBackground(String... strings) {

                String method = strings[0];
                DatabaseOperation databaseOperation = new DatabaseOperation(ctx);
                if(method.equals("get_info")){
                    listView = activity.findViewById(R.id.display_list_view);
                    SQLiteDatabase db = databaseOperation.getReadableDatabase();
                    Cursor cursor = databaseOperation.getInformation(db);
                    doctorAdapter = new DoctorAdapter(ctx,R.layout.display_doctor_row);

                    String name, category, hospital;
                    int id;

                    while(cursor.moveToNext()){
                        id = cursor.getInt(cursor.getColumnIndex("d_id"));
                        name = cursor.getString(cursor.getColumnIndex("d_name"));
                        category = cursor.getString(cursor.getColumnIndex("d_category"));
                        hospital = cursor.getString(cursor.getColumnIndex("d_hospital"));
                        Doctor doctor = new Doctor(id,name,category,hospital);
                        publishProgress(doctor);
                    }
                    return "get_info";
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Doctor... values) {
                // add each of doctor class object add method inside the adapter class
                doctorAdapter.add(values[0]);
            }

            @Override
            protected void onPostExecute(String s) {
                if(s.equals("get_info")){
                    listView.setAdapter(doctorAdapter);

                    listView.setOnItemClickListener(new  android.widget.AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                final int position, long id) {

                            Doctor doctor = (Doctor)parent.getItemAtPosition(position);
                            String ID = Integer.toString(doctor.getId());
                            Toast.makeText(ctx,ID,Toast.LENGTH_LONG).show();

                            // I need fire another activity and pass some values which i getting here

                        }
                    });
                }else{
                    Toast.makeText(ctx,s,Toast.LENGTH_LONG).show();
                }

            }

        }

这是调用AsyncTask类的类

            package com.me.doctor.doctor_me;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;

        public class DisplayDoctor extends AppCompatActivity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.display_doctor_layout);
                BackgroundTask backgroundTask = new BackgroundTask(this);
                backgroundTask.execute("get_info");
            }

        }

我曾经调查过有关Stack Overflow的封闭问题,但没有找到解决方案。

1 个答案:

答案 0 :(得分:1)

简短答案: 您已经在后台任务中有了上下文      Context ctx;

使用这个来调用下一个活动     ctx.startActivity(nextActivityIntent)

u可以像这样向意图添加值     Intent nextActivityIntent = new Intent(ctx,NextActivity.class); nextActivityIntent.putExtra("data", "some data");

与异步任务,我想您正在尝试查询另一个线程上的数据库 您可以将加载程序用于相同的 加载程序在单独的线程上运行

这是一个游标加载器的简单示例

取自github

的示例

公共类ForecastFragment扩展Fragment实现LoaderManager.LoaderCallbacks {

public static final int LOADER_ID = 0;
private ArrayAdapter<String> forecastAdapter;
private ForecastAdapter mForecastAdapter;

public ForecastFragment() { }

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    getLoaderManager().initLoader(LOADER_ID, null, this);
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    return rootView;
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    //some database query
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    //some action
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {

}

}