2014-03-03 62 views
2

如何獲取在EditText中鍵入的數據並單擊該窗口中的「提交」應該將其添加到以前的活動列表視圖項目中? 我需要做的是:如何在使用適配器的按鈕單擊上添加ListView項目

  1. 創建的EditText和提交按鈕
  2. 創建列表視圖在同一活動
  3. 點擊提交按鈕應該在列表視圖中顯示。

我看到這個類似的問題在這裏:add items to listview dynamically android

,但我無法理解answer.Somebody請解釋如何做到這一點。

+0

到現在爲止要實施什麼? –

+0

@SimplePlan我做了,直到按鈕click.Now我需要知道如何在EditText中輸入的文本可以添加到ListView。 – Nevaeh

+0

然後讓我看看你的ListView適配器代碼。 –

回答

9

你只要做到以下幾點: 準備好這樣的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" > 

    <EditText 
    android:id="@+id/editText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/addItem" 
    android:hint="Add a new item to List View" /> 

    <Button 
    android:id="@+id/addItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:text="Add" /> 

    <ListView 
    android:id="@+id/listView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/editText" > 
    </ListView> 

</RelativeLayout> 

活動看起來像以下:

public class MainActivity extends Activity { 
    EditText editText; 
    Button addButton; 
    ListView listView; 
    ArrayList<String> listItems; 
    ArrayAdapter<String> adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     editText = (EditText) findViewById(R.id.editText); 
     addButton = (Button) findViewById(R.id.addItem); 
     listView = (ListView) findViewById(R.id.listView); 
     listItems = new ArrayList<String>(); 
     listItems.add("First Item - added on Activity Create"); 
     adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, listItems); 
     listView.setAdapter(adapter); 
     addButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       listItems.add(editText.getText().toString()); 
       adapter.notifyDataSetChanged(); 
      } 
     }); 
     listView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> a, View v, int position, 
        long id) { 
       Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG) 
         .show(); 
      } 
     }); 
    } 
} 
+0

它的工作。謝謝你這麼多:) – Nevaeh

2

創建String Arraylist並對其進行初始化

ArrayList<String> str1 = new ArrayList<String>(); 

添加就可以了

 str1.add("First Row"); 
    str1.add("Second Row"); 
    str1.add("Third Row"); 
    str1.add("Fourth Row"); 
    str1.add("Fifth Row"); 

然後設置AdapterListView

adapter=new ListAdapter(this,str1); 
list.setAdapter(adapter); 

一些值,然後添加你EditText文本str1和然後叫adapter.notifyDataSetChanged();

str1.add(edit_message.getText().toString()); 
adapter.notifyDataSetChanged(); 

嘗試將此代碼添加到Button onclick()

演示輸出:

enter image description here

+0

我看到你上面的圖片.....但你知道如何打開新的活動,當你點擊「新項目」...如果你知道然後請告訴我 –

0

假設你有一個ArrayList

ArrayList<String> dataList = new Arraylist(); 

等按鈕的點擊,則需要新的數據項添加到您的數據數組列表。

首先將在edittext中輸入的值存入字符串中。

String editedValue = yourEditText.getText.toString();

然後我們需要在我們的datalist中添加這個。

dataList.add(editedValue); 

然後只需調用適配器。notifyDataSetChanged()

yourAdapter.notifyDataSetChanged(); 

它會工作。

相關問題