从ListView显示EditText的文本

时间:2012-02-24 07:22:09

标签: android android-layout android-widget

我在每个列表行都有EditTextButton。我ListView中有三个项目,我的任务是当有人点击包含相应Toast中文字的按钮时显示EditText。问题是它在null上显示Toast。这是我的代码:

package org.ritesh;



    public class Edit_Text_listviewActivity extends Activity {
        /** Called when the activity is first created. */
        ListView list;
        List<String> hello;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            list=new ListView(this);

            setContentView(list);
            hello=new ArrayList<String>();
            hello.add("Hello");
            hello.add("Hello World");
            hello.add("Hello world");

            adapter adap=new adapter(this,android.R.layout.simple_list_item_1,hello);
           list.setAdapter(adap); 
        }
        public class adapter extends ArrayAdapter
        {List<String> hello;
    Button btn;
    EditText text;
            public adapter(Context context, int textViewResourceId, List<String> objects) {
                super(context, textViewResourceId, objects);
                // TODO Auto-generated constructor stub
            hello=objects;
            }
            public int getCount(){
                return hello.size();
            }


                @Override
                public View getView(int position, View convertView, ViewGroup parent){

                    LayoutInflater inflater = (LayoutInflater) Edit_Text_listviewActivity.this
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View rowview=(View)inflater.inflate(R.layout.main, parent,false);
                    text=(EditText)rowview.findViewById(R.id.edit);
                    text.requestFocusFromTouch();
                    btn=(Button)rowview.findViewById(R.id.btn);
                    btn.setOnClickListener(new OnClickListener(){

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            String str=text.getText().toString();
                            Toast.makeText(Edit_Text_listviewActivity.this, str, Toast.LENGTH_LONG).show();
                        }

                    });
                return rowview;
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

我认为您在getview()方法中错过了一行..

text.setText(hello.get(position));

所以可能是,

text=(EditText)rowview.findViewById(R.id.edit);
text.requestFocusFromTouch();
text.setText(hello.get(position));

答案 1 :(得分:0)

检查出来

 public class Edit_Text_listviewActivity extends Activity {
            /** Called when the activity is first created. */
            ListView list;
            List<String> hello;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                list=new ListView(this);

                setContentView(list);
                hello=new ArrayList<String>();
                hello.add("Hello");
                hello.add("Hello World");
                hello.add("Hello world");

                adapter adap=new adapter(this,android.R.layout.simple_list_item_1,hello);
        adap.myActivity = this;
               list.setAdapter(adap); 
            }
        showMessage(String mytext){

                                Toast.makeText(Edit_Text_listviewActivity.this, mytext, Toast.LENGTH_LONG).show();
        }

            public class adapter extends ArrayAdapter
            {List<String> hello;
        Button btn;
        EditText text;
                public adapter(Context context, int textViewResourceId, List<String> objects) {
                    super(context, textViewResourceId, objects);
                    // TODO Auto-generated constructor stub
                hello=objects;
                }
                public int getCount(){
                    return hello.size();
                }


                    @Override
                    public View getView(int position, View convertView, ViewGroup parent){

                        LayoutInflater inflater = (LayoutInflater) Edit_Text_listviewActivity.this
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View rowview=(View)inflater.inflate(R.layout.main, parent,false);
                        text=(EditText)rowview.findViewById(R.id.edit);
                        text.requestFocusFromTouch();
                        btn=(Button)rowview.findViewById(R.id.btn);
                        btn.setOnClickListener(new OnClickListener(){

                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                               myActivity.showMessage(text.getText().toString(););
                            }

                        });
                    return rowview;
                }
            }
        }

答案 2 :(得分:0)

在点击中你必须指定 String item =(String)getListAdapter()。getItem(position);