How to add items to a list for an Android app?

I am very new to android and ask to develop an Android application. I am working on Android 2.2. The problem that I encountered is that the client name should appear after clicking the "Add" button, as in the basic tutorial, but this does not work. After clicking the button, the name does not appear in the list.

Here are the codes:

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> 

Please help me. thanks in advance

+4
source share
2 answers

You do not need to save ArrayList and ArrayAdapter

The ArrayAdapter will actually hold a list of lines inside it.

get rid of your ArrayList and change this line

 this.appointment.add(item); 

to

 this.aa.add(item); 

and you should be good to go.

I am also interested in why you use the full names of all your variables. I definitely understand that sometimes the scope of your various classes will force him to use "this." but it seems from your example that everything will be all right with you.

Is this just what you do for clarity when reading code, or does it serve other purposes?

EDIT: ps @Selvin completely nailed this, if they sent a response, please accept them instead of mine.

+5
source

You must do this using aa.add(item) ; No need for an ArrayList .

0
source

Source: https://habr.com/ru/post/1394011/


All Articles