我想从一个EditText一个按钮将值(项目和子项目)到ListView
我能够查看TextView的,但有一种方法来传递的EditText用的onClick TextView的和发送的TextView到ListView
下面是我的XML文件
<ListView
android:id="@+id/results_listview"
android:layout_width="323dp"
android:layout_height="369dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.58" />
<Button
android:id="@+id/AddButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.921"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/results_listview"
app:layout_constraintVertical_bias="0.084" />
<EditText
android:id="@+id/taskText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_task_title"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/results_listview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.243"
<EditText
android:id="@+id/descriptionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enter_description"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/results_listview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/taskText"
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:textSize="21sp"
/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="16sp" />
这是我的mainActivity
我已经尝试了许多不同的建议他人张贴,但都没有成功。我觉得我可能会丢失一些东西明显。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText taskText = (EditText) findViewById(R.id.taskText);
final EditText descText = (EditText) findViewById(R.id.descriptionText);
final TextView text1 = (TextView) findViewById(R.id.text1);
final TextView text2 = (TextView) findViewById(R.id.text2);
Button AddButton = (Button) findViewById(R.id.AddButton);
final ListView resultsListView = (ListView) findViewById(R.id.results_listview);
final HashMap<String, String> nameAddresses = new HashMap<>();
nameAddresses.put("item", "Subitem");
nameAddresses.put("fgfdg", "fgdg");
final List<HashMap<String, String>> listItems = new ArrayList<>();
final SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.text1, R.id.text2});
final SimpleAdapter adapter1 = new SimpleAdapter(this, listItems, R.layout.list_item,
new String[]{"First Line", "Second Line"},
new int[]{R.id.taskText, R.id.descriptionText});
final Iterator it = nameAddresses.entrySet().iterator();
while (it.hasNext())
{
HashMap<String, String> resultsMap = new HashMap<>();
Map.Entry pair = (Map.Entry)it.next();
resultsMap.put("First Line", pair.getKey().toString());
resultsMap.put("Second Line", pair.getValue().toString());
listItems.add(resultsMap);
}
resultsListView.setAdapter(adapter);
AddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String task = taskText.getText().toString();
String description = descText.getText().toString();
HashMap<String, String> nameAddresses = new HashMap<>();
nameAddresses.put("task", "description");
nameAddresses.put(task, description);
resultsListView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
//listItems.add(adapter);
}
});
}
}
你需要让你的适配器需要所有项目的清单,现在你是不是加入新值的适配器列表
HashMap<String, String> nameAddresses = new HashMap<>();
nameAddresses.put("task", "description");
nameAddresses.put(task, description);
resultsListView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
此代码不触及适配器列表中,因此不将其添加到列表中。
在你的类的顶部初始化您在列表中选择所需的项目列表,那么当你创建适配器组列出要适配器用于绘制项目的列表,当你按下“添加按钮”使一定要简单
list.put(“new”, “item”);
adapter.notifyDataSetChanged();
由于列表被链接到该适配器将重新绘制,它以前曾(这是“目录”)的项目列表中,因此没有必要建立新的清单或新的适配器