简体   繁体   English

使用BaseAdapter返回按钮到上一个活动

[英]Return button to previous activity with BaseAdapter

I currently have two activities doing HTTP requests. 我目前有两个活动正在执行HTTP请求。

The first activity contains a CustomList class extends BaseAdapter. 第一个活动包含一个CustomList类,它扩展了BaseAdapter。

On the second, there is a previous button allowing me to return to the first activity. 在第二个上,有一个上一个按钮,允许我返回到第一个活动。

Returning to the first activity, I would like to be able to recover the state in which I left it. 返回第一个活动,我希望能够恢复离开它的状态。 That is to say to be able to find the information which also come from an HTTP request. 也就是说能够找到同样来自HTTP请求的信息。 I would like to find the data "infos_user" which is in the first activity and all the data in the BaseAdapter. 我想在第一个活动中找到数据“ infos_user”,并在BaseAdapter中找到所有数据。

My architecture is as follows: Activity 0 (HTTP request) -> Activity 1 (with BaseAdapter and HTTP request) -> Activity 2 (HTTP request) 我的体系结构如下:活动0(HTTP请求)->活动1(带有BaseAdapter和HTTP请求)->活动2(HTTP请求)

I put all the code because I really don't know how can I do this :/ 我放了所有代码,因为我真的不知道该怎么做:/

First activity: 第一次活动:

public class GetChildrenList extends AppCompatActivity implements View.OnClickListener {
private ArrayList<Child> childrenImeList = new ArrayList<Child>();

private Button btn_previous;
private ListView itemsListView;
private TextView tv_signin_success;

int id = 0;
String infos_user;
String email;
String password;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_children_list);

    infos_user = (String) getIntent().getSerializableExtra("infos_user");

    Intent intent = new Intent(GetChildrenList.this , GetLearningGoalsList.class);
    intent.putExtra("username", infos_user);

    btn_previous = (Button) findViewById(R.id.btn_previous);

    btn_previous.setOnClickListener(this);

    tv_signin_success = (TextView) findViewById(R.id.tv_signin_success);


    tv_signin_success.setText("Bonjour " + infos_user + "!");

    itemsListView  = (ListView)findViewById(R.id.list_view_children);


    new GetChildrenAsync().execute();
}

class GetChildrenAsync extends AsyncTask<String, Void, ArrayList<Child>>  {

    private Dialog loadingDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loadingDialog = ProgressDialog.show(GetChildrenList.this, "Please wait", "Loading...");
    }

    @Override
    protected ArrayList<Child> doInBackground(String... params) {


        int age = 0;
        email = (String) getIntent().getSerializableExtra("email");
        password = (String) getIntent().getSerializableExtra("password");
        String first_name = null;
        String last_name = null;

        try {

            SendRequest sr = new SendRequest();
            String result = sr.sendHttpRequest("http://" + sr.getIP_ADDRESS() + "/childrenime/list", "GET", true, email, password);

            String jsonResult = "{ \"children\":" + result + "}";

            Log.d("result1", jsonResult);

            //Manage JSON result
            JSONObject jsonObject = new JSONObject(jsonResult);
            JSONArray childrenArray = jsonObject.getJSONArray("children");

            for (int i = 0; i < childrenArray.length(); ++i) {
                JSONObject child = childrenArray.getJSONObject(i);
                id = child.getInt("id");
                first_name = child.getString("first_name");
                last_name = child.getString("last_name");
                age = child.getInt("age");

                String name = first_name + " " + last_name;

                childrenImeList.add(new Child(id,name,age));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return childrenImeList;

    }

    @Override
    protected void onPostExecute(final ArrayList<Child> childrenListInformation) {
        loadingDialog.dismiss();
        if(childrenListInformation.size() > 0) {
            CustomListChildrenAdapter adapter = new CustomListChildrenAdapter(GetChildrenList.this, childrenListInformation);
            itemsListView.setAdapter(adapter);
        }
        else{
            Toast.makeText(getApplicationContext(), "Impossible de récupérer la liste des enfants", Toast.LENGTH_LONG).show();
        }
    }
}
}

BaseAdapter: BaseAdapter:

public class CustomListChildrenAdapter extends BaseAdapter implements View.OnClickListener {
private Context context;
private ArrayList<Child> children;
private Button btnChoose;
private TextView childrenName;
private TextView childrenAge;

public CustomListChildrenAdapter(Context context, ArrayList<Child> children) {
    this.context = context;
    this.children = children;
}

@Override
public int getCount() {
    return children.size(); //returns total item in the list
}

@Override
public Object getItem(int position) {
    return children.get(position); //returns the item at the specified position
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.layout_list_view_children,null);
        childrenName = (TextView)view.findViewById(R.id.tv_childrenName);
        childrenAge = (TextView) view.findViewById(R.id.tv_childrenAge);
        btnChoose = (Button) view.findViewById(R.id.btn_choose);
        btnChoose.setOnClickListener(this);

    } else {
        view = convertView;
    }

    btnChoose.setTag(position);

    Child currentItem = (Child) getItem(position);
    childrenName.setText(currentItem.getChildName());
    childrenAge.setText(currentItem.getChildAge() + "");

    return view;
}

@Override
public void onClick(View v) {
    Integer position = (Integer) v.getTag();
    Child item = (Child) getItem(position);
    String email = (String) ((Activity) context).getIntent().getSerializableExtra("email");
    String password = (String) ((Activity) context).getIntent().getSerializableExtra("password");

    Intent intent = new Intent(context, GetLearningGoalsList.class);
    intent.putExtra("idChild",item.getId());
    intent.putExtra("email",email);
    intent.putExtra("password",password);

    context.startActivity(intent);

}
}

Second Activity: 第二项活动:

public class GetLearningGoalsList extends AppCompatActivity implements View.OnClickListener {
private ArrayList<LearningGoal> childrenLearningList = new ArrayList<LearningGoal>();

private Button btn_previous;
private ListView itemsListView;

String email;
String password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_learning_goals_list);

    btn_previous = (Button) findViewById(R.id.btn_previous);

    btn_previous.setOnClickListener(this);

    itemsListView  = (ListView)findViewById(R.id.list_view_learning_goals);


    new GetLearningGoalsAsync().execute();
}

@Override
public void onClick(View v) {
    Intent myIntent = new Intent(GetLearningGoalsList.this, GetChildrenList.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(myIntent);
    return;
}

class GetLearningGoalsAsync extends AsyncTask<String, Void, ArrayList<LearningGoal>>  {

    private Dialog loadingDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        loadingDialog = ProgressDialog.show(GetLearningGoalsList.this, "Please wait", "Loading...");
    }

    @Override
    protected ArrayList<LearningGoal> doInBackground(String... params) {

        int id = 0;
        email = (String) getIntent().getSerializableExtra("email");
        password = (String) getIntent().getSerializableExtra("password");
        int idChild = (int) getIntent().getSerializableExtra("idChild");
        String name = null;
        String start_date = null;
        String end_date = null;

        try {

            List<BasicNameValuePair> parameters = new LinkedList<BasicNameValuePair>();
            parameters.add(new BasicNameValuePair("idchild", Integer.toString(idChild)));

            SendRequest sr = new SendRequest();
            String result = sr.sendHttpRequest("http://" + sr.getIP_ADDRESS() + "/learningchild/list"+ "?"+ URLEncodedUtils.format(parameters, "utf-8"), "POST", true, email, password);

            String jsonResult = "{ \"learningGoals\":" + result + "}";

            Log.d("result1", jsonResult);


            //Manage JSON result
            JSONObject jsonObject = new JSONObject(jsonResult);
            JSONArray learningGoalsArray = jsonObject.getJSONArray("learningGoals");

            for (int i = 0; i < learningGoalsArray.length(); ++i) {
                JSONObject learningGoal = learningGoalsArray.getJSONObject(i);
                id = learningGoal.getInt("id");
                name = learningGoal.getString("name");
                start_date = learningGoal.getString("start_date");
                end_date = learningGoal.getString("end_date");


                childrenLearningList.add(new LearningGoal(id,name,start_date,end_date));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return childrenLearningList;

    }

    @Override
    protected void onPostExecute(final ArrayList<LearningGoal> learningListInformation) {
        loadingDialog.dismiss();
        if(learningListInformation.size() > 0) {
            CustomListLearningGoalAdapter adapter = new CustomListLearningGoalAdapter(GetLearningGoalsList.this, learningListInformation);
            itemsListView.setAdapter(adapter);
        }
        else{
            Toast.makeText(getApplicationContext(), "Impossible de récupérer la liste des scénarios de cet enfant", Toast.LENGTH_LONG).show();
        }
    }
}
}

Thanks for your help. 谢谢你的帮助。

if you want to maintain GetChildrenList state as it is then just call finish() rather than new intent on previous button click as follow 如果您想保持GetChildrenList状态不变,则只需调用finish()而不是对上一个按钮单击的新意图,如下所示

replace in GetLearningGoalsList GetLearningGoalsList中替换

@Override
public void onClick(View v) {
    Intent myIntent = new Intent(GetLearningGoalsList.this, GetChildrenList.class);
    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(myIntent);
    return;
}

with

@Override
public void onClick(View v) {
   finish();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM