通过edittext将项目添加到listview

时间:2014-02-27 12:21:25

标签: java android

我有edittext和按钮以及列表视图,我想从edittext添加项目到listview,当我点击edittext中的按钮文本添加到listview但这个错误将出现“不幸的是,列表已停止。”

> main.xml代码是:

<EditText 
    android:id="@+id/EdtInput"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:hint="@string/StInput"
    />
<Button 
    android:id="@+id/BtnAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/StAdd"
    android:layout_toRightOf="@+id/EdtInput"
    />

<ListView 
    android:id="@+id/LstItem"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/EdtInput"
    android:layout_alignLeft="@+id/EdtInput"
    ></ListView>

&GT; MainActivity.java代码是:

package com.saly.list;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final EditText ET=(EditText) findViewById(R.id.EdtInput);
    final String StInput=ET.getText().toString();
    final ListView LV=(ListView) findViewById(R.id.LstItem);
    final ArrayList<String> ArLi=new ArrayList<String>();
    final ArrayAdapter<String> ArAd=new ArrayAdapter<String>(this, R.layout.main,ArLi);
    LV.setAdapter(ArAd);
    Button Add=(Button) findViewById(R.id.BtnAdd);
    Add.setOnClickListener(new OnClickListener() {          
        public void onClick(View v) {
            ArLi.add(StInput);
            ArAd.notifyDataSetChanged();
            ET.setText("");
        }       
    });     
}
}

这是LogCat错误:

02-27 16:44:06.369: I/Adreno-EGL(21719): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
02-27 16:44:06.409: D/OpenGLRenderer(21719): Enabling debug mode 0
02-27 16:44:12.515: E/ArrayAdapter(21719): You must supply a resource ID for a TextView
02-27 16:44:12.515: D/AndroidRuntime(21719): Shutting down VM
02-27 16:44:12.515: W/dalvikvm(21719): threadid=1: thread exiting with uncaught exception (group=0x41836ba8)
02-27 16:44:12.525: E/AndroidRuntime(21719): FATAL EXCEPTION: main
02-27 16:44:12.525: E/AndroidRuntime(21719): Process: com.saly.list, PID: 21719
02-27 16:44:12.525: E/AndroidRuntime(21719): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.ListView.makeAndAddView(ListView.java:1790)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.ListView.fillDown(ListView.java:691)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.ListView.fillFromTop(ListView.java:752)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.ListView.layoutChildren(ListView.java:1616)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.AbsListView.onLayout(AbsListView.java:2091)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.View.layout(View.java:14817)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.Choreographer.doFrame(Choreographer.java:544)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.os.Handler.handleCallback(Handler.java:733)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.os.Looper.loop(Looper.java:136)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.app.ActivityThread.main(ActivityThread.java:5017)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at java.lang.reflect.Method.invoke(Method.java:515)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-27 16:44:12.525: E/AndroidRuntime(21719):    at dalvik.system.NativeStart.main(Native Method)
02-27 16:44:12.525: E/AndroidRuntime(21719): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
02-27 16:44:12.525: E/AndroidRuntime(21719):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
02-27 16:44:12.525: E/AndroidRuntime(21719):    ... 40 more

4 个答案:

答案 0 :(得分:0)

Move final String StInput = ET.getText()。toString();在你的onClick方法。

Add.setOnClickListener(new OnClickListener() {          
    public void onClick(View v) {
        String StInput= ET.getText().toString();
        if(StInput != null && StInput.length() > 0){
            ArLi.add(StInput);
            ArAd.notifyDataSetChanged();
            ET.setText("");
        }else{
           //EditText is blank
        }
    }       
}); 

编辑 - 请添加此内容。

ArrayAdapter<String> ArAd=new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1,android.R.id.text1,ArLi);

我相信你正在使用一个简单的字符串列表。问题在于您传递的布局,因为您已经通过了主布局。将其更改为上面,并知道这是否有效。

答案 1 :(得分:0)

我正在DB中保存edittext值并立即将其显示在列表中 这是我的代码

    mEditProblem.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                       // calculate();
                        saveContentToDb();
                    }
                });

            mEditProblem.setOnFocusChangeListener(new View.OnFocusChangeListener() {          

                    public void onFocusChange(View v, boolean hasFocus) {
                        if(!hasFocus){
                           // calculate();
                            //if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                in.hideSoftInputFromWindow(mEditProblem.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                           // }
    //                      saveContentToDb();
                        }
                    }
                });


public void saveContentToDb(){

        newlyAddedText = mEditProblem.getText().toString();
        Toast.makeText(getApplicationContext(), mEditProblem.getText().toString(), 0).show();
        itemData.add(new ItemRow(mEditProblem.getText().toString(),getResources().getDrawable(R.drawable.ic_launcher) ));
        adapter.notifyDataSetChanged();
        mEditProblem.setText("");



    }

答案 2 :(得分:0)

您首次加载应用时会收到该文字。它永远不会更新。 您需要在按钮事件被引发时获取文本。

public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final EditText ET=(EditText) findViewById(R.id.EdtInput);
    final ListView LV=(ListView) findViewById(R.id.LstItem);
    final ArrayList<String> ArLi=new ArrayList<String>();
    final ArrayAdapter<String> ArAd=new ArrayAdapter<String>(this, R.layout.main,ArLi);
    LV.setAdapter(ArAd);
    Button Add=(Button) findViewById(R.id.BtnAdd);
    Add.setOnClickListener(new OnClickListener() {          
        public void onClick(View v) {
            ArLi.add(ET.getText().toString());
            ArAd.notifyDataSetChanged();
            ET.setText("");
        }       
    });     
}
}

答案 3 :(得分:0)

您的ListView会显示ArLi变量中存储的数据。你需要做的是像你一样添加字符串。那部分工作正常。

您的问题是,在点击按钮之前,您会从EditText获取文字。因此,点击按钮将始终不添加任何内容(或EditText的默认值。要在单击按钮后获取文本,您需要更改OnClickListener,如下所示:

Add.setOnClickListener(new OnClickListener() {          
    public void onClick(View v) {
        String input = ET.getText().toString();
        if(input != null && input != "")
        {
            ArLi.add(ET.getText().toString());
            ArAd.notifyDataSetChanged();
            ET.setText("");
        }
    }       
});

请注意,我认为您不想添加null或空字符串。根据需要调整。我还强烈建议你看一下Java编码实践。变量应以较低的字母开头。此外,编写具体的变量名而不是缩写将有助于使您的代码更具可读性。