简体   繁体   中英

How to pick the text form an AutoCompleteTextView and show it in a ListView?

public class MainActivity extends AppCompatActivity {
AutoCompleteTextView et;
Button bt;
ListView lt;
ArrayList<Double> addArray = new ArrayList<Double>();
String getInput;
double sum = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//created adapter for showing list and R.array.veggi is a resource file that contains vegetable names

    String[] veggi = getResources().getStringArray(R.array.veggi);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, veggi);
    lt.setAdapter(adapter1);

    et = (AutoCompleteTextView) findViewById(R.id.Et);
    bt = (Button) findViewById(R.id.bt);
    lt =  (ListView)findViewById(R.id.listview);



    // btton to show text or numbers entered in AutoCompleteTextView
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getInput = et.getText().toString();
             double inp = Double.parseDouble(getInput);

            if (addArray.contains(getInput)) {
                Toast.makeText(getBaseContext(), "Item Already Added to array", Toast.LENGTH_SHORT).show();
            } else if (getInput == null || getInput.trim().equals(" ")) {
                Toast.makeText(getBaseContext(), "Input field is Empty", Toast.LENGTH_SHORT).show();

            } else {
                addArray.add(inp);
                ArrayAdapter<Double> adapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
                lt.setAdapter(adapter);


                ((AutoCompleteTextView) findViewById(R.id.Et)).setText(" ");
            }
        }
    });       
  }
}

//created adapter for showing list and If I remove this code it will show the text that entered in LisView but i want to show suggestions and pick the words from it like google search kindof

String[] veggi = getResources().getStringArray(R.array.veggi);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, veggi);
        lt.setAdapter(adapter1);

Problem's here

 ArrayList<Double> addArray = new ArrayList<Double>();
 ArrayAdapter<Double> adapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);

Change this to ...

ArrayList<String> addArray = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM