打开多个列表视图项单击到一个类 [英] Open Multiple Listview Item Click to One Class

查看:30
本文介绍了打开多个列表视图项单击到一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望大家帮忙.

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

I have a activity which handles all the 10 image button clicks and list view intents. What i am looking to do is have 1 layout for all the list view button clicks. And in this layout call different data to it. When i started this project i had many activitys until a great stack overflow user pointed out that i can make it simpler which i did and made it a lot clear.

  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 later
private 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 images
private 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 listviews
private 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 index
static 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
}
}

如果有人能告诉我如何制作一个课程,我也可以从我的代码中调用所有列表视图中的所有项目点击,那就太好了.

It would be great if someone can show me how to make a class which i can call all the item clicks from all list views too from my code here.

 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);
}

@Override
public 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

推荐答案

如果我明白你想要什么,你可以创建一个类,在每次添加一个项目时附加一个 static Arraylist点击.所以创建一个类似

If I understand what you want, you could create a class with something like a static Arraylist to be appended each time an item is clicked. So create a class something like

public class Data class
{
    static ArrayList<String> dataArray = new ArrayList<String>();;

    public Data()
    {
        // empty constructor but could be used if needed
    }

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

Then you can add different getters/setters or whatever you need here. When you click on an item you just call something like

Data.dataArray.add("stuff");

然后在您的下一个 Activity 中检索此处的项目.

then retrieve items in here in your next Activity.

如果这太复杂或超出您的需要,那么您可以通过 Intent

If that is too complicated or more than you need then you can just pass an ArrayList or whatever object you need through the Intent

意图

另外,这只是一个偏好,但由于您所有的 Button 都做同样的事情,您可以不用初始化它们并在所有它们上设置 listeners.在xml中只需添加

Also, just a preference but since all of your Buttons do the same thing, you can do away with initializing them and setting listeners on all of them. In xml just add

`android:onClick="someFunctionName"`

到每个Button然后使用那个函数名

to each Button then use that function name

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 中的 implements OnClickListener> 声明

There is also no need for the case statements since they all do the same thing and you don't need implements OnClickListener in the Activity declaration

这篇关于打开多个列表视图项单击到一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆