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

时间:2017-06-08 08:07:35

标签: android http request

我目前有两个活动正在处理HTTP请求。

第一个活动包含一个CustomList类extends BaseAdapter。

第二个,有一个前一个按钮,允许我返回第一个活动。

回到第一个活动,我希望能够恢复我离开它的状态。也就是说能够找到也来自HTTP请求的信息。我想在第一个活动中找到数据“infos_user”,在BaseAdapter中找到 all 数据。

我的架构如下:活动0(HTTP请求) - >活动1(使用BaseAdapter和HTTP请求) - >活动2(HTTP请求)

我把所有代码都放了,因为我真的不知道怎么能这样做:/

第一项活动:

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:

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

}
}

第二项活动:

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果你想维持 GetChildrenList 状态,那么只需调用完成()而不是上一次按钮点击的新意图,如下所示

替换 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;
}

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