0

I am new to programming i am currently creating a project in which i used 3 edit text one button and one list view.

I am able to add all 3 edit text values in the list view on button click but its running only one time that means it show all the 3 values in the list view but when ever i enter new value it delete the previous one also if i want to add 5 times in list view then previous one should not be deleted instead i get all the values 5 times in my list. Also the list view is coming in vertically i need if i click on button 5 times it should store all three edit text values 5 times in horizontal manner.

I have tried with for loop but its not working for me as nothing is changed.

public class secondActivity extends AppCompatActivity {

    EditText edit1, edit2, edit3;
    Button button;

    ListView listView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        edit1 = findViewById(R.id.name);
        edit2 = findViewById(R.id.desigination);
        edit3 = findViewById(R.id.post);

        listView = findViewById(R.id.list_view);


        button = findViewById(R.id.list_button);
        button.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {

                String itemName = edit1.getText().toString();
                String itemDescription = edit2.getText().toString();

                List list = Collections.synchronizedList(new ArrayList());

                list.add(itemName);
                list.add(itemDescription);

                ArrayAdapter lists1 = new ArrayAdapter(secondActivity.this, android.R.layout.simple_list_item_1, list);
                listView.setAdapter(lists1);
                edit1.setText("");
                edit2.setText("");

            }

        });

    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sunny
  • 79
  • 10

1 Answers1

0

Don't create list every time you click the button.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    edit1 = findViewById(R.id.name);
    edit2 = findViewById(R.id.desigination);
    edit3 = findViewById(R.id.post);

    listView = findViewById(R.id.list_view);

    List list = Collections.synchronizedList(new ArrayList());


    button = findViewById(R.id.list_button);
    button.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onClick(View v) {

            String itemName = edit1.getText().toString();
            String itemDescription = edit2.getText().toString();

            list.add(itemName);
            list.add(itemDescription);

            ArrayAdapter lists1 = new ArrayAdapter(secondActivity.this, 
            android.R.layout.simple_list_item_1, list);
            listView.setAdapter(lists1);
            edit1.setText("");
            edit2.setText("");

        }

    });

}
Ehsan msz
  • 1,774
  • 2
  • 13
  • 26
  • Thank you sir it worked for me.But can you Please help me out how to align vertically or horizontally in list view ? – Sunny Oct 31 '19 at 06:39
  • One more thing sir i have to declare List as final list what if i have to save this list to another list ??? – Sunny Oct 31 '19 at 10:31
  • @Sunny you can make a custom view for items in your list view. [link](https://stackoverflow.com/questions/15832335) – Ehsan msz Oct 31 '19 at 12:23
  • @Sunny you can save your `final` variable to another variable. – Ehsan msz Oct 31 '19 at 12:33