如何根据listView中单击的项设置editText对象

时间:2016-12-20 03:11:28

标签: android listview android-edittext onclicklistener

我正在尝试根据我在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();
            }
        });

1 个答案:

答案 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)));


    }
});