나는 안드로이드에 대한 새로운 지식과 안드로이드 용 앱을 개발하도록 요청 받고있다. 나는 안드로이드 2.2에서 일하고있다. 내가 만난 문제는 기본 튜토리얼 에서처럼 '추가'버튼을 클릭하면 클라이언트의 이름이 나타나야한다는 것입니다. 그러나 그 방법으로는 작동하지 않습니다. 단추를 클릭 한 후 이름이 목록보기에 표시되지 않았습니다.Android 앱의 목록보기에 항목을 추가하려면 어떻게해야하나요?
다음코드 :
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>
나를 인도 해주십시오. 사전
당신은 ArrayList에와 ArrayAdapter와ArrayAdapter와 실제로 내부 문자열의 목록을 개최한다을 보관할 필요가없는
ArrayAdapter에 항목 추가 ... not ArrayList – Selvin
ops .. @ Selvin, 정말 고마워! ^^ –