如何在ListView上生成的按钮上设置onclick事件?
我有这个活动,
BB_NO_NETWORK
这是显示ListView的活动。 这将显示按钮列表。
这是我的每周xml,
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();
}
});
}
}
接下来是我的simplerowbtn 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>
我现在需要的是允许用户点击按钮并根据点击的按钮显示具有不同值的新页面。如何在按钮上添加onClick事件?
尝试使用setOnItemClickListener,代码已更新。