Android:如何识别从自定义列表视图中单击的按钮

时间:2017-02-04 22:20:57

标签: android listview android-custom-view

我正在创建一个自定义列表视图:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_display_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.anandgupta.login.MainActivity">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/myListView" />
</RelativeLayout>

list_row.xml 仅包含TextView和按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_row_selector"
android:padding="8dp">

<!-- Movie Title -->
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="@dimen/title"
    android:textStyle="bold" />

<!-- Buy Ticket -->
<Button
    android:id="@+id/buyTicket"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Buy"
    android:onClick="buyTicket"  //Button calling buyTicket...Not working
    />

MainActivity.java

package com.anandgupta.login;


import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

ListView listView;
private List<Movie> movieList = new ArrayList<Movie>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Assume that MovieAdapter and Movie class is already created and its working fine. 
    MovieAdapter movieAdapter = new MovieAdapter(this,movieList); 

    listView = (ListView)findViewById(R.id.myListView);
    listView.setAdapter(movieAdapter);

}

public void buyTicket()  //Not working
{  
 Toast.makeText(MainActivity.this,"Ticket Booked",Toast.LENGTH_LONG).show();
}


}

MovieAdapter.java

package com.anandgupta.login;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;


public class MovieAdapter extends BaseAdapter {
private List<Movie> movieList;
private Activity activity;
private LayoutInflater layoutInflater;
public MovieAdapter(Activity activity, List<Movie> movieList) {
    this.movieList = movieList;
    this.activity = activity;
}

@Override
public Object getItem(int position) {
    return movieList.get(position);
}

@Override
public int getCount() {
    return movieList.size();
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(layoutInflater==null)
        layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView==null)
        convertView = layoutInflater.inflate(R.layout.list_row,null);

    TextView title = (TextView)convertView.findViewById(R.id.title);

    Movie m = movieList.get(position);

    title.setText(m.getMovieTitle());

    return convertView;
}

}

Movie.java

package com.anandgupta.login;

public class Movie {
private String title;

Movie()
{

}

public void setMovieTitle(String title)
{
    this.title = title;
}

public void getMovieTitle(String title)
{
    return title;
}
}

我可以看到自定义列表,但在尝试通过点击按钮调用 buyTicket 功能时,我的应用程序将关闭。 任何帮助将不胜感激。

编辑:问题是该方法未通过视图:

public void buyTicket(View view) { ....... }

这解决了上述问题,但现在如何唯一地知道点击列表中的哪个按钮。

2 个答案:

答案 0 :(得分:0)

您可以在适配器的getView()方法中使用Button Click事件。

Button btnBookTicket = (Button)convertView.findViewById(R.id.buyTicket);
btnBookTicket.setOnClickListener(new View.OnClickListener()
{    
    ((MainACtivity)activity).buyMovieTicket();
});

答案 1 :(得分:0)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(layoutInflater==null)
        layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView==null)
        convertView = layoutInflater.inflate(R.layout.list_row,null);

    TextView title = (TextView)convertView.findViewById(R.id.title);

    Button buyTicketButton = (Button)convertView.findViewById(R.id.buyTicket);

    Movie m = movieList.get(position);

    title.setText(m.getMovieTitle());

     buyTicketButton.setOnClickListener(new OnClickListener(){
    //..... your code goes here
});

    return convertView;
}