列表视图在多个按钮点击声 [英] Listview On Multiple Button Clicks

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

问题描述

我有一个activity.Each按钮10图像按钮被点击时会开始在此布局,我想有一个列表视图不填写完整layout.So一个新的活动猜它必须是一个自定义之一。

I have 10 image buttons in one activity.Each button when clicked will start a new activity in this layout i want to have a list view that doesn't fill the full layout.So am guessing it would have to be a custom one.

  package com.baha.beallhasall;

  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 FirstActivityPage extends Activity implements OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_one);
    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()){
        case R.id.imageButton1:
            Intent intent0 = new Intent(FirstActivityPage.this, FourActivityPage.class);
            startActivity(intent0);
            break;
        case R.id.imageButton2:
            Intent intent1 = new Intent(FirstActivityPage.this, FourActivityPage.class);
            startActivity(intent1);
            break;
        case R.id.imageButton3:
            Intent intent2 = new Intent(FirstActivityPage.this, FiveActivityPage.class);
            startActivity(intent2);
            break;
        case R.id.imageButton4:
            Intent intent3 = new Intent(FirstActivityPage.this, SixActivityPage.class);
            startActivity(intent3);
            break;
        case R.id.imageButton5:
            Intent intent4 = new Intent(FirstActivityPage.this, SevenActivityPage.class);
            startActivity(intent4);
            break;
        case R.id.imageButton6:
            Intent intent5 = new Intent(FirstActivityPage.this, ThirdActivityPage.class);
            startActivity(intent5);
            break;
    }
   }

private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
    // TODO Auto-generated method stub

}

private ListView getListView() {
    // TODO Auto-generated method stub
    return null;
}
     }

这是一个具有上点击我的buttons.Is有可能有我我所有图像按钮活动的列表视图中有自己的阶级,而不是在这种情况下statement.Because我想这是我在这里我第一次活动与所有的code的按钮是好的,但我真的不认为一个人的类应该处理所有这些code.So这是这一轮最好的方法。

This is my first activity that has the on clicks for my buttons.Is it possible to have my list view for all my image button activity's in there own class and not in this case statement.Because am thinking this what i have here with all the code for buttons is fine but i don't really think that one class should handle all this code.So which is the best way round this.

http://img17.imageshack.us/img17/4936/0b8o.jpg 此图片只是我的项目布局。

http://img17.imageshack.us/img17/4936/0b8o.jpg This image is just my project layout.

http://img594.imageshack.us/img594/9373/a7l8.png 此图片是我试图用言语,但正如我在code世界是新的更容易,你们展现这种方式。

http://img594.imageshack.us/img594/9373/a7l8.png This image is what am trying to put in words but as i am new in this code world its easier to show you guys this way.

感谢

NEW code

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

listviewact

listviewact

        package com.example.testtest;

  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.ListView;

   public class Listviewact extends Activity {

public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.listview_layout);
  }

  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);
    setListAdapter(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.ic_launcher,
    R.drawable.ic_launcher,
    R.drawable.ic_launcher,
    R.drawable.ic_launcher,
    R.drawable.ic_launcher,
    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"},
    {"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
   }
   }

列表视图XML

Listview xml

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="55dp"
    android:src="@drawable/ic_launcher" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="125dp" >
  </ListView>

  </RelativeLayout>

我开始这个新项目,因为我不想搞糟我的其他project.I希望很接近,但是当我的第一个图像按钮点击没有任何反应。

I started this new project cos i didn't want to mess up my other project.I hope am close but when i click on the first image button nothing happens.

http://img836.imageshack.us/img836/1549/jo81.jpg
logcat的

http://img836.imageshack.us/img836/1549/jo81.jpg LOGCAT

推荐答案

你想达到什么是一个好主意的形象。

The image of what you want to achieve is a good idea.

我建议你只有一个用于显示在顶部的图像和底部的列表视图布局第二个活动。你可以通过在其上的图像显示在列表视图,从你的第一个(图像按钮)的活动,活动以显示其数据的信息。这是通过把信息放到Intent对象使用 putExtra()方法完成的。

I suggest you have only one second activity for showing your layout with the image on top and the listview at the bottom. You can pass information on which image to show and which data to show in the listview to that activity from your first (image-button) activity. This is done by putting the information into the Intent object using the putExtra() methods.

然后在第二个活动,你看从信息意图,并通过设置图像和初始化列表视图的适配器采取相应的行动。

Then in the second activity, you read that information from the Intent and act accordingly by setting the image and initializing the Adapter of the Listview.

顺便说一句,你并不需要一个自定义的的ListView 为布局,只需要使用的LinearLayout ,把一个的ImageView 的ListView 进去。

Btw., you do not need a custom ListView for that layout, just use a LinearLayout and put a ImageView and a ListView into it.

示例code

我添加了一些例如code,应解释如何做到这一点。它没有测试,所以很可能是在里面错别字或ommisions,但它至少应该给你一个起点和一个想法如何做到这一点。

I added some example code, that should explain how to do it. It's not tested, so there might well be typos or ommisions in there, but it should at least give you a start and an idea how to do it.

主要活动

Main Activity

在您的主要活动做这个,传递pressed按钮的ID一起到ListView活动。

In your main-activity do this, to pass the ID of the pressed button along to the listview-activity.

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, MyListViewActivity.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...        
    }
}

MyListViewActivity

MyListViewActivity

这将是你的第二个活动,我这里所说的ListViewActivity。在onResume(),我们为您在意图额外的数据(任何活动都通过它开始了一个Intent)。

This will be your second activity, which I called ListViewActivity here. In onResume() we check for the extra data in the intent (any Activity has an Intent by which it got started).

public MyListViewActivity extends Activity {

    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_mylistview);
    }

    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.imageView);
        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.listView);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
        listView.setListAdapter(adapter);
    }

    // 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.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
        R.drawable.image6
    };

    // 6 different sets of strings for the listviews
    private final static String[][] LISTVIEW_DATA = new String[][] {
        {"First A", "First B", "First C"},
        {"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
    }
}

* activity_mylistview.xml *

您在你的第二个画面想要的布局一个简单的例子,你要改善这一点。

A simple example for the layout you wanted in your second picture, you'll want to improve this.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>
    <ImageView
       android:id="imageView"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
    />
    <ListView
       android:id="listView"
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
    />
</LinearLayout>

这篇关于列表视图在多个按钮点击声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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