2014-02-27 153 views
1

我有edittext和按鈕和列表視圖,我想從edittext添加項目到列表視圖,當我點擊從edittext按鈕文本添加到列表視圖,但會出現此錯誤「不幸的是,列表已停止「。將項目添加到列表視圖編輯文本

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

> MainActivity.java代碼是:

包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 
+0

在附註中,值得思考如何命名變量 - http://stackoverflow.com/questions/414001/vari能夠命名約定在java –

回答

0

移動最終字符串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); 

我相信你正在使用一個簡單的字符串列表。問題在於你傳遞的佈局,因爲你已經通過主佈局。將其更改爲上面的內容並讓其知道這是否有效。

+0

tnx兄弟,但這個「不幸的是,列表已經停止。」錯誤仍然出現。 –

+0

我覺得現在的問題與適配器有關。你可以發佈錯誤日誌貓嗎? –

+0

02-27 16:21:01.861:E/ArrayAdapter(20169):您必須提供TextView的資源ID –

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



    } 
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(""); 
     }  
    });  
} 
} 
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編碼實踐,變量應該以一個較低的字母開頭,另外,編寫具體的變量名稱而不是縮寫將有助於讓你的代碼更具可讀性。

+1

tnx埃裏克,錯誤仍然出現 你的意思是什麼改變你的OnClickListener? –

+0

那麼你從來沒有添加任何東西。你在調用'create()'時得到了文本,但從來沒有得到另一個值。因此,在實際添加用戶編寫的文本之前,需要將用於獲取文本的行添加到偵聽器中。我看到接受的解決方案提到了失蹤Id的錯誤原因。我希望它現在適合你! – Eric