如何在适配器列表视图中添加的onclick按钮 [英] how add onclick button in adapter listview
问题描述
我的项目包含的ListView(homelistView),其中包含按钮(btnList)。
当我点击按钮(btnList)它必须去另一个活动。我尝试了很多,但我没有找到一个很好的例子。
请给我建议这方面一个很好的例子。 我的按钮(btnList)上点击不工作PLZ sujject我的错误和放大器;它的解决方案PLZ ..感谢ü提前。
编辑:
在术后第2天的斗争我得到了总产量的70%,清楚地看到最后就是这个链接按钮适配器
它看起来像这样
-------------------------------- A--
文字文本按钮(btnList)乙
- - - - - - - - - - - - - - - - C - -
文字文本按钮(btnList)D
-------------------------------- E--
EfficientAdapter.java
公共类EfficientAdapter扩展了BaseAdapter实现SectionIndexer {
私人字符串mSections =#ABCDEFGHIJKLMNOPQRSTUVWXYZ;
ArrayList的<患者GT; patientListArray;
私人LayoutInflater mInflater;
私人上下文的背景下;
受保护的ListView mListView;
私人诠释立场;
ViewHolder持有人;
公共EfficientAdapter(上下文的背景下){
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<患者GT;();
对于(JsonElement OBJ:Jarray){
患者patientList = gson.fromJson(OBJ,Patient.class);
patientListArray.add(patientList);
Log.i(patientList,patientListJson);
}
}
/ **
*排序patientListArray数据
* /
公共无效sortMyData(){
//排序patientListArray数据
Collections.sort(patientListArray,新的比较<对象>(){
@覆盖
公众诠释比较(对象01,对象02){
病人P1 =(患者)01;
患者P2 =(患者)O2;
返回p1.getName()与compareToIgnoreCase(p2.getName())。
}
});
}
公众诠释getCount将(){
返回patientListArray.size();
}
公共对象的getItem(INT位置){
返回的位置;
}
众长getItemId(INT位置){
返回的位置;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
this.positions =位置;
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.homemplebrowview,父母,假);
持有人=新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(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.text1.setText(Util.formatN2H(patientListArray.get(位置)
.getName()));
holder.text2.setText(patientListArray.get(位置).getMrnNumber());
holder.text3.setText(Util.formatN2H(patientListArray.get(位置)
.getRoom()));
holder.text4.setText(Util.formatN2H(patientListArray.get(位置)
.getAge()));
holder.text5.setText(Util.formatN2H(patientListArray.get(位置)
.getGender()));
holder.text6.setText(Util.formatN2H(patientListArray.get(位置)
.getWard()));
holder.text7.setText(Util.formatN2H(patientListArray.get(位置)
.getRoom()));
holder.text8.setText(Util.formatN2H(patientListArray.get(位置)
.getBed()));
// holder.btnList.setOnClickListener(新OnClickListener(试验));
holder.btnList.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
Toast.makeText(上下文,STAT,Toast.LENGTH_SHORT).show();
接下来的意图=新的意图(背景下,Home.class);
Log.i(next23,下一个+次);
context.startActivity(下);
}
});
返回convertView;
}
/ * OnItemClickListener测试=新OnItemClickListener(){
onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
((TextView中)view.findViewById(R.id.yourTextViewId))的getText();
{
//按钮btnList =(按钮)v.findViewById(R.id.listbutton);
btnList.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
Toast.makeText(上下文,STAT,Toast.LENGTH_SHORT).show();
接下来的意图=新的意图(背景下,Home.class);
Log.i(next23,下一个+次);
context.startActivity(下);
}
});
}
};
* /
静态类ViewHolder {
公众按钮btnList;
公众的TextView text8;
公众的TextView text7;
公众的TextView text6;
公众的TextView text5;
公共TextView的文本4;
公众的TextView文本1;
公众的TextView文本2;
公众的TextView文本3;
}
@覆盖
公共无效notifyDataSetChanged(){
super.notifyDataSetChanged();
}
/ *公共无效onItemClick(适配器视图<>为arg0,视图V,最终诠释的位置,
长ARG3)
{
holder.btnList =(按钮)v.findViewById(R.id.listbutton);
holder.btnList.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
Toast.makeText(上下文,STAT,Toast.LENGTH_SHORT).show();
接下来的意图=新的意图(背景下,Home.class);
Log.i(next23,下一个+次);
context.startActivity(下);
}
});
}
* /
公众诠释getPositionForSection(INT部分){
//排序patientListArray数据
sortMyData();
//如果没有为当前部分没有项目,previous节会
//选择
的for(int i =节; I> = 0;我 - ){
对于(INT J = 0; J< getCount将(); J ++){
如果(我== 0){
//对于数字部分
对于(INT K = 0; K< = 9; k ++){
如果(StringMatcher.match(
将String.valueOf(patientListArray.get(J)
.getName()。的charAt(0)),
将String.valueOf(K)))
复位J;
}
} 其他 {
如果(StringMatcher.match(
将String.valueOf(patientListArray.get(J).getName()
.charAt(0)),
将String.valueOf(mSections.charAt(I))))
复位J;
}
}
}
返回0;
}
公众诠释getSectionForPosition(INT位置){
返回0;
}
公共对象[] getSections(){
的String []部分=新的String [mSections.length()];
的for(int i = 0; I< mSections.length();我++)
区段[I] =将String.valueOf(mSections.charAt(ⅰ));
返回段;
}
}
您必须做出这样
一些变化 私人字符串mSections =#ABCDEFGHIJKLMNOPQRSTUVWXYZ;
ArrayList的<患者GT; patientListArray;
私人LayoutInflater mInflater;
私人上下文的背景下;
受保护的ListView mListView;
私人诠释立场;
ViewHolder持有人;
私人活动的活动;
和在构造
公共EfficientAdapter(活动行为){
mInflater = LayoutInflater.from(上下文);
//this.context =背景;
this.activity =行为;
他们意图改变上下文中的活动。
希望它可以帮助
My project contains listView(homelistView) that contains button(btnList).
When I click on button(btnList) it must go to another Activity. I tried a lot but I didn't find a good example.
Please suggest me a good example regarding this. my button(btnList) on click is not working plz sujject my mistake & solution for it plz.. Thank u in advance.
EDITED:
At last after 2 days of struggle i got 70% of Output clearly SEE is this link button in listview adapter
Its looks like this way
--------------------------------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;
}
}
You have to make some changes like
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ArrayList<Patient> patientListArray;
private LayoutInflater mInflater;
private Context context;
protected ListView mListView;
private int positions;
ViewHolder holder;
private Activity activity;
and in the constructor
public EfficientAdapter(Activity act) {
mInflater = LayoutInflater.from(context);
//this.context = context;
this.activity = act;
them in Intent change CONTEXT TO activity.
Hope it helps
这篇关于如何在适配器列表视图中添加的onclick按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!