按下时显示新列表的新ID

时间:2016-01-12 16:02:04

标签: android json listview android-intent android-listview

我想知道如何在按下时显示新列表id?目前,新列表会显示旧列表ID,但当我关闭应用并再次重新打开时,它会显示其ID

首先,数据将从MySQL检索并加载到android listView。 (假设它只有一个列表)。

  ListView listViewUpdate;
  String ID, iD;
  public static final int PROJECT_REQUEST_CODE = 1;
  public static final int CAMERA_REQUEST_CODE = 2;
  int mClickedPosition;
  String ReceiveProject, ReceiveDescription, ReceiveTimeIn, ReceiveTimeOut;
  Integer ReceiveProgress;
  String myJSON;
  JSONArray details = null;
  TextView totalHours;
  String MiNtimeIn,MaXtimeOut;
  List<DetailsBean> details1=new ArrayList<>();
  CustomBaseAdapter objadapter;

 public void BuildEditDetails(final String ID) { // Assume the ID is foreign key

        class GetDataJSON extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://192.168.107.115/Android/CRUD/detailsRetrieve.php?id=" + ID);


                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                } finally {
                    try {
                        if (inputStream != null) inputStream.close();
                    } catch (Exception squish) {
                    }
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result) {
                myJSON = result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }

    protected void showList() {
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            details = jsonObj.getJSONArray(Configs.TAG_RESULTS);

            for (int i = 0; i < details.length(); i++) {
                JSONObject c = details.getJSONObject(i);
                String project = c.getString(Configs.TAG_PROJECT);
                String description = c.getString(Configs.TAG_WORKDESCRIPTION);
                int percentage = c.getInt(Configs.TAG_PERCENTAGE);
                String in = c.getString(Configs.TAG_IN);
                String out = c.getString(Configs.TAG_OUT);
                iD = c.getString(Configs.TAG_ID); // its real id
                DetailsBean dbean=new DetailsBean(iD,project,description,percentage,in,out);
                details1.add(dbean);
            }
             objadapter=new CustomBaseAdapter(getActivity(),details1);
            listViewUpdate.setAdapter(objadapter);

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

当点击笔图标(mClickedPosition == - 1)时,它会打算活动B ,并在活动A中添加新列表 listView。如果按下列表**(mClickedPosition!== 1),它会显示其ID,并且意图** B 供用户编辑。

    listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> listView, View view,
                                        int position, long id) {
                    mClickedPosition = position;
                    String iD = details1.get(position).getID();
                    Intent intent = new Intent(getActivity(), Edit_Details.class);
                    intent.putExtra("iD", iD);
                    intent.putExtra("ID", ID);
                    intent.putExtra("mClickedPosition", mClickedPosition);
                    Toast.makeText(getActivity(), "This is" + iD + ID, Toast.LENGTH_LONG).show();
                    startActivityForResult(intent, PROJECT_REQUEST_CODE);

                }
            });


 @Override public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.addDetails:
                mClickedPosition = -1;
                Intent intent = new Intent(getActivity(), Edit_Details.class);
                intent.putExtra("ID", ID);
               // intent.putExtra("iD", iD);
                startActivityForResult(intent, PROJECT_REQUEST_CODE);
                break;

OnActivityResult活动A

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == PROJECT_REQUEST_CODE) {
                ReceiveProject = data.getStringExtra("project1");
                ReceiveDescription = data.getStringExtra("description");
                ReceiveProgress = data.getIntExtra("progress", 0);
                ReceiveTimeIn = data.getStringExtra("timeIn");
                ReceiveTimeOut = data.getStringExtra("timeOut");
                if(mClickedPosition==-1)
                { // add list
                    if(objadapter!=null)
                    {
                        objadapter.addNewItem(iD,ReceiveProject,ReceiveDescription,ReceiveProgress,ReceiveTimeIn,ReceiveTimeOut);
                    }
                }
                else
                {  // update list
                    if(objadapter!=null)
                    {
                        objadapter.changeItem(mClickedPosition,iD,ReceiveProject,ReceiveDescription,ReceiveProgress,ReceiveTimeIn,ReceiveTimeOut);

                    }
                }
            }
        }

最后是CustomBaseAdapter

public class CustomBaseAdapter extends ArrayAdapter<DetailsBean>{   // for ListView


    Activity context;
    List<DetailsBean> details;

    public CustomBaseAdapter(Activity context,List<DetailsBean> details) {
        super(context, R.layout.retrieve_details, details);
        this.context = context;
        this.details = details;
    }

    public void changeItem(int m,String ID,String Project,String Description,int Percentage,String in,String out)
    {
        DetailsBean obj = new DetailsBean(ID, Project, Description, Percentage, in, out);
        obj.setProject(Project);
        obj.setProgress(Percentage+"");
        obj.setTimeIn(in);
        obj.setTimeOut(out);
        obj.setDescription( Description);
        details.set(m,obj);
        this. notifyDataSetChanged();
    }

    public void addNewItem(String ID,String Project,String Description,int Percentage,String in,String out) {
        DetailsBean obj = new DetailsBean(ID, Project, Description, Percentage, in, out);
        obj.setProject(Project);
        obj.setProgress(Percentage+"");
        obj.setTimeIn(in);
        obj.setTimeOut(out);
        obj.setDescription( Description);
        details.add(obj);
        this.notifyDataSetChanged();
    }
  

新列表已添加到上一个列表下方,但是当我点击时   新的列表,它仍然显示旧的列表ID ..我怎么能做到   显示其ID?感谢

当来到这个类时,它从MySQL中检索值

enter image description here

当我点击1个列表或2个列表时,它会显示其ID,(1和2)

现在我添加了一个新列表,现在列出了3个

enter image description here

当我点击第三个列表时,它假设显示3但显示2

2 个答案:

答案 0 :(得分:1)

尝试使用适配器CustomBaseAdapter类:

public List<DetailsBean> getDetails(){
return details;
}
onItemClick中的

  @Override
                 public void onItemClick(AdapterView<?> listView, View view,
                                        int position, long id) {
                    mClickedPosition = position;
                    String iD = listViewUpdate.getDetails().get(position).getID();

//so check id here
....

检查onActivityResult

什么是iD?这个变量的价值是什么?

objadapter.addNewItem(iD,ReceiveProject,ReceiveDescription,ReceiveProgress,ReceiveTimeIn,ReceiveTimeOut

答案 1 :(得分:1)

  

按下时显示新列表的新ID

使用String jsonStr = MyApplication.getmInstance().getJsonStr(); onPostExecute获取最新ID并将数据发送回上一个Activity.like:

AddMore