android - 带有 BaseAdapter 的 Android ListView 中的 ImageButton

标签 android android-layout android-widget android-manifest

我已经实现了一个 BaseAdapter 并将该 BaseAdapter 绑定(bind)到我的 ListView。 ListView 每一行的布局都包含一个 ImageButton。我如何将该 ImageButton 绑定(bind)到点击监听器,然后使用该 ImageButton 调用新 Activity 。

最佳答案

这是一个示例适配器类。

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.amazon.mp3.AlbumDetail.Details;



public class CustomListAdapter extends BaseAdapter {
private  ArrayList<Details> allElementDetails;
private Context con; 
private LayoutInflater mInflater;
String temp;
public CustomListAdapter(Context context, ArrayList<Details> results) {
    allElementDetails = results;
    mInflater = LayoutInflater.from(context);
    con=context;
}
public int getCount() {
    return allElementDetails.size();        
}
public Object getItem(int position) {
    return allElementDetails.get(position);
}
public long getItemId(int position) {
    return position;
}
public View getView(final int position, View convertView, ViewGroup parent) 
{
                    //select ur xml file
    convertView = mInflater.inflate(R.layout.listview_elements, null);

    TextView textview1 = (TextView) convertView.findViewById(R.id.TextView01);
    TextView textview2 = (TextView) convertView.findViewById(R.id.TextView02);
    TextView textview3 = (TextView) convertView.findViewById(R.id.TextView03);
    Button buy=(Button)convertView.findViewById(R.id.buy_song_button);
    buy.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

        Intent intent=new   Intent(con,Buy_song.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

         con.startActivity(intent);


        }
            else
                Toast.makeText(con,"Not available for purchase", Toast.LENGTH_SHORT).show();

        }
    });

    if(allElementDetails.get(position).songname.contains("&amp;"))
    {
  temp=allElementDetails.get(position).songname.replace("&amp;", "and");
    textview1.setText(temp);
    }
    else
    textview1.setText(allElementDetails.get(position).songname);

    textview2.setText(allElementDetails.get(position).runtime);
    if(allElementDetails.get(position).price.startsWith("$"))
    textview3.setText(allElementDetails.get(position).price);
    else
           textview3.setText("Album only");
    return convertView;
}    

}

在此适配器中寻找购买按钮。

关于android - 带有 BaseAdapter 的 Android ListView 中的 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582345/

相关文章:

android - Google Play 开发者控制台 - 发生意外错误。请稍后再试

android - 我怎样才能在android中为textview实现这个自定义形状?

安卓 : split the screen in 2 equals parts with 2 listviews

android - 从小部件启动 Activity

java - 如何从 onResponse 返回值

android - SearchView 在一项 Activity 中,结果在另一项 Activity 中

android - 长时间运行的后台进程

java - 切换内容 View 时信息不会保存

java - 如何更改小部件启动器预览?

android - 谁在调用/启动/停止我的 Android 应用程序?