2017-07-18 124 views
0

我試圖做一個應用程序,您可以使用EditText和ListView將項目添加到列表中。我使用this website來幫助,因爲我沒有太多的android編碼經驗,但我不得不改變一下代碼,因爲我使用了兩個活動而不是一個。但是我的ListView已經消失了,我不知道爲什麼。ListView沒有顯示?

Questions.java(ListView的活性)

package com.example.sylvie.dogwise; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class Questions extends AppCompatActivity { 
    ListView listview; 
    String[] ListElements = new String[] { 
      "Question 1", 
      "Question 2" 
    }; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_questions); 

     Bundle newQuestion = getIntent().getExtras(); 
     if (newQuestion==null){ 
      return; 
     } 
     String QuestionName = newQuestion.getString("QuestionName"); 

     listview = (ListView) findViewById(R.id.QuestionsList); 

     final List<String> ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements)); 
     final ArrayAdapter<String> adapter = new ArrayAdapter<String> 
       (Questions.this, android.R.layout.simple_list_item_1, ListElementsArrayList); 

     ListElementsArrayList.add(QuestionName); 
     adapter.notifyDataSetChanged(); 
    } 

    public void backHomeOnClick(View view){ 
     Intent b = new Intent(this, HomeScreen.class); 
     startActivity(b); 
    } 

    public void askAQuestionOnClick(View view){ 
     Intent i = new Intent(this, AskAQuestion.class); 
     startActivity(i); 
    } 
    } 

activity_questions.xml(ListView的活性)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.sylvie.dogwise.Questions"> 

    <Button 
     android:id="@+id/qBackButton" 
     android:layout_width="88dp" 
     android:layout_height="wrap_content" 
     android:text="Back" 
     android:onClick="backHomeOnClick" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" /> 

    <ListView 
     android:id="@+id/QuestionsList" 
     android:layout_width="384dp" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/qBackButton" /> 

    <Button 
     android:id="@+id/AskAQuestionButton" 
     android:layout_width="384dp" 
     android:layout_height="50dp" 
     android:layout_marginBottom="0dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginRight="0dp" 
     android:text="Ask a Question" 
     android:onClick="askAQuestionOnClick" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

AskAQuestion.java(所述的EditText活性)

package com.example.sylvie.dogwise; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 

public class AskAQuestion extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_ask_aquestion); 
    } 
    public void backQuestionsOnClick(View view){ 
     Intent b = new Intent(this, Questions.class); 
     startActivity(b); 
    } 
    public void okOnClick(View view){ 
     Intent o = new Intent(this, Questions.class); 

     final EditText QuestionInput = (EditText) findViewById(R.id.editText); 
     String Question = QuestionInput.getText().toString(); 

     o.putExtra("QuestionName", Question); 

     startActivity(o); 
    } 
} 

activity_ask_aquestion .xml(EditText活動)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.sylvie.dogwise.AskAQuestion"> 

    <Button 
     android:id="@+id/aaqBackButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="0dp" 
     android:layout_marginTop="0dp" 
     android:onClick="backQuestionsOnClick" 
     android:text="Back" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="350dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:text="Question" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="100dp" /> 

    <Button 
     android:id="@+id/okButton" 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="32dp" 
     android:text="OK" 
     android:onClick="okOnClick" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/editText" /> 
</android.support.constraint.ConstraintLayout> 
+0

'listview.setAdapter(適配器)'您的onCreate()方法 –

回答

3

你需要調用listview.setAdapteradapter

public class Questions extends AppCompatActivity { 

    // fields 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // code ... 
     // no need of notifyDataSetChanged 
     listview.setAdapter(adapter); 
     // from this moment on, use adapter.notifyDataSetChanged 
     // to indicate changes in dataset 
    } 
    // other methods 
} 
+0

雖然'adapter.notifyDataSetChanged()內;'只在需要時適配器被附加到listview(setadapter),並且你想在數據發生變化時通知適配器 –

+0

非常感謝! – Sylvie

+0

我很高興我可以幫助:) –