简体   繁体   中英

Add random data from listview items into Textview

When you click the button as it appears in the code, the data in the edit text is added as a list view.

When I click the button, I want to add a random data from the listview items and add it in the textview.

public class MainActivity extends AppCompatActivity {

    ListView listgor;
    EditText textgir;
    Button katgir;
    Button kuraceker;
    TextView katilimcisecer;


    ArrayList<String> data = new ArrayList<String>();

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

        data.add("Hello World");

        listgor = (ListView) findViewById(R.id.listeler);
        textgir = (EditText) findViewById(R.id.sira1);
        katgir = (Button) findViewById(R.id.katilimciekle);
        kuraceker = (Button) findViewById(R.id.kuracek);
        katilimcisecer = (TextView) findViewById(R.id.katilimci) ;


        listgor.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
        katgir.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View currentView) {
                data.add(textgir.getText().toString());
                textgir.setText("");
            }
        });

    } 
}

To add data from listview items to TextView use code below.

int randomIndex = ThreadLocalRandom.current().nextInt(0, data.size())
yourTextView.setText(data.get(randomIndex))

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