为什么无法在listView中添加新列表?

时间:2015-11-28 15:47:06

标签: android listview android-listview

我的活动中有listView和图标button。点击图标后,应该转到活动B ,最后通过添加新列表将活动B 值返回 A 。但现在问题是我只能创建一个列表。当它有另一个值返回A时,它会更新旧列表而不是添加新列表。

我从here获得了教程,区别在于我使用startActivityForResult将活动B的值返回给A.

请帮助我..我一整天都被困在这里......

活动A

 int mClickedPosition; // add list

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.addDetails:
                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())) {
                mClickedPosition = -1;
                Intent intent = new Intent(getApplication(), Add_Details_Information.class);  // go to Details class
                startActivityForResult(intent, PROJECT_REQUEST_CODE);
                }
                   return true;
                   }
                });
                po.show(); //showing popup menu
             }
                 return super.onOptionsItemSelected(item);

    }

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Add_Details_Information
       if(requestCode==PROJECT_REQUEST_CODE)
       {
                ReceiveProject = data.getStringExtra("Project");
                ReceiveDescription = data.getStringExtra("Description");
                ReceiveProgress = data.getIntExtra("progress",0);
                ReceiveTimeIn = data.getStringExtra("TimeIn");
                ReceiveTimeOut = data.getStringExtra("TimeOut");
                ArrayList<SearchResults> searchResults = GetSearchResults(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);
                (new MyCustomBaseAdapter(this,searchResults)).notifyDataSetChanged();
                listview.setAdapter(new MyCustomBaseAdapter(this,searchResults));
        }
        else if(requestCode==CAMERA_REQUEST_CODE)
       {

       }
    }

    private ArrayList<SearchResults> GetSearchResults(String p, String d,int pro, String i, String o) {
        ArrayList<SearchResults> results = new ArrayList<SearchResults>();
        SearchResults sr1=new SearchResults();
        sr1.setProject(" Project/Service/Training : " + p);
        sr1.setDescription(" Description : " + d);
        sr1.setProgress(" Progress : " + pro);
        sr1.setTimeIn(" Time In : " + i);
        sr1.setTimeOut(" Time Out : " + o);
        if(mClickedPosition==-1)
        {
            results.add(sr1);
        }
        return results;

    }

MyCustomBaseAdapter

public class MyCustomBaseAdapter extends BaseAdapter{   // for ListView in WorkDetailsTable


        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 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);
                holder.txtProgress = (TextView) convertView.findViewById(R.id.ListProgress);
                holder.txtIn=(TextView)convertView.findViewById(R.id.ListTimeIn);
                holder.txtOut=(TextView)convertView.findViewById(R.id.ListTimeOut);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtProject.setText(searchArrayList.get(position).getProject());
            holder.txtDescription.setText(searchArrayList.get(position).getDescription());
            holder.txtProgress.setText(searchArrayList.get(position).getProgress());
            holder.txtIn.setText(searchArrayList.get(position).getTimeIn());
            holder.txtOut.setText(searchArrayList.get(position).getTimeOut());

            return convertView;
        }

        static class ViewHolder {
            TextView txtProject;
            TextView txtDescription;
            TextView txtProgress;
            TextView txtIn;
            TextView txtOut;
        }
    }

1 个答案:

答案 0 :(得分:0)

目前,不是在ListView中添加新项目,而是将适配器的新对象传递给setAdapter,这将仅显示ListView中的最新添加项目,而不是添加到当前适配器中。

这样做:

1。MyCustomBaseAdapter类中创建一个方法,用于在数据源中添加从getCount方法返回大小的新项目:

public void addNewItem(String ReceiveProject,String ReceiveDescription,...){
  // add ReceiveProject,ReceiveDescription 
  // in ArrayList which using in Adapter

  SearchResults objSearchResults=new SearchResults();
  objSearchResults.setProject(ReceiveProject);
  ....
  searchArrayList.add(objSearchResults);
  this. notifyDataSetChanged();
}

2。使用第一次通过MyCustomBaseAdapter方法传递的setAdapter类的相同对象来调用addNewItem方法:

if(requestCode==PROJECT_REQUEST_CODE)
   {
      MyCustomBaseAdapter objMyCustomBaseAdapter=
                      (MyCustomBaseAdapter)listview.getAdapter();     
   }