views:

30

answers:

1

Something is wrong whit my code. in application when I "add item", it doesnt show anything, and if aim clicking somewhere around the android application, then "item" sometimes comes. Can somebody help me?

package com.example.proov;

import java.util.ArrayList;

import com.example.proov.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class proovin extends Activity {
    private ListView LView;


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

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LView = (ListView) findViewById(R.id.ListView01);
        //      Set option as Multiple Choice. So that user can able to select more the one option
                LView.setAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_multiple_choice, ar));
                LView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

             Button b = (Button) findViewById(R.id.add_item);
             final EditText d = (EditText) findViewById(R.id.title);
             b.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ar.add(d.getText().toString()); 

                    }
                });
    }
}
+1  A: 

The item is likely getting added to the ArrayList, but that is different from getting added to the ListView. You need to tell the ListView that you updated the data model so that it knows to look. See ArrayAdapter.notifyDatasetChanged()

Mayra