单击 ListActivity 中的事件侦听器 [重复]
Posted
技术标签:
【中文标题】单击 ListActivity 中的事件侦听器 [重复]【英文标题】:Click Event Listeners in a ListActivity [duplicate] 【发布时间】:2012-06-23 09:52:36 【问题描述】:可能重复:android: How to fire onListItemClick in Listactivity with buttons in list?
我已经开发了一个应用程序,我在其中制作了 ListActivity,其中自定义列表视图将显示自定义项目列表。一切都很顺利,但在这里我对 itemOnClickListner
感到困惑。如何在 listActivity 中添加 onclick 列表器?因为没有任何初始化的列表视图,我可以通过该列表视图控件设置列表器...我从here 找到了,但它也不适合我
:这里是代码::
package com.AppFavorits;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.Toast;
import com.FavoritesDB.CommentsDataSource;
import com.SharedDB.SharedCommentsDataSource;
public class Favorites extends ListActivity implements OnClickListener
protected static final String TAG = "Favorites";
CommentsDataSource datasource;
ListView lstFavrowlistv;
float[] rate;
static boolean[] bSelected;
static ArrayList<Comment> alPackagenm;
static ArrayList alAppName;
static String[] strAppnm;
Drawable[] alIcon;
ViewHolder holder;
static int sizeincrement = 1;
private SharedCommentsDataSource ShrdDatasource;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
protected void onResume()
super.onResume();
datasource = new CommentsDataSource(this);
datasource.open();
ShrdDatasource = new SharedCommentsDataSource(this);
alAppName = datasource.getAllComments();
alPackagenm = datasource.getAllPackage();
Log.i(TAG, "values >>>" + alAppName);
Log.i(TAG, "values >>>" + alPackagenm);
int inc = 0;
alIcon = new Drawable[200];
for (int i = 0; i < alPackagenm.size(); i++)
Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname);
for (int j = 0; j < GetAllApp.lstpinfo.size(); j++)
if (alPackagenm
.get(i)
.toString()
.equalsIgnoreCase(
GetAllApp.lstpinfo.get(j).pname.toString()))
alIcon[inc] = GetAllApp.lstpinfo.get(j).icon;
Log.i("TAG", "sqlPackagename"
+ alPackagenm.get(i).toString());
Log.i("TAG", "from getAllapp"
+ GetAllApp.lstpinfo.get(j).pname.toString());
inc++;
ArrayList<RowModel> list = new ArrayList<RowModel>();
ArrayList<Model> Mlist = new ArrayList<Model>();
rate = new float[alAppName.size()];
bSelected = new boolean[alAppName.size()];
Iterator itr = alAppName.iterator();
String strVal = null;
while (itr.hasNext())
strVal += itr.next().toString() + ",";
int lastIndex = strVal.lastIndexOf(",");
strVal = strVal.substring(0, lastIndex);
System.out.println("Output String is : " + strVal);
String strAr[] = strVal.split(",");
int Appinc = 0;
for (String s : strAr)
list.add(new RowModel(s));
Appinc += 1;
for (String s : strAr)
Mlist.add(new Model(s));
setListAdapter(new RatingAdapter(list, Mlist));
datasource.close();
class RowModel
String label;
float rating = 0.0f;
RowModel(String label)
this.label = label;
public String toString()
if (rating >= 3.0)
return (label.toUpperCase());
return (label);
private RowModel getModel(int position)
return (((RatingAdapter) getListAdapter()).getItem(position));
class RatingAdapter extends ArrayAdapter<RowModel>
private ArrayList<Model> mlist;
boolean[] checkBoxState;
RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist)
super(Favorites.this, R.layout.outbox_list_item,
R.id.txvxFavrowiconappname, list);
checkBoxState = new boolean[list.size()];
this.mlist = mlist;
public View getView(final int position, View convertView,
ViewGroup parent)
View row = super.getView(position, convertView, parent);
holder = (ViewHolder) row.getTag();
if (convertView == null)
holder = new ViewHolder(row);
row.setTag(holder);
else
row = convertView;
((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
.get(position));
RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener()
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch)
Integer myPosition = (Integer) ratingBar.getTag();
RowModel model = getModel(myPosition);
model.rating = rating;
rate[position] = rating;
;
holder.ratingBar1.setOnRatingBarChangeListener(l);
holder.chkbxFavrowsel
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
Model element = (Model) holder.chkbxFavrowsel
.getTag();
element.setSelected(buttonView.isChecked());
bSelected[position] = isChecked;
element.setsizeInc(sizeincrement);
// if (holder.chkbxFavrowsel.isChecked() ==
// isChecked)
ShrdDatasource.open();
ShrdDatasource.createComment(alAppName
.get(position).toString(),
"https://play.google.com/store/apps/details?id="
+ alPackagenm.get(position)
.toString(), String
.valueOf(rate[position]));
ShrdDatasource.close();
Log.i(TAG, "Check Position is " + position);
//
);
RowModel model = getModel(position);
ViewHolder holder = (ViewHolder) row.getTag();
holder.ratingBar1.setTag(new Integer(position));
holder.ratingBar1.setRating(model.rating);
holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
holder.txvxFavrowiconappname.setText(alAppName.get(position)
.toString());
holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
holder.chkbxFavrowsel.setTag(mlist.get(position));
return (row);
@Override
protected void onDestroy()
// TODO Auto-generated method stub
super.onDestroy();
ShrdDatasource.close();
@Override
public void onClick(View v)
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
.show();
Log.i(TAG, "Click fire");
更新::
package com.AppFavorits;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.FavoritesDB.CommentsDataSource;
import com.SharedDB.SharedCommentsDataSource;
public class Favorites extends ListActivity implements OnClickListener
protected static final String TAG = "Favorites";
CommentsDataSource datasource;
ListView lstFavrowlistv;
float[] rate;
static boolean[] bSelected;
static ArrayList<Comment> alPackagenm;
static ArrayList alAppName;
static String[] strAppnm;
Drawable[] alIcon;
ViewHolder holder;
static int sizeincrement = 1;
private SharedCommentsDataSource ShrdDatasource;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
protected void onResume()
super.onResume();
datasource = new CommentsDataSource(this);
datasource.open();
ShrdDatasource = new SharedCommentsDataSource(this);
alAppName = datasource.getAllComments();
alPackagenm = datasource.getAllPackage();
Log.i(TAG, "values >>>" + alAppName);
Log.i(TAG, "values >>>" + alPackagenm);
int inc = 0;
alIcon = new Drawable[200];
for (int i = 0; i < alPackagenm.size(); i++)
Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname);
for (int j = 0; j < GetAllApp.lstpinfo.size(); j++)
if (alPackagenm
.get(i)
.toString()
.equalsIgnoreCase(
GetAllApp.lstpinfo.get(j).pname.toString()))
alIcon[inc] = GetAllApp.lstpinfo.get(j).icon;
Log.i("TAG", "sqlPackagename"
+ alPackagenm.get(i).toString());
Log.i("TAG", "from getAllapp"
+ GetAllApp.lstpinfo.get(j).pname.toString());
inc++;
ArrayList<RowModel> list = new ArrayList<RowModel>();
ArrayList<Model> Mlist = new ArrayList<Model>();
rate = new float[alAppName.size()];
bSelected = new boolean[alAppName.size()];
Iterator itr = alAppName.iterator();
String strVal = null;
while (itr.hasNext())
strVal += itr.next().toString() + ",";
int lastIndex = strVal.lastIndexOf(",");
strVal = strVal.substring(0, lastIndex);
System.out.println("Output String is : " + strVal);
String strAr[] = strVal.split(",");
int Appinc = 0;
for (String s : strAr)
list.add(new RowModel(s));
Appinc += 1;
for (String s : strAr)
Mlist.add(new Model(s));
setListAdapter(new RatingAdapter(list, Mlist));
datasource.close();
class RowModel
String label;
float rating = 0.0f;
RowModel(String label)
this.label = label;
public String toString()
if (rating >= 3.0)
return (label.toUpperCase());
return (label);
private RowModel getModel(int position)
return (((RatingAdapter) getListAdapter()).getItem(position));
class RatingAdapter extends ArrayAdapter<RowModel> implements OnClickListener
private ArrayList<Model> mlist;
boolean[] checkBoxState;
RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist)
super(Favorites.this, R.layout.outbox_list_item,
R.id.txvxFavrowiconappname, list);
checkBoxState = new boolean[list.size()];
this.mlist = mlist;
public View getView(final int position, View convertView,
ViewGroup parent)
View row = super.getView(position, convertView, parent);
holder = (ViewHolder) row.getTag();
if (convertView == null)
holder = new ViewHolder(row);
row.setTag(holder);
else
row = convertView;
((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
.get(position));
RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener()
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch)
Integer myPosition = (Integer) ratingBar.getTag();
RowModel model = getModel(myPosition);
model.rating = rating;
rate[position] = rating;
;
holder.ratingBar1.setOnRatingBarChangeListener(l);
holder.chkbxFavrowsel
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
Model element = (Model) holder.chkbxFavrowsel
.getTag();
element.setSelected(buttonView.isChecked());
bSelected[position] = isChecked;
element.setsizeInc(sizeincrement);
// if (holder.chkbxFavrowsel.isChecked() ==
// isChecked)
ShrdDatasource.open();
ShrdDatasource.createComment(alAppName
.get(position).toString(),
"https://play.google.com/store/apps/details?id="
+ alPackagenm.get(position)
.toString(), String
.valueOf(rate[position]));
ShrdDatasource.close();
Log.i(TAG, "Check Position is " + position);
//
);
RowModel model = getModel(position);
ViewHolder holder = (ViewHolder) row.getTag();
holder.ratingBar1.setTag(new Integer(position));
holder.ratingBar1.setRating(model.rating);
holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
holder.txvxFavrowiconappname.setText(alAppName.get(position)
.toString());
holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
holder.chkbxFavrowsel.setTag(mlist.get(position));
return (row);
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"hey this ", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Click this");
@Override
protected void onDestroy()
// TODO Auto-generated method stub
super.onDestroy();
ShrdDatasource.close();
@Override
public void onClick(View v)
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
.show();
Log.i(TAG, "Click fire");
更新3
package com.AppFavorits;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.Toast;
import com.FavoritesDB.CommentsDataSource;
import com.SharedDB.SharedCommentsDataSource;
public class Favorites extends Activity implements OnClickListener, OnItemClickListener
protected static final String TAG = "Favorites";
CommentsDataSource datasource;
ListView lstFavrowlistv;
float[] rate;
static boolean[] bSelected;
static ArrayList<Comment> alPackagenm;
static ArrayList alAppName;
static String[] strAppnm;
Drawable[] alIcon;
ViewHolder holder;
static int sizeincrement = 1;
private SharedCommentsDataSource ShrdDatasource;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites);
lstFavrowlistv = (ListView)findViewById(R.id.lstFavrowlistv);
lstFavrowlistv.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng)
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
.show();
Log.i(TAG, "Click fire");
);
@Override
protected void onResume()
super.onResume();
datasource = new CommentsDataSource(this);
datasource.open();
ShrdDatasource = new SharedCommentsDataSource(this);
alAppName = datasource.getAllComments();
alPackagenm = datasource.getAllPackage();
Log.i(TAG, "values >>>" + alAppName);
Log.i(TAG, "values >>>" + alPackagenm);
int inc = 0;
alIcon = new Drawable[200];
for (int i = 0; i < alPackagenm.size(); i++)
Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname);
for (int j = 0; j < GetAllApp.lstpinfo.size(); j++)
if (alPackagenm
.get(i)
.toString()
.equalsIgnoreCase(
GetAllApp.lstpinfo.get(j).pname.toString()))
alIcon[inc] = GetAllApp.lstpinfo.get(j).icon;
Log.i("TAG", "sqlPackagename"
+ alPackagenm.get(i).toString());
Log.i("TAG", "from getAllapp"
+ GetAllApp.lstpinfo.get(j).pname.toString());
inc++;
ArrayList<RowModel> list = new ArrayList<RowModel>();
ArrayList<Model> Mlist = new ArrayList<Model>();
rate = new float[alAppName.size()];
bSelected = new boolean[alAppName.size()];
Iterator itr = alAppName.iterator();
String strVal = null;
while (itr.hasNext())
strVal += itr.next().toString() + ",";
int lastIndex = strVal.lastIndexOf(",");
strVal = strVal.substring(0, lastIndex);
System.out.println("Output String is : " + strVal);
String strAr[] = strVal.split(",");
int Appinc = 0;
for (String s : strAr)
list.add(new RowModel(s));
Appinc += 1;
for (String s : strAr)
Mlist.add(new Model(s));
lstFavrowlistv.setAdapter(new RatingAdapter(list, Mlist));
datasource.close();
class RowModel
String label;
float rating = 0.0f;
RowModel(String label)
this.label = label;
public String toString()
if (rating >= 3.0)
return (label.toUpperCase());
return (label);
private RowModel getModel(int position)
return (((RatingAdapter) lstFavrowlistv.getAdapter()).getItem(position));
class RatingAdapter extends ArrayAdapter<RowModel> implements OnClickListener
private ArrayList<Model> mlist;
boolean[] checkBoxState;
RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist)
super(Favorites.this, R.layout.outbox_list_item,
R.id.txvxFavrowiconappname, list);
checkBoxState = new boolean[list.size()];
this.mlist = mlist;
public View getView(final int position, View convertView,
ViewGroup parent)
View row = super.getView(position, convertView, parent);
holder = (ViewHolder) row.getTag();
if (convertView == null)
holder = new ViewHolder(row);
row.setTag(holder);
else
row = convertView;
((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
.get(position));
RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener()
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch)
Integer myPosition = (Integer) ratingBar.getTag();
RowModel model = getModel(myPosition);
model.rating = rating;
rate[position] = rating;
;
holder.ratingBar1.setOnRatingBarChangeListener(l);
holder.chkbxFavrowsel
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
Model element = (Model) holder.chkbxFavrowsel
.getTag();
element.setSelected(buttonView.isChecked());
bSelected[position] = isChecked;
element.setsizeInc(sizeincrement);
// if (holder.chkbxFavrowsel.isChecked() ==
// isChecked)
ShrdDatasource.open();
ShrdDatasource.createComment(alAppName
.get(position).toString(),
"https://play.google.com/store/apps/details?id="
+ alPackagenm.get(position)
.toString(), String
.valueOf(rate[position]));
ShrdDatasource.close();
Log.i(TAG, "Check Position is " + position);
//
);
RowModel model = getModel(position);
ViewHolder holder = (ViewHolder) row.getTag();
holder.ratingBar1.setTag(new Integer(position));
holder.ratingBar1.setRating(model.rating);
holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
holder.txvxFavrowiconappname.setText(alAppName.get(position)
.toString());
holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
holder.chkbxFavrowsel.setTag(mlist.get(position));
return (row);
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"hey this ", Toast.LENGTH_SHORT).show();
Log.i(TAG, "Click this");
@Override
protected void onDestroy()
// TODO Auto-generated method stub
super.onDestroy();
ShrdDatasource.close();
@Override
public void onClick(View v)
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
.show();
Log.i(TAG, "Click fire");
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG)
.show();
Log.i(TAG, "Click fire");
【问题讨论】:
访问 [***.com/questions/1821871/… 这肯定有助于完成它。 [1]:***.com/questions/1821871/… @domnic 我已经更新了我的代码,请检查一下,它不工作 一个简单的问题:你为什么要把所有的东西都写在onResume()
?
@PareshMayani 因为我已经在应用程序中使用了 sqlitedb,并且需要将数据从第一个标签传递到下一个标签,所以每次我需要在 onResume 中做。
【参考方案1】:
在list Activity中使用getListview()来获取List............
在 Oncreate 中
ListView lv = getListView();
http://www.mkyong.com/android/android-listview-example/
此链接有两种方式
1- overriding onListItemClick(
2- Setting you listener..
【讨论】:
我已经更新了我的代码,请检查一下 对不起你做了什么? public void onClick(View v) 这是吗? 我认为您需要覆盖链接中给出的 onListItemClick 我试过了,但还是不行 嗨!我有更新....在此更新中,我通过简单的活动进行了更改,并在应用程序中制作了 listview 控件,但在这种情况下也会发生同样的情况【参考方案2】:试试这个方法..
ListView lv = getListView();
lv. storelist.setOnItemClickListener(new AdapterView.OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
这对你有帮助。
谢谢
【讨论】:
我已经更新了我的代码,请检查一下 嗨!我有更新....在此更新中,我通过简单的活动进行了更改,并在应用程序中制作了 listview 控件,但在这种情况下也会发生同样的情况【参考方案3】:为此重写函数onlistitemclick()。这里的整数位置代表你按下的项目的位置
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
.......
【讨论】:
我已经更新了我的代码,请检查一下 您好转到此链接link。尝试使用 BaseAdapter 嗨!我有更新....在此更新中,我通过简单的活动进行了更改,并在应用程序中制作了 listview 控件,但在这种情况下也会发生同样的情况【参考方案4】:试试这个
getListView().setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
// TODO Auto-generated method stub
);
【讨论】:
我已经更新了我的代码,请检查一下 试试这个绝对适合你 我在onCreate and
onResume` 中尝试过这个,但它不起作用
嗨!我有更新....在此更新中,我通过简单的活动进行了更改,并在应用程序中制作了 listview 控件,但在这种情况下也会发生同样的情况以上是关于单击 ListActivity 中的事件侦听器 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
无法让 onListItemClick 为 ListActivity 中的一行运行