2012-04-24 46 views
1

我在我的佈局文件中有一個列表視圖和一個按鈕。點擊該按鈕我想添加項目到列表視圖。當活動開始時,listview應該是空的,但它應該通過向它添加項目而增長。點擊「AddItem」按鈕,我顯示一個AlertDialog從用戶獲取項目/輸入。我如何將該項目添加到列表視圖?請幫我用代碼。我應該使用哪種適配器在ListView內使用我自己的佈局?我應該擴展ListActivity嗎?Android:如何添加一個項目到列表視圖onclick按鈕?

這是我的佈局文件:my_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/blue" 
android:orientation="vertical" > 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:divider="@android:color/black" 
    android:dividerHeight="2dp" > 
</ListView> 

<Button 
     android:id="@+id/addItemButtom" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_weight="1" 
     android:text="Add Item" /> 


</LinearLayout> 

這裏是我的row_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/white" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <TextView android:layout_weight = "1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Newly Added Item : " 
     android:textColor="@android:color/black" 
     android:textSize="17dp" /> 

    <TextView android:layout_weight = "1" 
     android:id="@+id/itemTextView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

</LinearLayout> 

這裏是我的java文件:

public class MyList extends Activity implements OnClickListener{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.my_list); 

    findViewById(R.id.addItemButtom).setOnClickListener(this); 

} 



@Override 
public void onClick(View v) 
{ 
    switch (v.getId()) { 
    case R.id.enterInverter: 

     AlertDialog.Builder alert = new AlertDialog.Builder(this); 
     final EditText et = new EditText(this);  
     et.setHint("Enter Item"); 
     et.setMaxLines(1);  
     et.setTextSize(17); 
     alert.setTitle("Enter Item"); 
     alert.setView(et); 
     alert.setInverseBackgroundForced(true); 

     alert.setPositiveButton("Add", new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int whichButton) 
      { 
       String item = et.getText().toString().trim(); 


      } 
     }); 

     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int whichButton) 
      { 
       dialog.cancel(); 
      } 
     }); 
     alert.show(); 



     break; 

    default: 
     break; 
    } 
} 
} 
+0

我應該看看這個:http://stackoverflow.com/questions/2383847/android-layout-with-listview-and-buttons – user1264255 2012-04-24 07:47:41

回答

3

哪裏是你的ListView ?

當您在列表中添加列表項(Array或ArrayList的或...)

your_adapter.notifyDataSetChanged(); 

它使您繪製的ListView新鮮

+0

ListView是在my_list.xml佈局文件 – 2012-04-24 08:34:04

+0

沒有適配器的列表項目? – sique 2012-04-24 08:49:17

4

一個ArrayAdapter可能是點開始。當然,它會根據你的列表而改變,但我建議你使用ArrayAdapter。獲取列表的引用,爲其定義一個新的適配器並使用setAdapter方法進行設置。然後,您可以添加/刪除項目到您的適配器。

+0

你能提供一些代碼嗎?至今我還沒有使用適配器 – 2012-04-24 08:38:57

+1

非常感謝。它現在有效。我改變了列表視圖ID,然後將這些代碼添加到活動文件中。在onCreate()中:list =(ListView)findViewById(R.id.myListView); adapter = new ArrayAdapter (MyList.this,R.layout.my_list_row,R.id.itemTextView); list.setAdapter(適配器);獲得用戶輸入後:adapter.add(item); adapter.notifyDataSetChanged(); – 2012-04-24 09:12:59

+1

恭喜! – Siyamed 2012-04-24 09:27:56

相關問題