title: Dynamically adding listitems to baseadapter an android
body: <p>I want to dynamically add new listitems by pressing a button.but i don't no the right way to do this. </p>
  
  <p><strong>BaseAdapter</strong></p>
  
  <pre><code>public class CompetitionResultListAdapter extends BaseAdapter{
  
      private Activity activity;
      private ArrayList&lt;Result&gt; result;
      DataManager datamanager;
  
  
      @Override
      public int getCount() {
          return result!=null ? result.size() : 0;
      }
  
      @Override
      public Object getItem(int position) {
          return result.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if(convertView==null){
              // inflate the layout
              LayoutInflater inflater = activity.getLayoutInflater();
              convertView = inflater.inflate(R.layout.list_item_competition_result, parent, false);
          }
          EditText listItemDate = (EditText) convertView.findViewById(R.id.date);
          EditText listItemPlace = (EditText) convertView.findViewById(R.id.place);
          EditText listItemComment = (EditText) convertView.findViewById(R.id.comment);
          result.get(position).setDate(listItemDate.getText().toString());
          result.get(position).setPlace(listItemPlace.getText().toString());
          result.get(position).setComment(listItemComment.getText().toString());
  
  
          return convertView;
      }
  
      public void refreshResultList(ArrayList&lt;Result&gt; result){
          this.result = result;
          notifyDataSetChanged();
  
      }
  
  }
  </code></pre>
  
  <p><strong>java class</strong></p>
  
  <pre><code>public class AddCompetitionResult extends Fragment {
  
      private CompetitionResultListAdapter competitionResultListAdapter;
      private DataManager datamanager;
      ArrayList&lt;Result&gt; result;
      ListView listView;
      private Horse horse;
      private Button addResults;
      int clickCounter=0;
  
      public AddHorseCompetitionResult(Horse horse) {
          this.horse = horse;
      }
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.add_horse_competition_result,
                  null);
  
          addResults = (Button) view.findViewById(R.id.btn_add_competition_result);
  
          listView = (ListView) view.findViewById(R.id.data_list);
          // result = datamanager.create(getActivity());
  
          competitionResultListAdapter = new CompetitionResultListAdapter();
  
          View footerView = ((LayoutInflater) getActivity().getSystemService(
                  Context.LAYOUT_INFLATER_SERVICE)).inflate(
                  R.layout.competition_result_listview_footer, null, false);
  
          listView.addFooterView(footerView);
  
          listView.setAdapter(competitionResultListAdapter);
  
          addResults.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  String resultId = null;
          //      result.add();
                  competitionResultListAdapter.notifyDataSetChanged();
  
              }
          });
  
          listView.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  if (competitionResultListAdapter.getCount() == position) {
                      MainActivity mainActivity = MainActivity.getInstance();
                      mainActivity.setFragment(new AddHorseDetailedInfo(horse),
                              "Add horse more info");
                      // datamanager.createResult(result);
  
                  } else {
                      // ContactGroup cg = (ContactGroup)
                      // competitionResultListAdapter.getItem(position);
                      // MainActivity mainActivity = MainActivity.getInstance();
                      // mainActivity.setFragment(new
                      // ContactListView(cg.getGroupId()), "contacts");
                  }
              }
  
          });
  
          return view;
  
      }
  }
  </code></pre>