问题描述
我的项目包含的ListView(homelistView)包含的按钮(btnList)。
当我点击按钮(btnList)它必须去另一个活动。我尝试了很多,但我没有找到一个很好的例子。
请给我建议这方面一个很好的例子。
下面是我的code:
下面是我的列表视图中包含按钮。当点击按钮就必须去其他活动
-------------------------------- A--
按钮(btnList)乙
- - - - - - - - - - - - - - - - C - -
按钮(btnList)D
-------------------------------- E--
homempleb.xml之前,我使用XML的code。 buttonlist工作对我罚款按Mohith维尔马样品
< ListView控件
机器人:ID =@ + ID / homelistView
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1.04
机器人:dividerHeight =0dip> < /&的ListView GT;
homempleb.xml目前我添加滚动索引列表视图我,改变了code按如下.. Listbutton是不是为我工作now..Plz帮助我
< com.woozzu.android.widget.IndexableListView 机器人:ID =@ + ID / homelistView
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1.04
机器人:dividerHeight =0dip>
< /com.woozzu.android.widget.IndexableListView>
MainActivity.java
公共类MainActivity扩展活动器具
SearchView.OnQueryTextListener,SearchView.OnCloseListener {私人的ListView ListView的;
//私人IndexableListView的ListView;
私人搜索查看搜索;
EfficientAdapter objectAdapter;
// EfficientAdapter2 objectAdapter1;
INT长度限制:Textlength = 0;
私人复选框checkStat,checkRoutine,checkTat;
私人的ArrayList<病人GT; patientListArray;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.homempleb);
Log.i(扫描,txtScanResult); ActionItem nextItem =新ActionItem();
最后QuickAction quickAction =新QuickAction(这一点,
QuickAction.VERTICAL);
quickAction.addActionItem(nextItem);
quickAction.setOnDismissListener(新QuickAction.OnDismissListener(){
@覆盖
公共无效onDismiss(){
Toast.makeText(getApplicationContext(),驳回,
Toast.LENGTH_SHORT).show();
}
}); 搜索=(搜索查看)findViewById(R.id.searchView1);
search.setIconifiedByDefault(假);
search.setOnQueryTextListener(本);
search.setOnCloseListener(本);
search.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
quickAction.show(五);
}
});
checkStat =(复选框)findViewById(R.id.checkBoxStat);
checkRoutine =(复选框)findViewById(R.id.checkBoxRoutine);
checkTat =(复选框)findViewById(R.id.checkBoxTat);
checkStat.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果(((复选框)ⅴ).isChecked()){
checkStat.setChecked(真);
Toast.makeText(MainActivity.this,STAT,
Toast.LENGTH_SHORT).show();
checkRoutine.setChecked(假);
checkTat.setChecked(假);
}
}
});
checkRoutine.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果(((复选框)ⅴ).isChecked()){
checkRoutine.setChecked(真);
Toast.makeText(MainActivity.this,例行公事,
Toast.LENGTH_SHORT).show();
checkStat.setChecked(假);
checkTat.setChecked(假);
}
}
});
checkTat.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果(((复选框)ⅴ).isChecked()){
checkTat.setChecked(真);
Toast.makeText(MainActivity.thisTAT效率研究,
Toast.LENGTH_SHORT).show();
checkRoutine.setChecked(假);
checkStat.setChecked(假);
}
}
}); // ListView控件=(IndexableListView)findViewById(R.id.homelistView);
ListView控件=(ListView控件)findViewById(R.id.homelistView);
listView.setTextFilterEnabled(真);
listView.setFastScrollEnabled(真);
listView.setFastScrollAlwaysVisible(真);
objectAdapter =新EfficientAdapter(本);
listView.setAdapter(objectAdapter); 按钮refreshButton =(按钮)findViewById(R.id.refreshButton);
refreshButton.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// objectAdapter1 =新EfficientAdapter2(MainActivity.this);
objectAdapter =新EfficientAdapter(MainActivity.this); //适配器
//使用
//新
//数据
listView.setAdapter(objectAdapter); Log.i(notifyDataSetChanged,数据更新);
// objectAdapter1.notifyDataSetChanged();
objectAdapter.notifyDataSetChanged(); }
});}@覆盖
公共布尔的OnClose(){
返回false;
}@覆盖
公共布尔onQueryTextChange(字符串newText){
返回false;
}@覆盖
公共布尔onQueryTextSubmit(查询字符串){
返回false;
}
}
EfficientAdapter.JAVA
公共类EfficientAdapter扩展BaseAdapter实现SectionIndexer {私人字符串mSections =#ABCDEFGHIJKLMNOPQRSTUVWXYZ;
ArrayList的<病人GT; patientListArray;私人LayoutInflater mInflater;
私人上下文的背景下;公共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){
ViewHolder持有人; 如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.homemplebrowview,NULL);
持有人=新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); holder.btnList.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
Toast.makeText(上下文,STAT,Toast.LENGTH_SHORT).show();
接下来意向=新意图(背景下,Home.class);
Log.i(next23,下一步);
context.startActivity(下);
}
}); 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(){ @覆盖
公共无效的onClick(视图v){意向书下一=新
意图(背景下,Home.class); context.startActivity(下);
}});
**
返回convertView;
}静态类ViewHolder {
公共按钮btnList;
公众的TextView text8;
公众的TextView text7;
公众的TextView text6;
公众的TextView text5;
公众的TextView文本4;
公众的TextView文本1;
公众的TextView文本2;
公众的TextView文字3;
}@覆盖
公共无效notifyDataSetChanged(){
super.notifyDataSetChanged();
}公众诠释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(ⅰ));
返回段;
} }
首先从MainActivity类别中删除btnList,btnScan。
在您EfficientAdapter类添加对象上下文语境
更改您的构造器:
公共EfficientAdapter(上下文的背景下){
mInflater = LayoutInflater.from(上下文);
}
到
公共EfficientAdapter(上下文的背景下){
mInflater = LayoutInflater.from(上下文);
this.context =背景;
}
在您的ViewHolder类,你需要添加按钮btnList。
然后在getview使用方法找到它的视图 holder.btnList
然后使用:
holder.btnList.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
接下来意向=新意图(背景下,SeviceDetails.class);
context.startActivity(下);
}
});
在回归前convertView您getview方法。
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.
Below is my code:
Here is my listview contains button. When on click of button it must go to other activity
--------------------------------A--
button(btnList) B
--------------------------------C---
BUTTON(btnList) D
--------------------------------E--
homempleb.xml Before i used this code in xml. buttonlist worked fine for me as per Mohith verma sample
<ListView
android:id="@+id/homelistView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.04"
android:dividerHeight="0dip" >
</ListView>
homempleb.xml Currently i added scroll index to my listview and changed the code as per below.. Listbutton is not working for me now..Plz help me
<com.woozzu.android.widget.IndexableListView
android:id="@+id/homelistView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.04"
android:dividerHeight="0dip" >
</com.woozzu.android.widget.IndexableListView>
MainActivity.java
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener, SearchView.OnCloseListener {
private ListView listView;
// private IndexableListView listView;
private SearchView search;
EfficientAdapter objectAdapter;
// EfficientAdapter2 objectAdapter1;
int textlength = 0;
private CheckBox checkStat, checkRoutine, checkTat;
private ArrayList<Patient> patientListArray;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homempleb);
Log.i("scan", " txtScanResult ");
ActionItem nextItem = new ActionItem();
final QuickAction quickAction = new QuickAction(this,
QuickAction.VERTICAL);
quickAction.addActionItem(nextItem);
quickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
@Override
public void onDismiss() {
Toast.makeText(getApplicationContext(), "Dismissed",
Toast.LENGTH_SHORT).show();
}
});
search = (SearchView) findViewById(R.id.searchView1);
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(this);
search.setOnCloseListener(this);
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
quickAction.show(v);
}
});
checkStat = (CheckBox) findViewById(R.id.checkBoxStat);
checkRoutine = (CheckBox) findViewById(R.id.checkBoxRoutine);
checkTat = (CheckBox) findViewById(R.id.checkBoxTat);
checkStat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
checkStat.setChecked(true);
Toast.makeText(MainActivity.this, "STAT",
Toast.LENGTH_SHORT).show();
checkRoutine.setChecked(false);
checkTat.setChecked(false);
}
}
});
checkRoutine.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
checkRoutine.setChecked(true);
Toast.makeText(MainActivity.this, "ROUTINE",
Toast.LENGTH_SHORT).show();
checkStat.setChecked(false);
checkTat.setChecked(false);
}
}
});
checkTat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
checkTat.setChecked(true);
Toast.makeText(MainActivity.this, "TAT Effeciency",
Toast.LENGTH_SHORT).show();
checkRoutine.setChecked(false);
checkStat.setChecked(false);
}
}
});
// listView = (IndexableListView) findViewById(R.id.homelistView);
listView = (ListView) findViewById(R.id.homelistView);
listView.setTextFilterEnabled(true);
listView.setFastScrollEnabled(true);
listView.setFastScrollAlwaysVisible(true);
objectAdapter = new EfficientAdapter(this);
listView.setAdapter(objectAdapter);
Button refreshButton = (Button) findViewById(R.id.refreshButton);
refreshButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// objectAdapter1 = new EfficientAdapter2(MainActivity.this);
objectAdapter = new EfficientAdapter(MainActivity.this);// adapter
// with
// new
// data
listView.setAdapter(objectAdapter);
Log.i("notifyDataSetChanged", "data updated");
// objectAdapter1.notifyDataSetChanged();
objectAdapter.notifyDataSetChanged();
}
});
}
@Override
public boolean onClose() {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
}
EfficientAdapter.JAVA
public class EfficientAdapter extends BaseAdapter implements SectionIndexer {
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ArrayList<Patient> patientListArray;
private LayoutInflater mInflater;
private Context context;
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) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.homemplebrowview, null);
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);
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");
context.startActivity(next);
}
});
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() {
@Override
public void onClick(View v) { Intent next=new
Intent(context, Home.class); context.startActivity(next);
} });
**
return convertView;
}
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 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;
}
}
First remove btnList, btnScan from your MainActivity class.
In your EfficientAdapter class add object Context Context
Change your contructor:
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
to:
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context=context;
}
In your ViewHolder class you need to add Button btnList.
Then in getview method find its view using holder.btnList
Then use:
holder.btnList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent next=new Intent(context, SeviceDetails.class);
context.startActivity(next);
}
});
in your getview method before return convertView.
这篇关于的onclick在列表视图android的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!