0
    int size = mcq.size();


    String arr[] = null;
    int i;
    {


        for (i = 0; i < size; i++) {


            if (op1.isPressed()) {
                arr[i] = tv1.getText().toString();
                // Log.e("Array",arr[i]);

            } else if (op2.isPressed()) {
                arr[i] = tv2.getText().toString();
                //Log.e("Array",arr[i]);

            } else if (op3.isPressed()) {
                arr[i] = tv3.getText().toString();
                //  Log.e("Array",arr[i]);


            } else if (op4.isPressed()) {
                arr[i] = tv4.getText().toString();
                //Log.e("Array",arr[i]);


            }

I am trying to store the data in an array when the button is pressed,but it always shows null.And when the for loop is over I want to display my array.

6
  • you may consider using ArrayList Commented May 11, 2016 at 6:06
  • the problem in ArrayList is that it adds the duplicate value@ndeokar Commented May 11, 2016 at 6:08
  • it has .contains() method to check for duplication Commented May 11, 2016 at 6:09
  • you may debug and find out why array is null after for loop Commented May 11, 2016 at 6:09
  • Ok,if I clicked op1,op2,op3 and op4 then it will add all the data for the same position,which I don, not want.For a position I want only one value.So can u tell me if it solves the problem I will use it @ndeokar Commented May 11, 2016 at 6:12

4 Answers 4

2

here , Arrays in Java have a size so u cannot do like this. Instead of this use list,

ArrayList<String> arr = new ArrayList<String>();

Inorder to do using String array :

String[] arr = new String[SIZEDEFNE HERE];

For ur answer :

ArrayList<String> arr = new ArrayList<String>();
    int i;
        {
            for (i = 0; i < size; i++) {
                if (op1.isPressed()) {
                    arr.add(tv1.getText().toString()); 
                } else if (op2.isPressed()) {
                    arr.add(tv2.getText().toString()); 

                } else if (op3.isPressed()) {
                    arr.add(tv3.getText().toString()); 

                } else if (op4.isPressed()) {
                    arr.add(tv4.getText().toString()); 
                }

Retrive value using

String s = arr.get(0);
Sign up to request clarification or add additional context in comments.

Comments

1

This is because your string array is null. Declare it with the size as

String[] arr = new String[size];

2 Comments

Still getting null
You may have not initialized your TextView's (tv1, tv2, tv3, tv4) properly.
1

Try Array List. use the following code in your main java

final ArrayList<String> list = new ArrayList<String>();

Button button= (Button) findViewById(R.id.buttonId);
final EditText editText = (EditText) findViewById(R.id.editTextId)
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        list.add(editText.getText().toString());
    }
});

Comments

0

To avoid duplication in arraylist .You can use arraylist for faster then String array

ArrayList<String> list = new ArrayList<String>();
    list.add("Krishna");
    list.add("Krishna");
    list.add("Kishan");
    list.add("Krishn");
    list.add("Aryan");
    list.add("Harm");

System.out.println("List"+list);
HashSet hs = new HashSet();
hs.addAll(list);
list.clear();
list.addAll(hs);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.