title: Avoid duplicates in list
body: <p>I am trying to avoid duplicate values in my list but it's not working. Can you help me for this? I did a lot of work on it, but I'm new to android and also coding. Can you help me where I have made my mistake?</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class Dynamic extends Activity {
  
      Button bt, bt2, ent, clr;
      EditText et, et2;
      String input, input1;
      public static ArrayList&lt;String&gt; k_listItems = new ArrayList&lt;String&gt;();
      public static ArrayList&lt;String&gt; p_listItems = new ArrayList&lt;String&gt;();
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          bt = (Button) findViewById(R.id.nameok);
          et = (EditText) findViewById(R.id.name);
  
          input = et.getText().toString();
  
          bt.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
  
                  if (et.getText().length() == 0) {
                      Context context = getApplicationContext();
                      CharSequence text = "Enter Your Name";
                      int duration = Toast.LENGTH_SHORT;
  
                      Toast toast = Toast.makeText(context, text, duration);
                      toast.show();
                  } else {
  
                      {
                          for (int i = 0; i &lt; k_listItems.size(); i++) {
                              if (k_listItems.get(i).equals("input")) {
                                  Intent in = new Intent(Dynamic.this, Dynamic.class);
                                  startActivity(in);
                                  Toast.makeText(getApplicationContext(), "Name Already Exists", Toast.LENGTH_SHORT).show();
                                  break;
                              }
                          }
                      }
  
                      if (et.getText().length() == 0) {
                          Context context = getApplicationContext();
                          CharSequence text = "Enter Your Number";
                          int duration = Toast.LENGTH_SHORT;
                          Toast toast = Toast.makeText(context, text, duration);
                          toast.show();
                      } else {
  
                          et2.setVisibility(View.VISIBLE);
                          bt2.setVisibility(View.VISIBLE);
                          bt2.setOnClickListener(new View.OnClickListener() {
  
                              @Override
                              public void onClick(View v) {
                                  // TODO Auto-generated method stub
                                  if (et2.getText().length() == 0) {
                                      Context context = getApplicationContext();
                                      CharSequence text = "Enter Your Number";
                                      int duration = Toast.LENGTH_SHORT;
                                      Toast toast = Toast.makeText(context, text, duration);
                                      toast.show();
                                  } else {
                                      ent.setVisibility(View.VISIBLE);
                                      clr.setVisibility(View.VISIBLE);
                                      ent.setOnClickListener(new View.OnClickListener() {
  
                                          @Override
                                          public void onClick(View v) {
                                              if (null != input &amp;&amp; input.length() &gt; 0) {
                                                  k_listItems.add(input);
                                              }
                                              if (null != input1 &amp;&amp; input1.length() &gt; 0) {
                                                  p_listItems.add(input1);
                                              }
                                              input = et.getText().toString();
                                              input1 = et2.getText().toString();
  
                                              Intent in = new Intent(Dynamic.this, GetList.class);
                                              in.putExtra("input", input);
                                              in.putExtra("input1", input1);
                                              startActivity(in);
                                          }
                                      });
                                      clr.setOnClickListener(new View.OnClickListener() {
  
                                          @Override
                                          public void onClick(View v) {
                                              Intent in4 = new Intent(Dynamic.this, Dynamic.class);
                                              startActivity(in4);
                                          }
                                      });
                                  }
                              }
                          });
  
                      }
                  }
              }
          });
      }
  
      protected void onStart() {
          super.onStart();
  
          et2 = (EditText) findViewById(R.id.number);
          bt2 = (Button) findViewById(R.id.numberok);
          ent = (Button) findViewById(R.id.enter);
          clr = (Button) findViewById(R.id.clear);
      }
  
      public void onBackPressed() {
          Intent startMain = new Intent(Intent.ACTION_MAIN);
          startMain.addCategory(Intent.CATEGORY_HOME);
          startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(startMain);
      }
  
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>Mylist to get that list...</p>
  
  <pre><code>import java.util.ArrayList;
  
  
  public class GetList extends Activity {
  
      ListView lv;
      Button bt3;
      Dialog d;
      EditText edt;
      String x;
      public static ArrayList&lt;String&gt; x_listItems = new ArrayList&lt;String&gt;();
      public static ArrayList&lt;String&gt; y_listItems = new ArrayList&lt;String&gt;();
      String inp, inp1;
  
      public static ArrayAdapter&lt;String&gt; a_adapter;
  
      public void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.list);
  
          lv = (ListView) findViewById(R.id.listView1);
  
          Intent in = getIntent();
  
          inp = in.getStringExtra("input");
          inp1 = in.getStringExtra("input1");
  
          a_adapter = new ArrayAdapter&lt;String&gt;(this,
                  android.R.layout.simple_list_item_1, x_listItems);
  
          lv.setAdapter(a_adapter);
  
          if (null != inp &amp;&amp; inp.length() &gt; 0) {
              x_listItems.add(inp);
              a_adapter.notifyDataSetChanged();
          }
          if (null != inp1 &amp;&amp; inp1.length() &gt; 0) {
              y_listItems.add(inp1);
              Log.v("num", "" + y_listItems);
          }
          lv.setOnItemClickListener(new OnItemClickListener() {
              @SuppressWarnings("deprecation")
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View v, int position,
                                      long arg3) {
                  if (x_listItems.get(position) != null) {
                      AlertDialog ab = new AlertDialog.Builder(
                              GetList.this).create();
                      ab.setTitle("Details");
                      ab.setMessage("NAME:" + x_listItems.get(position) + "\n" + "Number" + y_listItems.get(position));
                      ab.setButton("OK", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              Toast.makeText(getApplicationContext(), "Thanx for visiting", Toast.LENGTH_SHORT).show();
                          }
                      });
                      ab.show();
                  }
              }
          });
          lv.setOnItemLongClickListener(new OnItemLongClickListener() {
              @SuppressWarnings("deprecation")
              @Override
              public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View v,
                                             final int position, long arg3) {
                  if (x_listItems.get(position) != null) {
                      AlertDialog ab = new AlertDialog.Builder(
                              GetList.this).create();
                      ab.setTitle("Slect Your Option");
                      ab.setButton("Edit", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              final String z = x_listItems.get(position);
                              Log.v("position", "" + z);
                              d = new Dialog(GetList.this);
                              d.setContentView(R.layout.abc);
                              d.setTitle("Enter new name");
                              Button sub = (Button) d.findViewById(R.id.submit);
                              edt = (EditText) d.findViewById(R.id.edt);
                              edt.setText(x_listItems.get(position));
  
                              sub.setOnClickListener(new OnClickListener() {
                                  public void onClick(View v) {
                                      x_listItems.remove(position);
                                      x = edt.getText().toString();
                                      x_listItems.add(position, x);
                                      String p = x_listItems.get(position);
                                      String f = y_listItems.get(position);
                                      y_listItems.set(position, f);
                                      Log.v("changednum", "" + inp1);
                                      a_adapter.notifyDataSetChanged();
                                      lv.setAdapter(a_adapter);
                                      Toast.makeText(getApplicationContext(), "" + edt.getText().toString().trim(), 4000).show();
                                      d.dismiss();
                                  }
                              });
                              d.show();
                          }
                      });
  
                      ab.setButton2("Delete", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              x_listItems.remove(position);
                              y_listItems.remove(position);
                              a_adapter.notifyDataSetChanged();
                              lv.setAdapter(a_adapter);
                          }
                      });
                      ab.show();
                  }
                  return true;
              }
  
              private int get(int position) {
                  return 0;
              }
          });
      }
  
      public void onBackPressed() {
          super.onBackPressed();
          Intent in4 = new Intent(GetList.this, Dynamic.class);
          startActivity(in4);
      }
  }
  </code></pre>