按下时显示新列表的新 ID

Posted

技术标签:

【中文标题】按下时显示新列表的新 ID【英文标题】:Show new id for newly list when it is pressed 【发布时间】:2016-04-17 08:13:50 【问题描述】:

我想知道按下时如何显示新列表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),它会意图Activity B,并在Activity 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 中检索值

当我点击 1 个列表或 2 个列表时,它会显示它们的 id,(1 和 2)

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

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

【问题讨论】:

您的意思是,notifyDataSetChanged() 不会更新您的列表视图? @Vyacheslav no..我可以看到列表视图中添加了一个新列表,但是当我单击新列表时,它会显示旧列表 ID。如果我关闭应用程序并重新打开它,只有它会显示它的 id 【参考方案1】:

按下时显示新列表的新ID

使用AddMore 类的onPostExecute 获取最新的id 并将数据发送回之前的Activity.like:

@Override 
protected void onPostExecute(String s)  
 super.onPostExecute(s); 
 loading.dismiss();
 try 
  JSONObject jsonObject=new JSONObject(s);
  int latestID=jsonObject.optInt("lastId");
  //call setResult here 
  ....
  returnIntent.putExtra("project1", project1); 
  returnIntent.putExtra("description", description); 
  returnIntent.putExtra("progress", progress); 
  returnIntent.putExtra("timeIn", timeIn); 
  returnIntent.putExtra("iD", latestID);  //<<<
 catch(Exception ex)

 
 

【讨论】:

【参考方案2】:

尝试在适配器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

【讨论】:

它们听起来一样吗?我在 CustomBaseAdapter 中有这个 public DetailsBean getItem(int position) return details.get(position); 我使用你的代码,新列表仍然显示旧列表 id :( @Tony ,调试模式下显示哪些值?您在onItemClick 中看到旧列表了吗? 是的,我看到了。再次查看我的帖子。从Activity B(Edit_Details)获取新值并返回到onActivityResultActivity A

以上是关于按下时显示新列表的新 ID的主要内容,如果未能解决你的问题,请参考以下文章

按下单元格中的按钮时显示新视图

如何使用 CSS 在每次闪烁时显示新文本

按下底部颤动时显示警报

在按下 DetailDisclosureButton 时显示 UIViewController

按下热键时显示一个对话框实例

按下按钮时显示 ViewController,同时还存在 tabBar