2013-02-12 102 views
0

我想避免重複值在我的列表中,但它不工作。你能幫我解決嗎?我在這方面做了很多工作,但我對android和編碼都很陌生。你能幫助我,我犯了我的錯誤嗎?避免列表中的重複項

import java.util.ArrayList; 

public class Dynamic extends Activity { 

    Button bt, bt2, ent, clr; 
    EditText et, et2; 
    String input, input1; 
    public static ArrayList<String> k_listItems = new ArrayList<String>(); 
    public static ArrayList<String> p_listItems = new ArrayList<String>(); 

    @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 < 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 && input.length() > 0) { 
               k_listItems.add(input); 
              } 
              if (null != input1 && input1.length() > 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; 
    } 

} 

MYLIST來獲取列表...

import java.util.ArrayList; 


public class GetList extends Activity { 

    ListView lv; 
    Button bt3; 
    Dialog d; 
    EditText edt; 
    String x; 
    public static ArrayList<String> x_listItems = new ArrayList<String>(); 
    public static ArrayList<String> y_listItems = new ArrayList<String>(); 
    String inp, inp1; 

    public static ArrayAdapter<String> 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<String>(this, 
       android.R.layout.simple_list_item_1, x_listItems); 

     lv.setAdapter(a_adapter); 

     if (null != inp && inp.length() > 0) { 
      x_listItems.add(inp); 
      a_adapter.notifyDataSetChanged(); 
     } 
     if (null != inp1 && inp1.length() > 0) { 
      y_listItems.add(inp1); 
      Log.v("num", "" + y_listItems); 
     } 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      @SuppressWarnings("deprecation") 
      @Override 
      public void onItemClick(AdapterView<?> 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<?> 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); 
    } 
} 

回答

2

你爲什麼不使用Set,而不是List?他們不允許重複。如果您將自己的對象放在那裏,您將需要一個hashCode和equals方法,但它應該適用於字符串

+0

不錯,但我不知道在實習位置設置 – Ramz 2013-02-12 06:00:44

+0

Im和我的TL嚴格的說,我應該使用ArrayList – Ramz 2013-02-12 06:09:44

1

HashSet不允許重複值。它提供了add方法而不是put方法。您還可以使用它的contains方法來檢查對象是否已經在HashSet中可用。 HashSet可用於您想要維護唯一列表的地方。

HashSet<String> stateSet = new HashSet<String>(); 
stateSet.add ("CA"); 
stateSet.add ("WI"); 
stateSet.add ("NY"); 

if (stateSet.contains("PB")) /* if CA, it will not add but shows following message*/ 
    System.out.println("Already found"); 
else 
    stateSet.add("PB"); 
+0

我可以甲肝這個HashSet的我從來沒有使用過小說明在 – Ramz 2013-02-12 06:01:40

+0

林在一個見習位置,我的TL嚴格地說,我假設使用Arraylist – Ramz 2013-02-12 06:10:02

3

設置將不允許重複...查看下面

HashSet<String> x_hashSet = new HashSet<String>(); 
HashSet<String> y_hashSet = new HashSet<String>(); 

x_hashSet.addAll(x_listItems); 
y_hashSet.addAll(y_listItems); 

x_listItems.clear(); 
y_listItems.clear(); 

x_listItems.addAll(x_hashSet); 
y_listItems.addAll(y_hashSet); 

例如然後加入列表適配器。而已。

希望這會幫助你;)