我正在尝试为项目内部button
实现 Onclicklistener Listview
但无法获取在列表视图中调用的 OnitemClick 方法。
我从stack overflow
那里得到了以下代码片段
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
( break point)
click++;
}
});
当我单击按钮时,断点永远不会消失。
活动代码
public class MainActivity extends AppCompatActivity {
long click=0;
String[] aOrderID = {"1-AA-355433", "2-AA-456474", "3-AA-43546464", "4-A-446464E6","5-A-43555" };
String[] aOrderType = {
"LIMIT-BUY",
"LIMIT-SELL",
"LIMIT-BUY",
"LIMIT-SELL",
"LIMIT-BUY"
};
double[] aPrice={3353.33, 4567.77, 335.66, 535.77, 678.67};
double[] aVOL={93353.33, 94567.77, 9335.66, 9535.77, 9678.67};
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomListAdapter whatever = new CustomListAdapter(this,
aOrderID, aOrderType,aPrice, aVOL );
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(whatever);
////////////////////////
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
// Your code for item clicks
// CODE NEVER GETS HER AFTER BUTTON IS CLICKED ON
click++;
}
});
}
}
定制适配器
public class CustomListAdapter extends ArrayAdapter {
int ted;
//to reference the Activity
private final Activity context;
//to store the animal images
private Integer[] imageIDarray = null;
//List of order ids
private String[] aOrderID = null;
// Type of order id limit buy
private String[] aOrderType = null;
// Price, price to sell of buy at
private double[] aPrice = null;
// Amount of bitcoins to buy or sell
private double[] aVol = null;
public CustomListAdapter(Activity context,
String[] aOrderID, String[] aOrderType,
double[] aPrice, double[] aVol) {
super(context, R.layout.row2, aOrderID);
this.context = context;
this.aOrderID = aOrderID;
this.aOrderType = aOrderType;
this.aPrice = aPrice;
this.aVol = aVol;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.row2, null, true);
TextView temp;
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderType[position]);
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderID[position]);
temp = (TextView) rowView.findViewById(R.id.idVol);
temp.setText(Double.toString(aVol[position]));
temp = (TextView) rowView.findViewById(R.id.idPrice);
temp.setText(Double.toString(aPrice[position]));
return rowView;
}
}
用于列表视图的 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"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROW!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROW2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView33" />
<TextView
android:id="@+id/textView35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Oder Type "
app:layout_constraintStart_toEndOf="@+id/textView33"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/IDtYPE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Limit Order"
app:layout_constraintStart_toEndOf="@+id/textView35"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Volume "
app:layout_constraintStart_toEndOf="@+id/textView34"
app:layout_constraintTop_toBottomOf="@+id/textView35" />
<TextView
android:id="@+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type::"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/idOrderID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Limmit"
app:layout_constraintStart_toEndOf="@+id/textView40"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView40" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:layout_marginTop="4dp"
android:text="Delete Order"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView34" />
<TextView
android:id="@+id/textView45"
android:layout_width="wrap_content"
android:layout_height="17dp"
android:text="-------------------------------------------------"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<TextView
android:id="@+id/idPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="000.00"
app:layout_constraintStart_toEndOf="@+id/textView43"
app:layout_constraintTop_toBottomOf="@+id/idOrderID" />
<TextView
android:id="@+id/idVol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2.9999"
app:layout_constraintStart_toEndOf="@+id/textView37"
app:layout_constraintTop_toBottomOf="@+id/textView35" />
您应该使用此方法中的按钮单击事件。试试这个。
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.row2, null, true);
TextView temp;
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderType[position]);
temp = (TextView) rowView.findViewById(R.id.idOrderID);
temp.setText(aOrderID[position]);
temp = (TextView) rowView.findViewById(R.id.idVol);
temp.setText(Double.toString(aVol[position]));
temp = (TextView) rowView.findViewById(R.id.idPrice);
temp.setText(Double.toString(aPrice[position]));
Button btn = (Button) rowView.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do your stuff.
}
});
return rowView;
}