public class MyWorkout extends AppCompatActivity {
Button addExercise
// Record how many clicks
int clickCounter=0;
private ListView mListView;
ArrayList<String> listItems = new ArrayList<String>()
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SetContentView(R.layout.activity_my_workout);
Toolbar MyWorkouttoolbar = (Toolbar) findViewById(R.id.MyWorkouttoolbar);
setSupportActionBar(MyWorkouttoolbar);
listItems.add("Power Clean");
listItems.add("Dead Lift");
listItems.add("Hang Clean");
listItems.add("Military Press");
listItems.add("Leg Press");
listItems.add("Squat");
listItems.add("Bench MAX");
listItems.add("Squat MAX");
listItems.add("Stair Stepper");
listItems.add("Treadmill");
listItems.add("Bike");
listItems.add("Elliptical");
listItems.add("Bicep Curls");
listItems.add("Dumb Bell Squats");
listItems.add("Goblet Squats");
listItems.add("Bar Bell Curls");
listItems.add("Bar Bell Bench Press");
listItems.add("Dumb Bell Bench Press");
listItems.add("Incline Bar Bell Bench Press");
listItems.add("Incline Dumb Bell Bench Press");
listItems.add("Decline Dumb Bell Bench Press");
listItems.add("Decline Bar Bell Bench Press");
listItems.add("Dumb Bell Lunges");
listItems.add("Bar Bell Lunges");
listItems.add("Dumb Bell Flys");
listItems.add("Tricep Push Downs");
if (mListView == null) {
mListView = (ListView) findViewById(R.id.MyWorkoutlistView);
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
addExercise = (Button) findViewById(R.id.addExercise);
addExercise.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Collections.shuffle(listItems);
adapter.getItem(4);
adapter.notifyDataSetChanged();
}
});
}
private void updateListView(){
ListView listView = (ListView)findViewById(R.id.MyWorkoutlistView);
}
// method which will handle dynamic insertion
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
protected ListView getListView(){
if (mListView==null){
mListView = (ListView) findViewById(R.id.MyWorkoutlistView);
}
return mListView;
}
protected void setListAdapter(ListAdapter adapter){
getListView().setAdapter(adapter);
}
protected ListAdapter getListAdapter(){
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter){
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
I have a list view populated with exercises. I would like to randomly select an element in an array and add that random element to another list view.
Also is it possible to change the original exercise list view to display only the random element?
I am new to android studio, so if I am unclear let me know and I will explain better. Thank you
I have a list view populated with exercises. I would like to randomly select an element in an array and add that random element to another list view.
You can:
Random.nextInt()
arr[selectedPosition]
ListView
's adapter, for instance with ArrayAdapter.add()
.If you don't use ArrayAdapter
, you need to implement your interface for adding elements, or you can create and set a brand new adapter with ListView.setAdapter()
.
Also is it possible to change the original exercise list view to display only the random element?
Sure but there is some work to do. If you use ArrayAdapter
, you can just clear()
the adapter, then add()
only the element you want to display. To not lose date, you can keep all the exercises in an array/list external to the adapters.
If you don't use ArrayAdapter
, and want to implement your custom logic, remember to call notifyDataSetChanged()
on the adapter after modifications, so that ListView
knows to refresh itself.
Check my answer here I explain how to add dynamically elements into a listview.
just in case...
private EditText editTxt;
private Button btn;
private ListView list;
private ArrayAdapter<String> adapter;
private ArrayList<String> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTxt = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
list = (ListView) findViewById(R.id.listView);
arrayList = new ArrayList<String>();
// Adapter: You need three parameters 'the context, id of the layout (it will be where the data is shown),
// and the array that contains the data
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
// Here, you set the data in your ListView
list.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// this line adds the data of your EditText and puts in your array
arrayList.add(editTxt.getText().toString());
// next thing you have to do is check if your adapter has changed
adapter.notifyDataSetChanged();
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With