在Listview中动态添加新项目 [英] Add New Item in Listview dynamically

查看:50
本文介绍了在Listview中动态添加新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,我想在预定义数组中添加项目

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();
    }
}

推荐答案

魔术师,我感到您作为一个初学者很痛苦,并且自己也为这些练习而苦苦挣扎. MH关于使用列表并将其直接添加到适配器的上述建议是正确的.我已经包含了该练习的变更版本,但是它基本上可以完成您想要的操作.我添加了几个按钮,一个按钮将新项目添加到列表中,一个按钮退出应用程序.两者都在xml布局文件中为主要活动添加了"onClick".

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;
}

}

关联的xml布局文件看起来像...

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>

注意:注意ListView ID;它必须是您在使用ListActivity时在上面看到的方式... http://www .vogella.com/articles/AndroidListView/article.html

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

这篇关于在Listview中动态添加新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆