2012-01-31 104 views
4

我在android中很新,被要求爲android開發一個應用程序。我正在使用Android 2.2。我遇到的問題是,一旦我點擊基本教程中的「添加」按鈕,客戶端的名稱就會出現,但它不會以這種方式工作。點擊按鈕後名稱不會顯示在列表視圖中。如何在Android應用程序的listview中添加項目?

這裏代碼:

public class MainActivity extends Activity implements OnClickListener,  
OnKeyListener { 

    ArrayList<String> appointment; 
    ArrayAdapter<String> aa; 

    EditText editText1; 
    Button addButton; 
    ListView listView1; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     editText1 = (EditText)findViewById(R.id.editText1); 
     addButton = (Button)findViewById(R.id.addButton); 
     listView1 = (ListView)findViewById(R.id.listView1); 

     addButton.setOnClickListener(this); 
     editText1.setOnKeyListener(this); 

     appointment = new ArrayList<String>(); 
     aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
       appointment); 
     listView1.setAdapter(aa); 

    } 

    private void addItems(String item){ 

     if (item.length()>0){ 
      this.appointment.add(item); 
      this.aa.notifyDataSetChanged(); 
      this.editText1.setText(""); 
     } 

    } 

    public void onClick(View v) { 

     if(v==this.addButton){ 
      this.addItems(this.editText1.getText().toString()); 
     } 

    } 



    public boolean onKey(View v, int keyCode, KeyEvent event) { 

     if (event.getAction()==KeyEvent.ACTION_DOWN && 
       keyCode==KeyEvent.KEYCODE_DPAD_CENTER) 
      this.addItems(this. editText1.getText().toString()); 

     return false; 
    } 


} 

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout 
android:orientation="vertical" 
android:layout_width="fill_parent" android:layout_height="wrap_content"  
android:weightSum="1"> 

<ImageView 

android:id="@+id/phones_icon" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center_horizontal" 
android:src="@drawable/user" 
android:layout_margin="20dp" 
/> 

<Button 
android:text="Add New Appoinments" 
android:id="@+id/addButton" 
android:layout_gravity="center_horizontal" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textStyle="bold" 
android:typeface="sans" 
    ></Button> 

<EditText android:id="@+id/editText1" android:layout_height="wrap_content" 
android:layout_width="match_parent" android:inputType="textPersonName"> 
    <requestFocus></requestFocus> 
</EditText> 

<ListView android:id="@+id/listView1" android:layout_width="wrap_content" 
android:layout_height="252dp"></ListView> 

</LinearLayout> 
</ScrollView> 

請指引我。在此先感謝

+2

將項目添加到ArrayAdapter ...不是ArrayList – Selvin 2012-01-31 16:08:03

+0

ops .. @ Selvin,非常感謝!^^ – 2012-01-31 16:42:28

回答

5

你並不需要保持一個ArrayList和ArrayAdapter

一個ArrayAdapter將實際持有其內部字符串列表。

擺脫你的ArrayList和改變這一行

this.appointment.add(item); 

this.aa.add(item); 

,你應該是好去。

我也對你爲什麼使用所有變量的全限定名感興趣。我明確地認爲,有時你們各個班級的範圍將使得有必要使用「這個」。面前的事情,但從我的例子來看,如果沒有「這個」,你會沒事的。

這只是您在閱讀代碼時爲清晰起見而做的事情,還是用於某些其他目的?

編輯:p.s. @Selvin完全釘了這一個,如果他們發佈答案,請接受他們而不是我的。

0

你應該可以使用aa.add(item);不需要ArrayList

相關問題