Ramz
Ramz

Reputation: 658

Avoid duplicates in list

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?

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 to get that list...

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

Upvotes: 1

Views: 4876

Answers (3)

Prabhakaran
Prabhakaran

Reputation: 379

Set will not allow duplicates...See example below

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

Then add list to adapter. That's it.

Hope this will help you ;)

Upvotes: 5

Asad Iqbal
Asad Iqbal

Reputation: 304

HashSet does not allow duplicate values. It provides add method rather put method. You also use its contain method to check whether the object is already available in HashSet. HashSet can be used where you want to maintain a unique list.

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

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38511

Why don't you use a Set instead of a List? They do not allow duplicates. You will need a hashCode and equals method if you put your own objects in there, but it should just work for Strings

Upvotes: 2

Related Questions