避免重复列表

时间:2013-02-12 05:14:12

标签: android arraylist duplicates

我正在尝试避免列表中的重复值,但它无法正常工作。你可以帮帮我吗?我做了很多工作,但我是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);
    }
}

3 个答案:

答案 0 :(得分:4)

设置不允许重复...请参阅下面的示例

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

然后将列表添加到适配器。就是这样。

希望这会对你有帮助;)

答案 1 :(得分:2)

为什么不使用Set代替List?他们不允许重复。如果你把自己的对象放在那里,你将需要一个hashCode和equals方法,但它应该适用于字符串

答案 2 :(得分:1)

HashSet不允许重复值。它提供了添加方法而不是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");