Add New Item in Listview dynamically

2020-07-24 05:38发布

I am a beginner and I want to add items in my Predefined array

public class MainActivity extends ListActivity {
        //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
        ;
        String listItem[]={"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE"};


        ArrayAdapter<String> adapter;
        EditText et;

        @Override
        public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.main);
        adapter=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            listItem);
        setListAdapter(adapter);
        et=(EditText) findViewById(R.id.editText);
    }


    public void addItems(View v) {
        String data=et.getText().toString();
        listItem.add(""+data);
        adapter.notifyDataSetChanged();
    }
}

1条回答
小情绪 Triste *
2楼-- · 2020-07-24 05:51

Illusionist, I've felt your pain as a beginner and have struggled with these exercises myself. The above advice from MH regarding using a list and adding straight to the adapter is correct. I've included an altered version of the exercise, but it basically does what you want it to do. I've added a couple of buttons, one to add a new item to the list and one to exit the application. Both have "onClick" added in the xml layout file for the main activity.

See if you can follow what I've done and let me know if you have any questions or concerns...

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;

public class MainActivity extends ListActivity {
EditText et;
String listItem[]={"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense",   "HTC Sensation XE"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et = (EditText) findViewById(R.id.editText);

    List values = new ArrayList();
    for (int i = 0; i < listItem.length; i++) {
        values.add(listItem[i]);
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

public void onClick(View view) {
    ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
    String device;
    switch (view.getId()) {
    case R.id.addItem:
        List myList = new ArrayList();
        device = et.getText().toString();
        myList.add(device);
        adapter.add(device);
        et.setText("");
        break;
    case R.id.exit:
        finish();
        break;  
    }
    adapter.notifyDataSetChanged();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

The associated xml layout file looks like...

<?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:orientation="vertical" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"> 
    <Button
        android:id="@+id/addItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Item"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bye Bye"
        android:onClick="onClick"/>
</LinearLayout>
    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Text Goes Here"/>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

Note: be careful of the ListView id; it has to be the way you see it above when using ListActivity... http://www.vogella.com/articles/AndroidListView/article.html

查看更多
登录 后发表回答