OnItemClick 永远不会被调用 ListView 中的按钮

Posted

技术标签:

【中文标题】OnItemClick 永远不会被调用 ListView 中的按钮【英文标题】:OnItemClick never being called for button in ListView 【发布时间】:2018-07-01 05:11:25 【问题描述】:

我正在尝试在 Listview 项目中为 button 实现 Onclicklistener,但无法在列表视图中调用 OnitemClick 方法。

我从stack overflow得到以下代码sn-p

    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_
android:layout_
tools:layout_editor_absoluteY="81dp">


<TextView
    android:id="@+id/textView33"
    android:layout_
    android:layout_
    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_
    android:layout_
    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_
    android:layout_
    android:text="  Oder Type    "
    app:layout_constraintStart_toEndOf="@+id/textView33"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/IDtYPE"
    android:layout_
    android:layout_
    android:text="Buy Limit Order"
    app:layout_constraintStart_toEndOf="@+id/textView35"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView37"
    android:layout_
    android:layout_
    android:text="  Volume  "
    app:layout_constraintStart_toEndOf="@+id/textView34"
    app:layout_constraintTop_toBottomOf="@+id/textView35" />


<TextView
    android:id="@+id/textView40"
    android:layout_
    android:layout_
    android:text="Type::"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/idOrderID"
    android:layout_
    android:layout_
    android:text="Buy Limmit"
    app:layout_constraintStart_toEndOf="@+id/textView40"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView43"
    android:layout_
    android:layout_
    android:text="Price  "
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView40" />


<Button
    android:id="@+id/button"
    android:layout_
    android:layout_
    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_
    android:layout_
    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_
    android:layout_
    android:text="000.00"
    app:layout_constraintStart_toEndOf="@+id/textView43"
    app:layout_constraintTop_toBottomOf="@+id/idOrderID" />

<TextView
    android:id="@+id/idVol"
    android:layout_
    android:layout_
    android:text="2.9999"
    app:layout_constraintStart_toEndOf="@+id/textView37"
    app:layout_constraintTop_toBottomOf="@+id/textView35" />

【问题讨论】:

【参考方案1】:

您应该使用此方法中的按钮单击事件。试试这个。

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;


【讨论】:

要从列表框中删除一个项目,我是否必须在我的 Activity 类中获取 onclick 事件?我怎么能从适配器类的 onCkick 事件中删除它?????? 您应该在活动类和适配器类之间创建接口,以通过适配器类对活动类进行更改。 还在学习java我不知道你在actity类和适配器类之间创建接口是什么意思?

以上是关于OnItemClick 永远不会被调用 ListView 中的按钮的主要内容,如果未能解决你的问题,请参考以下文章

S3.putObject - 回调永远不会被调用

QAbstractItemModel data() 永远不会被调用

Hibernate @PostLoad 永远不会被调用

Android onSystemUiVisibilityChange 永远不会被调用

SSE:“onmessage”永远不会被调用

意图服务永远不会被调用