android– 打开多个Listview项目点击一个类

android– 打开多个Listview项目点击一个类,第1张

概述希望你们能帮忙.我有一个活动,处理所有10个图像按钮点击和列表视图意图.我要做的是为所有列表视图按钮点击提供1个布局.并在此布局中调用不同的数据.当我开始这个项目时,我有很多活动,直到一个伟大的堆栈溢出用户指出我可以使它更简单,我做了,并使它很清楚.packagecom.exampl

希望你们能帮忙.

我有一个活动,处理所有10个图像按钮点击和列表视图意图.我要做的是为所有列表视图按钮点击提供1个布局.并在此布局中调用不同的数据.当我开始这个项目时,我有很多活动,直到一个伟大的堆栈溢出用户指出我可以使它更简单,我做了,并使它很清楚.

  package com.example.testtest;  import androID.app.Activity;  import androID.graphics.Typeface;  import androID.os.Bundle;  import androID.Widget.ArrayAdapter;  import androID.Widget.ImageVIEw;  import androID.Widget.ListVIEw;  import androID.Widget.TextVIEw;  public class ListvIEwact extends Activity {public voID onCreate(Bundle b) {    super.onCreate(b);    setContentVIEw(R.layout.ListvIEw_layout);    Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/AlexBrush-Regular-OTF.otf");    TextVIEw tv = (TextVIEw) findVIEwByID(R.ID.textVIEw1);    tv.setTypeface(tf);  }   public voID onResume() {    super.onResume();    int buttonID = getIntent().getIntExtra("buttonID", 0);    int buttonIDx = getbuttonIDx(buttonID);    // find and set image according to buttonID    int imageID = IMAGE_IDS[buttonIDx];        // image to show for given button    ImageVIEw imageVIEw = (ImageVIEw)findVIEwByID(R.ID.imageVIEw1);    imageVIEw.setimageResource(imageID);    // find and set ListvIEw imtes according to buttonID    String[] items = ListVIEW_DATA[buttonIDx]; // ListvIEw items to show for given button    ListVIEw ListVIEw = (ListVIEw)findVIEwByID(R.ID.ListVIEw1);    ArrayAdapter adapter = new ArrayAdapter(this, androID.R.layout.simple_List_item_1, items);    ListVIEw.setAdapter(adapter);}private voID setlistadapter(ArrayAdapter adapter) {    // Todo auto-generated method stub}// a little helper to map IDs to array indices // to be able to fetch the correct image and ListvIEw data laterprivate final static int[] button_IDS = new int[] {    R.ID.imagebutton1,     R.ID.imagebutton2,     R.ID.imagebutton3,     R.ID.imagebutton4,     R.ID.imagebutton5,     R.ID.imagebutton6};// 6 imagesprivate final static int[] IMAGE_IDS = new int[] {    R.drawable.bmw,    R.drawable.ford,    R.drawable.honda,    R.drawable.toy,    R.drawable.vok2,    R.drawable.ic_launcher};// 6 different sets of strings for the ListvIEwsprivate final static String[][] ListVIEW_DATA = new String[][] {    {"First A", "First B", "First C", "First D","First E","First F"},    {"Second A", "Second B", "Second C"},    {"Third A", "Third B", "Third C"},    {"Forth A", "Forth B", "Forth C"},    {"Fifth A", "Fifth B", "Fifth C"},    {"Sixth A", "Sixth B", "Sixth C"},};// map button ID to array indexstatic private int getbuttonIDx(int ID) {    for(int i = 0; i<button_IDS.length; i++) {        if (button_IDS[i] == ID) return i;    }    return 0;    // should not happen}}

如果有人可以告诉我如何创建一个类,我可以从我的代码中调用所有列表视图中的所有项目点击,这将是很棒的.

 package com.example.testtest;import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.ArrayAdapter;import androID.Widget.Imagebutton;import androID.Widget.ListVIEw;public class MainActivity extends Activity implements OnClickListener{@OverrIDe protected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.layout_of_button);Imagebutton btn1 = (Imagebutton)findVIEwByID(R.ID.imagebutton1);Imagebutton btn2 = (Imagebutton)findVIEwByID(R.ID.imagebutton2);Imagebutton btn3 = (Imagebutton)findVIEwByID(R.ID.imagebutton3);Imagebutton btn4 = (Imagebutton)findVIEwByID(R.ID.imagebutton4);Imagebutton btn5 = (Imagebutton)findVIEwByID(R.ID.imagebutton5);Imagebutton btn6 = (Imagebutton)findVIEwByID(R.ID.imagebutton6);btn1.setonClickListener(this);btn2.setonClickListener(this);btn3.setonClickListener(this);btn4.setonClickListener(this);btn5.setonClickListener(this);btn6.setonClickListener(this);}@OverrIDepublic voID onClick(VIEw v) {    switch(v.getID()) {    // if one of the image buttons is pressed...    case R.ID.imagebutton1:    case R.ID.imagebutton2:    case R.ID.imagebutton3:    case R.ID.imagebutton4:    case R.ID.imagebutton5:    case R.ID.imagebutton6:           Intent intent = new Intent(this, ListvIEwact.class);        // pass ID of pressed button to ListvIEw-activity        intent.putExtra("buttonID", v.getID());          startActivity(intent);        break;    // here you Could place handling of other clicks if necessary...            }}private voID setlistadapter(ArrayAdapter<String> arrayAdapter) {// Todo auto-generated method stub}private ListVIEw getListVIEw() {// Todo auto-generated method stub   return null; } }

干杯.

http://img40.imageshack.us/img40/705/f6h9.png

解决方法:

如果我理解你想要的东西,你可以创建一个类,每次点击一个项目时都会附加一个静态ArrayList.所以创建类似的类

public class Data class{    static ArrayList<String> dataArray = new ArrayList<String>();;    public Data()    {        // empty constructor but Could be used if needed    }

然后你可以在这里添加不同的getter / setter或者你需要的任何东西.当你点击一个项目时,你只需要调用类似的东西

Data.dataArray.add("stuff");

然后在下一个活动中检索此处的项目.

如果这太复杂或超出您的需要,那么您可以通过Intent传递一个ArrayList或任何您需要的对象

Intents

此外,只是一个偏好,但由于你的所有按钮做同样的事情,你可以取消初始化它们并在所有这些上设置监听器.在xml中添加

`androID:onClick="someFunctionname"`

然后使用该函数名称

public voID someFunctionname(VIEw v) {switch(v.getID()) {// if one of the image buttons is pressed...    Intent intent = new Intent(this, ListvIEwact.class);    // pass ID of pressed button to ListvIEw-activity    intent.putExtra("buttonID", v.getID());      startActivity(intent);    break;// here you Could place handling of other clicks if necessary...        }

也不需要case语句,因为它们都做同样的事情,并且你不需要在Activity声明中使用实现OnClickListener

总结

以上是内存溢出为你收集整理的android – 打开多个Listview项目点击一个类全部内容,希望文章能够帮你解决android – 打开多个Listview项目点击一个类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1101665.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存