我正在尝试创建一个应用程序,您可以使用EditText和ListView将项目添加到列表中。我正在使用this website来帮助,因为我没有太多的android编码经验,但我不得不稍微更改代码,因为我使用的是两个活动而不是一个。但是我的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 :(得分:3)
您需要致电listview.
setAdapter(adapter
)
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
}