2012-06-07 4 views
0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/speed" 
    android:inputType="number"></EditText>" 
<TextView 
    android:id="@+id/TextViewSpeed" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text ="Speed" 
    android:layout_below="@+id/speed" 
    /> 
<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:text="AddValue" 
    android:id="@+id/AddValue" 
    > 
</Button> 
<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/ListView" 
    android:layout_below="@+id/AddValue" 
    > 
</ListView> 

</RelativeLayout> 

이것은 내 레이아웃 코드입니다. EditText에서 같은 페이지에있는 ListView에 텍스트 데이터를 추가하고 싶습니다. AddValue 버튼을 클릭하면 목록에 텍스트를 추가하는 코드를 작성하는 방법. 미리 감사드립니다. 런타임에 Android에 목록의 항목을 추가하고 싶습니다.

답변

2

사용 조각 줘야 도움 아래에서이 코드

addbutton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) {  
     String val = edittext.getText().toString(); 
     list.add(val); 
     ((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged(); 
    } 
}); 
+0

안녕하세요. RajaReddy !! :) – andrewww

+0

문제가 해결되면 답변으로 받아들입니다 .... –

+0

여기에있는 목록은 무엇입니까? – andrewww

1

.

package org.sample; 

    import android.app.ListActivity; 
    import android.os.Bundle; 
    import android.view.ContextMenu; 
    import android.view.ContextMenu.ContextMenuInfo; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.EditText; 

    public class SampleActivity extends Activity 
    { 
     private Button add; 
     private EditText speedText; 
     private ArrayAdapter<String> adapter; 
     private ListView AddValue; 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1); 

        AddValue=(ListView)findViewById(R.id.AddValue); 
      AddValue.setAdapter(adapter); 

      add = (Button) findViewById(R.id.add); 
      speedText = (EditText) findViewById(R.id.speed); 
      add.setOnClickListener(new OnClickListener() 
      { 

       @Override 
       public void onClick(View arg0) 
       { 
        if (speedText.getText().toString().length() != 0) 
        { 
         adapter.add(speedText.getText().toString()); 
         adapter.notifyDataSetChanged(); 
        } 

       } 
      }); 
     } 
    } 
관련 문제