2012-01-31 54 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

相关问题