2
我已經實現了一個BaseAdapter並將該BaseAdapter綁定到了我的ListView。 ListView的每一行的佈局包含一個ImageButton。如何將ImageButton綁定到單擊偵聽器,然後使用該ImageButton來調用新的活動。ImageButton內部Android ListView與BaseAdapter
我已經實現了一個BaseAdapter並將該BaseAdapter綁定到了我的ListView。 ListView的每一行的佈局包含一個ImageButton。如何將ImageButton綁定到單擊偵聽器,然後使用該ImageButton來調用新的活動。ImageButton內部Android ListView與BaseAdapter
這裏是一個示例適配器類。
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("&"))
{
temp=allElementDetails.get(position).songname.replace("&", "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;
}
}
查找此適配器中的購買按鈕。