Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can someone please please please help me to check what's wrong with my code ? In my **Activity A**, it has a `listView` which support multi-line.Value from **B** will be returned to **A** and add new list. Now I only able to have one list. If there are another value return to A, it will update the previous list instead of ADD A NEW ONE !

It's really driving me nuts!! Can someone help me to figure out what's wrong here ? Highly Appreciated !

Activity B

Java
save.setOnClickListener(new View.OnClickListener()
           {  // return values to previous activity
               @Override
               public void onClick(View v)
               {
                   Intent returnIntent=new Intent();
                   Project=project.getSelectedItem().toString();
                   Description=description.getText().toString();
                   progress=seekBar.getProgress();
                   returnIntent.putExtra("Project",Project);
                   returnIntent.putExtra("Description", Description);
                   returnIntent.putExtra("progress", progress);
                   Toast.makeText(getApplicationContext(), progress+"", Toast.LENGTH_LONG).show();
                   returnIntent.putExtra("TimeIn", TimeIn);
                   returnIntent.putExtra("TimeOut",TimeOut);
                   setResult(Activity.RESULT_OK,returnIntent);
                   finish();

               }
           });

Activity A

int mClickedPosition;

    @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));

           }
       }

           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);
               }
               else
               {


               }
           return results;

       }
   }


**MyCustomBaseAdapter.java**

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;
          }
      }


**SearchResult.java**

public class SearchResults {

      private String weather = "";
      private String date = "";
      private String status = "";
      private String timeIn="";
      private String timeOut="";
      private String project="";
      private String description="";
      private String progress="";

      public void setWeather(String weather) {
          this.weather = weather;
      }

      public String getWeather() {
          return weather;
      }

      public void setDate(String date) {
          this.date = date;
      }

      public String getDate() {
          return date;
      }

      public void setStatus(String status) {
          this.status = status;
      }

      public String getStatus() {
          return status;
      }

      public void setTimeIn(String timeIn) {
          this.timeIn = timeIn;
      }

      public String getTimeIn() {
          return timeIn;
      }

      public void setTimeOut(String timeOut){
         this.timeOut=timeOut;
      }

      public String getTimeOut()
      {
          return timeOut;
      }

      public void setProject(String project){
          this.project=project;
      }

      public String getProject()
      {
          return project;
      }

      public void setProgress(String progress){
          this.progress=progress;
      }

      public String getProgress()
      {
          return progress;
      }

      public void setDescription(String description){
          this.description=description;
      }

      public String getDescription()
      {
          return description;
      }

  }


**The value from B can return to A, but it updates the list instead of add a new list.**
Posted
Updated 29-Nov-15 2:11am
v2
Comments
ridoy 29-Nov-15 11:12am    
Its too tough to examine the huge code. You need to precisely tell what lines of code cause the problem

1 solution

Not sure if it will work but try clearing the adapter I don't see you removing or clearing the adapter just saw a notfiydatasetchaged. Might have Ovelooked it but still. If you don't delete What is already in the adapter then all it does is appened the new value with the old.

In case you don't know how to clear it should be

adapter.clear();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900