Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

android - edittext value not save in listview dynamically

in my listview already 20 items and above listview there is one "ADD" button .when i am click on button then open one popup dialog.. in popup dialog there is on edittext and save and close button .. when i am write something edittext and click on save button then its not working.... i'll use custom listview with one image left side and text in center..... help me

Button add = (Button)findViewById(R.id.btn_Add);
    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.popup);
            dialog.setTitle("Add Category");
            Button close = (Button)dialog.findViewById(R.id.btn_cls);
            close.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });

            Button save = (Button)dialog.findViewById(R.id.btn_sv);
            save.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText edit = (EditText)dialog.findViewById(R.id.editText1);
                    list.add(edit.getText().toString());
                    edit.setText("");               
                    adapter.notifyDataSetChanged();

                }
            });

            dialog.show();
        }
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this way,hope this will help you to solve your problem.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@drawable/black"
    android:orientation="vertical">

    <EditText
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/bbbb"
        android:ems="10"
        android:textColor="@color/White"
        android:hint="Search"
        android:padding="5dp"
        android:textSize="20sp">

        <requestFocus />
    </EditText>

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:dividerHeight="5dp"
        android:cacheColorHint="@null"
        android:listSelector="@null"
        android:clipToPadding="false"
        android:drawSelectorOnTop="true" />

    <Button
        android:id="@+id/btn_Add"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/list_selector"
        android:text="ADD Category"
        android:textSize="25sp"
        android:textStyle="bold" />

</LinearLayout>

listview_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/button_border"
    android:gravity="center"
    android:padding="5dp">

    <ImageView
        android:id="@+id/flag"
        android:background="@drawable/sd"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:adjustViewBounds="true"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="5dp">
        <TextView
            android:id="@+id/country"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="21sp"
            android:textStyle="bold" />
        </LinearLayout>


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/round"
        android:layout_marginLeft="5dp"/>

</LinearLayout>

popup.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
        <Button
            android:id="@+id/btn_cls"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@drawable/list_selector"
            android:text="Close" />

        <Button
            android:id="@+id/btn_sv"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@drawable/list_selector"
            android:text="Save" />
    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    private ListView list;
    private ListViewAdapter adapter;
    private EditText editsearch;
    private Button add;

    private String[] animal =  new String[] {"COW","LION","TIGER","ELEPHANT","MONKEY","DONKEY"};
    private ArrayList<String> listData;
    private Dialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.listview);
        add = (Button)findViewById(R.id.btn_Add);

        listData = new ArrayList<String>();
        for (int i=0;i<animal.length;i++){
            listData.add(animal[i]);
        }

        Collections.sort(listData);
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                addAnimal();
            }
        });

        adapter = new ListViewAdapter(this,listData);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,listData.get(position),Toast.LENGTH_SHORT).show();

//                if(listData.get(position).equals("COW"));
//                 intent = new Intent(getBaseContext(),COW.class);
//
//                 else if(listData.get(position).equals("LION"))
//                 intent = new Intent(getBaseContext(),LION.class);
//
//                 else if(listData.get(position).equals("TIGER"))
//                     intent = new Intent(getBaseContext(),TIGER.class);
//
//                 else if(listData.get(position).equals("MONKEY"))
//                 intent = new Intent(getBaseContext(),MOKEY.class);
//
//                 else if(listData.get(position).equals("DONKEY"))
//                 intent = new Intent(getBaseContext(),DONKEY.class);
            }
        });




        editsearch = (EditText) findViewById(R.id.search);

        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                String text = editsearch.getText().toString();
                adapter.filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
            }
        });
    }


    public void addAnimal(){
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.popup);
        dialog.setTitle("Add Animal");
        final EditText edit = (EditText)dialog.findViewById(R.id.editText1);
        Button close = (Button)dialog.findViewById(R.id.btn_cls);
        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        Button save = (Button)dialog.findViewById(R.id.btn_sv);
        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(edit.getText().toString().trim().length()>0){
                    listData.add(edit.getText().toString());
                    Collections.sort(listData);
                    adapter = new ListViewAdapter(MainActivity.this,listData);
                    list.setAdapter(adapter);
                    dialog.dismiss();
                }else{
                    edit.setError("Value Required");
                }

            }
        });

        dialog.show();
    }

    class ListViewAdapter extends BaseAdapter {

        private Context context;
        private ArrayList<String> data;
        private ArrayList<String> filterData;


        public ListViewAdapter(Context context,ArrayList<String> data) {
            this.context = context;
            this.data = data;
            filterData =new ArrayList<String>();
            filterData.addAll(this.data);
        }

        public class ViewHolder {
            TextView country;
        }


        @Override
        public int getCount() {
            return filterData.size();
        }

        @Override
        public Object getItem(int position) {
            return filterData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
                holder.country = (TextView) view.findViewById(R.id.country);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.country.setText(filterData.get(position));
            return view;
        }

        public void filter(String charText) {
            filterData.clear();
            if (charText.length() == 0) {
                filterData.addAll(data);
            }else{
                for (String animal : data){
                    if (animal.toLowerCase().contains(charText.toLowerCase())){
                        filterData.add(animal);
                    }
                }
            }
            notifyDataSetChanged();
        }

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...