How to set onclick event on buttons generated on ListView?
I have this Activity,
public class Weekly extends AppCompatActivity {
protected ListView planList = null;
private ArrayAdapter<String> listAdapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weekly);
planList = findViewById( R.id.planList );
displayList();
}
private void displayList(){
ArrayList<String> planListArray = new ArrayList<String>();
planListArray.add("Sunday");
planListArray.add("Monday");
planListArray.add("Tuesday");
planListArray.add("Wednesday");
planListArray.add("Thursday");
planListArray.add("Friday");
planListArray.add("Saturday");
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerowbtn, planListArray);
planList.setAdapter( listAdapter );
planList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
builder.setMessage(value);
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}
This is the activity where ListView is displayed. This will display list of buttons.
Here is my weekly xml,
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/color_gradient"
tools:context="bmicalculator.bmicalculator.Weekly">
<ListView
android:id="@+id/planList"
android:layout_width="338dp"
android:layout_height="442dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Next is my simplerowbtn xml,
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:color="#ffffff"
android:textSize="25sp"></Button>
What I need now is to allow user click the button and display a new page with different value based on button clicked. How can I add onClick event on the buttons?
Tried to use setOnItemClickListener, code updated.