2012-02-24 41 views
0

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

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; 
      } 
     } 
    } 
+0

如果包含来自logcat的异常跟踪并对代码进行格式化,那么获得正确答案的机会将增加。 – Tapirboy 2012-02-24 07:32:02

回答

1

我认为你缺少你getview()方法一行..

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

所以它可能是,

text=(EditText)rowview.findViewById(R.id.edit); 
text.requestFocusFromTouch(); 
text.setText(hello.get(position)); 
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; 
       } 
      } 
     } 
+0

当我点击任何按钮时,它显示的文字中包含最后一个Edittext中的文本,而不是相应的edittext中的文本。 – 2012-02-24 08:52:12

+0

好吧做一件事情宣布edittext和按钮内getview与最终modifire – vipin 2012-02-24 09:03:12

+0

嘿,工作或不? – vipin 2012-02-24 09:51:37

0

在点击你哈已经指定 String item =(String)getListAdapter()。getItem(position);