Android 如何在适配器列表视图中添加onclick按钮
我的项目包含包含按钮(btnList)的listView(homelistView) 当我点击按钮(btnList)时,它必须转到另一个活动。我试了很多,但没有找到一个好的例子 请给我举一个很好的例子。 点击时我的按钮(btnList)不起作用,请点击我的错误和解决方案。。先谢谢你 编辑: 经过两天的努力,我终于得到了70%的产出,清楚地看到了这一点 看起来是这样的Android 如何在适配器列表视图中添加onclick按钮,android,android-layout,android-intent,Android,Android Layout,Android Intent,我的项目包含包含按钮(btnList)的listView(homelistView) 当我点击按钮(btnList)时,它必须转到另一个活动。我试了很多,但没有找到一个好的例子 请给我举一个很好的例子。 点击时我的按钮(btnList)不起作用,请点击我的错误和解决方案。。先谢谢你 编辑: 经过两天的努力,我终于得到了70%的产出,清楚地看到了这一点 看起来是这样的 --------------------------------A-- text text button(bt
--------------------------------A--
text text button(btnList) B
--------------------------------C---
text text BUTTON(btnList) D
--------------------------------E--
EfficientAdapter.java
public class EfficientAdapter extends BaseAdapter implements SectionIndexer {
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ArrayList<Patient> patientListArray;
private LayoutInflater mInflater;
private Context context;
protected ListView mListView;
private int positions;
ViewHolder holder;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context = context;
String patientListJson = CountriesList.jsonData;
JSONObject jssson;
try {
jssson = new JSONObject(patientListJson);
patientListJson = jssson.getString("PostPatientDetailResult");
} catch (JSONException e) {
e.printStackTrace();
}
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray Jarray = parser.parse(patientListJson).getAsJsonArray();
patientListArray = new ArrayList<Patient>();
for (JsonElement obj : Jarray) {
Patient patientList = gson.fromJson(obj, Patient.class);
patientListArray.add(patientList);
Log.i("patientList", patientListJson);
}
}
/**
* sorting the patientListArray data
*/
public void sortMyData() {
// sorting the patientListArray data
Collections.sort(patientListArray, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
Patient p1 = (Patient) o1;
Patient p2 = (Patient) o2;
return p1.getName().compareToIgnoreCase(p2.getName());
}
});
}
public int getCount() {
return patientListArray.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
this.positions = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.homemplebrowview, parent,false);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.name);
holder.text2 = (TextView) convertView.findViewById(R.id.mrn);
holder.text3 = (TextView) convertView.findViewById(R.id.date);
holder.text4 = (TextView) convertView.findViewById(R.id.age);
holder.text5 = (TextView) convertView.findViewById(R.id.gender);
holder.text6 = (TextView) convertView.findViewById(R.id.wardno);
holder.text7 = (TextView) convertView.findViewById(R.id.roomno);
holder.text8 = (TextView) convertView.findViewById(R.id.bedno);
holder.btnList = (Button) convertView.findViewById(R.id.listbutton);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(Util.formatN2H(patientListArray.get(position)
.getName()));
holder.text2.setText(patientListArray.get(position).getMrnNumber());
holder.text3.setText(Util.formatN2H(patientListArray.get(position)
.getRoom()));
holder.text4.setText(Util.formatN2H(patientListArray.get(position)
.getAge()));
holder.text5.setText(Util.formatN2H(patientListArray.get(position)
.getGender()));
holder.text6.setText(Util.formatN2H(patientListArray.get(position)
.getWard()));
holder.text7.setText(Util.formatN2H(patientListArray.get(position)
.getRoom()));
holder.text8.setText(Util.formatN2H(patientListArray.get(position)
.getBed()));
// holder.btnList.setOnClickListener(new OnClickListener(test));
holder.btnList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "STAT", Toast.LENGTH_SHORT).show();
Intent next = new Intent(context, Home.class);
Log.i("next23", "next"+ next);
context.startActivity(next);
}
});
return convertView;
}
/*OnItemClickListener test = new OnItemClickListener() {
onItemClick(AdapterView<?> parent, View view, int position, long id){
((TextView)view.findViewById(R.id.yourTextViewId)).getText();
{
// Button btnList=(Button)v.findViewById(R.id.listbutton);
btnList.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(context, "STAT", Toast.LENGTH_SHORT).show();
Intent next = new Intent(context, Home.class);
Log.i("next23", "next"+ next);
context.startActivity(next);
}
});
}
};
*/
static class ViewHolder {
public Button btnList;
public TextView text8;
public TextView text7;
public TextView text6;
public TextView text5;
public TextView text4;
public TextView text1;
public TextView text2;
public TextView text3;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
/*public void onItemClick(AdapterView<?> arg0, View v, final int position,
long arg3)
{
holder.btnList=(Button)v.findViewById(R.id.listbutton);
holder.btnList.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast.makeText(context, "STAT", Toast.LENGTH_SHORT).show();
Intent next = new Intent(context, Home.class);
Log.i("next23", "next"+ next);
context.startActivity(next);
}
});
}
*/
public int getPositionForSection(int section) {
// sorting the patientListArray data
sortMyData();
// If there is no item for current section, previous section will be
// selected
for (int i = section; i >= 0; i--) {
for (int j = 0; j < getCount(); j++) {
if (i == 0) {
// For numeric section
for (int k = 0; k <= 9; k++) {
if (StringMatcher.match(
String.valueOf(patientListArray.get(j)
.getName().charAt(0)),
String.valueOf(k)))
return j;
}
} else {
if (StringMatcher.match(
String.valueOf(patientListArray.get(j).getName()
.charAt(0)),
String.valueOf(mSections.charAt(i))))
return j;
}
}
}
return 0;
}
public int getSectionForPosition(int position) {
return 0;
}
public Object[] getSections() {
String[] sections = new String[mSections.length()];
for (int i = 0; i < mSections.length(); i++)
sections[i] = String.valueOf(mSections.charAt(i));
return sections;
}
}
公共类EfficientAdapter扩展BaseAdapter实现SectionIndexer{
私有字符串mSections=“#abcdefghijklmnopqrstuvxyz”;
ArrayList耐心等待;
私人停车场;
私人语境;
受保护的ListView-mListView;
私人职位;
视窗座;
公共效率适配器(上下文){
mInflater=LayoutInflater.from(上下文);
this.context=上下文;
字符串patientListJson=CountriesList.jsonData;
JSONObject jssson;
试一试{
jssson=新的JSONObject(patientListJson);
patientListJson=jssson.getString(“PostPatientDetailResult”);
}捕获(JSONException e){
e、 printStackTrace();
}
Gson Gson=新的Gson();
JsonParser=新的JsonParser();
JsonArray Jarray=parser.parse(patientListJson).getAsJsonArray();
patientListArray=新建ArrayList();
for(JsonElement对象:Jarray){
Patient patientList=gson.fromJson(obj,Patient.class);
patientList.add(patientList);
Log.i(“patientList”,patientListJson);
}
}
/**
*对PatientListary数据进行排序
*/
公共无效sortMyData(){
//对PatientListary数据进行排序
Collections.sort(patientListArray,新的Comparator(){
@凌驾
公共整数比较(对象o1、对象o2){
患者p1=(患者)o1;
患者p2=(患者)o2;
返回p1.getName().compareToIgnoreCase(p2.getName());
}
});
}
public int getCount(){
返回patientListArray.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
这个位置=位置;
如果(convertView==null){
convertView=mInflater.inflate(R.layout.homemplebrowview,父级,false);
holder=新的ViewHolder();
holder.text1=(TextView)convertView.findViewById(R.id.name);
holder.text2=(TextView)convertView.findViewById(R.id.mrn);
holder.text3=(TextView)convertView.findViewById(R.id.date);
holder.text4=(TextView)convertView.findViewById(R.id.age);
holder.text5=(TextView)convertView.findViewById(R.id.gender);
holder.text6=(TextView)convertView.findViewById(R.id.wardno);
holder.text7=(TextView)convertView.findViewById(R.id.roomno);
holder.text8=(TextView)convertView.findViewById(R.id.bedno);
holder.btnList=(按钮)convertView.findViewById(R.id.listbutton);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.text1.setText(Util.format2h(patientListArray.get)(位置)
.getName());
holder.text2.setText(patientListArray.get(position.getMrnNumber());
holder.text3.setText(Util.format2h(patientListArray.get)(位置)
.getRoom());
holder.text4.setText(Util.format2h(patientListArray.get)(位置)
.getAge());
holder.text5.setText(Util.format2h(patientListArray.get)(位置)
.getGender());
holder.text6.setText(Util.format2h(patientListArray.get)(位置)
.getWard());
holder.text7.setText(Util.format2h(patientListArray.get)(位置)
.getRoom());
holder.text8.setText(Util.format2h(patientListArray.get)(位置)
.getBed());
//holder.btnList.setOnClickListener(新的OnClickListener(测试));
holder.btnList.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(上下文,“STAT”,Toast.LENGTH_SHORT).show();
下一个意图=新意图(上下文,Home.class);
Log.i(“next23”、“next”+next);
背景。开始触觉(下一步);
}
});
返回视图;
}
/*OnItemClickListener测试=新建OnItemClickListener(){
onItemClick(AdapterView父对象、视图、整型位置、长id){
((TextView)view.findViewById(R.id.yourTextViewId)).getText();
{
//按钮btnList=(按钮)v.findViewById(R.id.listbutton);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Toast.makeText(上下文,“STAT”,Toast.LENGTH_SHORT).show();
下一个意图=新意图(上下文,Home.class);
Log.i(“next23”、“next”+next);
背景。开始触觉(下一步);
}
});
}
};
*/
静态类视窗夹{
公共按钮列表;
公共文本视图文本8;
公共文本视图文本7;
公共文本视图文本6;
公共文本视图文本5;
公共文本视图文本4;
公共文本视图文本1;
公共文本视图文本2;
公共文本视图文本3;
}
@凌驾
public void notifyDataSetChanged(){
super.notifyDataSetChanged();
}
/*公共链接(AdapterView arg0,视图v,最终int位置,
长arg3)
{
holder.btnList=(按钮)v.findviewbyd(R.id.listbutton);
holder.btnList.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Toast.makeText(上下文,“STAT”,Toast.LENGTH_SHORT).show();
下一个意图=新意图(上下文,Home.class);
Log.i(“next23”、“next”+next);
背景。开始触觉(下一步);
class EfficientAdapter extends BaseAdapter {
Context con;
public EfficientAdapter(Context con) {
this.con = con;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) con
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.homemplebrowview, parent, false);
Button row = (Button) rowView.findViewById(R.id.listbutton);
final int n = position;
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent next = new Intent(context, Home.class);
startActivity(next);
}
});
return rowView;
}
}
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ArrayList<Patient> patientListArray;
private LayoutInflater mInflater;
private Context context;
protected ListView mListView;
private int positions;
ViewHolder holder;
private Activity activity;
public EfficientAdapter(Activity act) {
mInflater = LayoutInflater.from(context);
//this.context = context;
this.activity = act;