如何在更新ArrayList中的第一条记录后更新第二条记录

时间:2018-03-08 05:05:02

标签: android service

在我的Android应用程序中Two Activities,我的第一个Activity包含TicketId的票证列表和Update button此处点击更新按钮后,它将转到{ {1}}并且有用于更新数据的文本字段和用于提交的按钮。当我点击该按钮时,数据已更新,并再次返回Activity2。    这是我的主要问题当我点击花药记录的更新按钮我的应用程序关闭时不幸停止错误下面是我的代码。

这是我的第一个活动:

Activity1

列表适配器:

       public void loadHeroList() {

        //getting the progressbar
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
        //making the progressbar visible
        progressBar.setVisibility(View.VISIBLE);

        StringRequest stringRequest= new StringRequest(Request.Method.GET, "http://182.18.163.39/train/m/list_details.php?username="+user+"&key="+pass,
                new Response.Listener<String>(){
                    @Override
                    public void onResponse(String response) {
                        //hiding the progressbar after completion
                        progressBar.setVisibility(View.INVISIBLE);

                        try {

                            JSONArray jsonarray = new JSONArray(response);
                            for (int i = 0; i < jsonarray.length(); i++) {
                                JSONObject jsonobject = jsonarray.getJSONObject(i);
                                String name = jsonobject.getString("Sno");
                                String Tktid = jsonobject.getString("TKTID");
                                link = jsonobject.getString("Link");
                                status = jsonobject.getString("Status");

                                List list = new List(user, pass, jsonobject.getString("Sno"), jsonobject.getString("TKTID"),jsonobject.getString("Link"),jsonobject.getString("Status"));
                                tktList.add(list);

                                Log.i("website content", name);
                                Log.i("website content", Tktid);
                                Log.i("website content", link);

                            }

                            //creating custom adapter object
                            ListViewAdapter adapter = new ListViewAdapter(tktList, getApplicationContext());

                            //adding the adapter to listview
                            listView.setAdapter(adapter);

//                            adapter = (ListViewAdapter) listView.getAdapter();
//                            List item = adapter.getItem(0);
//                            adapter.remove(item);
//                            adapter.notifyDataSetChanged();

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

我的第二项活动:

 final Button btupdate = listViewItem.findViewById(R.id.btupdate);
//        Button btview = listViewItem.findViewById(R.id.btview);
        final List hero = tktList.get(position);

        btupdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(mCtx, Main2Activity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                i.putExtra("Link", hero.getLink());
                i.putExtra("Status", hero.getStatus());
                i.putExtra("username", hero.getUsername());
                i.putExtra("password", hero.getPassword());
                mCtx.startActivity(i);

            }

        });

        //Getting the hero for the specified position
        //setting hero values to textviews
        textViewName.setText(hero.getSno());
        textViewImageUrl.setText(hero.getTktid());
        //returning the listitem


        return listViewItem;
    }

这是我的错误日志:

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

        btnupdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                etupdate = ((EditText) findViewById(R.id.EditTextUpdate)).getText().toString();
                action = etupdate;

                ticketid = message;

                if (status.equalsIgnoreCase("Pending")) {

                    try {
                        action = URLEncoder.encode(action,"UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    try {
                        int l = etupdate.length();
                        if (l != 0) {
                            url = "http://182.18.163.39/train/m/action.php?username=" + username + "&key=" + password + "&tktid=" + ticketid + "==&action=" + action;
                        } else
                            url = "http://182.18.163.39/train/m/action.php?username=" + username + "&key=" + password + "&tktid=" + ticketid + "==&action=";

                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpPost = new HttpGet(url);
                        HttpResponse response = httpClient.execute(httpPost);
                        HttpEntity entity = response.getEntity();

                        String responseString = EntityUtils.toString(entity);

                        JSONArray jsonarray = new JSONArray(responseString);
                        JSONObject jsonObj = new JSONObject();
                        jsonObj.put("Result", jsonarray);
                        String msg = jsonObj.getJSONArray("Result").getJSONObject(0).getString("Error");

                        String Successmsg = "Deficiency updated successfully.";

                        if (msg.equalsIgnoreCase(Successmsg)) {

                            onUpdatePressed(view);

                        } else {
                            Toast.makeText(getApplicationContext(), "failed to update", Toast.LENGTH_SHORT).show();

                        }
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "Network Failed", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(getApplicationContext(), "Already updated you are not allow to update", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void onUpdatePressed(View view) {

//        Intent i = new Intent(this, MainActivity.class);
//        i.putExtra("UserName",username);
//        i.putExtra("PassWord",password);
//        startActivity(i);
        Toast.makeText(Main2Activity.this, "Successfully Updated", Toast.LENGTH_SHORT).show();
        MainActivity m = new MainActivity();
        m.loadHeroList();
        super.onBackPressed();
    }

4 个答案:

答案 0 :(得分:0)

尝试将您的webservice调用放入异步任务

将以下代码放在doInBackground

<h1>Form Name</h1>

答案 1 :(得分:0)

您无法使用主线程发出网络请求。你必须将网络电话包裹在后台任务上,例如Asynctask只是使用接口回调来获得结果。

答案 2 :(得分:0)

onResume方法中的主要活动,如下面的代码所示。

@Override
protected void onResume() {
    super.onResume();
    loadHeroList();
}

并删除onUpdatePressed方法中的代码,如下所示

 MainActivity m = new MainActivity();
    m.loadHeroList();

只调用super.onBackPresss然后调用onResume方法

中的第一个活动

答案 3 :(得分:0)

您可以在单击btnUpdate时从列表中删除更新数据。

       tktList.remove(tktList.indexOf(hero));

然后通知适配器。