我正在尝试根据我在editText
中保存的项目,弄清楚如何在listView
对象中设置文字。我正在为DnD建造一个复杂的骰子辊。这需要用户输入并计算值,用户还可以保存卷以供以后使用。如何让它填充editText
对象以便用户重新掷骰子?以下是我用来保存到listView
的代码:
public void saveToList(){
String getInput = et_roll1.getText().toString();
if (addArray.contains(getInput)){
Toast.makeText(getBaseContext(), "Item Already Added", Toast.LENGTH_LONG).show();
}
else if (getInput == null || getInput.trim().equals("")){
Toast.makeText(getBaseContext(), "Input Field is Empty", Toast.LENGTH_LONG).show();
}
else{
addArray.add(getInput);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
lv_rolls.setAdapter(adapter);
((EditText)findViewById(R.id.et_roll1)).setText(" "); //reset editText to blank
}
以下是我onClickListener
的代码:
lv_rolls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
resetValue();
}
});
答案 0 :(得分:1)
lv_rolls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
tv_roll1_result.setText(" ");
et_roll1.setText(String.valueOf(lv_rolls.getItemAtPosition(position)));
}
});