如何编辑以前的listView而不是添加新的?

时间:2015-11-30 15:42:53

标签: android listview android-listview

我在活动A中有一个图标button和一个listView

图标按钮----->意图B,将值返回A,添加到新列表

ListView点击 - >意图B,将更改后的值返回listView

我使用mClickedPosition=-1;来验证图标是否被点击或listView,,但即使点击了listView,它仍会添加到新列表中。

活动A

      @Override
        public boolean onOptionsItemSelected(MenuItem item) {
             switch (item.getItemId()) {
                case R.id.addDetails:
                    mClickedPosition=-1; // if icon is clicked
                    View menuItemView = findViewById(R.id.addDetails);
                    PopupMenu po = new PopupMenu(this, menuItemView); //for drop-down menu
                    po.getMenuInflater().inflate(R.menu.popup_details, po.getMenu());
                    po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                    Toast.makeText(getApplication(), "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                    if ("Add Work Details".equals(item.getTitle())) 
                    {
                       Intent intent = new Intent(getApplication(), B.class);  // go to Activity B
                       startActivityForResult(intent, PROJECT_REQUEST_CODE);
                     }
                        return true;
                      }
                        });
                     po.show(); //showing popup menu
                }
                return super.onOptionsItemSelected(item);

            }

   listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { // if listView is clicked
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                mClickedPosition=position;
                Object o = listview.getItemAtPosition(position);
                SearchResults fullObject = (SearchResults) o;
                String temp = fullObject.getDescription();
                String Project=fullObject.getProject();
                String [] ReceiveProject=Project.split(":");
                String[] ReceiveDescription = temp.split(":");
                Intent i = new Intent(getApplication(), B.class);
                i.putExtra("ReceiveProject", ReceiveProject[1].trim());
                i.putExtra("ReceiveDescription", ReceiveDescription[1]);
                startActivityForResult(i,PROJECT_REQUEST_CODE);
            }
        });
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
            ReceiveProject = data.getStringExtra("Project");
            ReceiveDescription = data.getStringExtra("Description");
            if(mClickedPosition==-1) {  // if icon clicked
                MyCustomBaseAdapter objMyCustomBaseAdapter = (MyCustomBaseAdapter) listview.getAdapter();
                objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut); // add list
            }
            else
            {
                // change the current list
            }

        }

MyCustomBaseAdapter

public class MyCustomBaseAdapter extends BaseAdapter{   // for ListView in Activity A


        private static ArrayList<SearchResults> searchArrayList;

        private LayoutInflater mInflater;

        public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
            searchArrayList = results;
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return searchArrayList.size();
        }



        public Object getItem(int position) {
            return searchArrayList.get(position);
        }

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

       public void addNewItem(String P,String D,int Per,String I,String O)
      {
        SearchResults obj=new SearchResults();
        obj.setProject(" Project/Service/Training : "+P);
        obj.setDescription(" Work Description : " + D);
        searchArrayList.add(obj);
        this. notifyDataSetChanged();
    }



        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.custom_row_view, null);
                holder = new ViewHolder();
                holder.txtProject= (TextView) convertView.findViewById(R.id.ListProject);
                holder.txtDescription = (TextView) convertView.findViewById(R.id.ListDescription);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtProject.setText(searchArrayList.get(position).getProject());
            holder.txtDescription.setText(searchArrayList.get(position).getDescription());
            return convertView;
        }

        static class ViewHolder {
            TextView txtProject;
            TextView txtDescription;

        }
    }

1 个答案:

答案 0 :(得分:0)

1.点击特定药水的对象,点击

 SearchResults obj=searchArrayList.get(postion);

2.现在设置这样的值

 obj.setProject(" Project/Service/Training : "+P);
 obj.setDescription(" Work Description : " + D);

3.最后通知适配器。

this. notifyDataSetChanged();