如何将新列表添加到listView? [英] How to add new list to listView?

查看:113
本文介绍了如何将新列表添加到listView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人请帮助我查看我的代码有什么问题?在我的**活动A **中,它有一个支持多行的`listView`。来自** B **的值将返回到** A **并添加新列表。现在我只能有一个列表。如果有另一个值返回A,它将更新上一个列表而不是添加一个新的



这真的让我疯狂!有人可以帮我弄清楚这里有什么问题吗?非常感谢!



活动B



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

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

               }
           });



活动A




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 **





**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 **





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

  }





** B的值可以返回A,但它更新列表而不是添加新列表。**



**The value from B can return to A, but it updates the list instead of add a new list.**

推荐答案

不确定它是否可行但是尝试清除适配器我看不到你删除或清除适配器只是看到一个notfiydatasetchaged。可能已经Ovelooked但仍然。如果你没有删除适配器中已有的内容,那么它所做的就是使用旧的值添加新值。



如果您不知道如何明确它应该是



adapter.clear();
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();


这篇关于如何将新列表添加到listView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆