开发者

How to add item to arraylist with button click?

Something is wrong with my code. In the application, when I "add item", it doesn't show anything, and if I am 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.getTex开发者_开发技巧t().toString()); 

                    }
                });
    }
}


Use below code instead of your code.

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
        final ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, ar);
        LView.setAdapter(adpt);
        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());
                adpt.setNotifyOnChange(true);
                LView.setAdapter(adpt);
            }
        });
    }
}


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


you can use this add string to list on a button click

final String a[]={"hello","world"};
final ArrayAdapter<String> at=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,a);
final ListView sp=(ListView)findViewById(R.id.listView1);
sp.setAdapter(at);
final EditText et=(EditText)findViewById(R.id.editText1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            int k=sp.getCount();
            String a1[]=new String[k+1];
            for(int i=0;i<k;i++)
                a1[i]=sp.getItemAtPosition(i).toString();
            a1[k]=et.getText().toString();
            ArrayAdapter<String> ats=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,a1);
            sp.setAdapter(ats);
        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜