添加一个ListView到Case语句 [英] Adding a Listview to Case Statement

查看:233
本文介绍了添加一个ListView到Case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩弄这个code好几天,现在,如果有人可以提供帮助。我有10个ImageButton的一个布局我有一个case语句这需要them.What我需要照顾的所有按钮点击是让所有的按钮里面一个ListView时clicked.Now我从Ridcully一些好的code
列表视图在多个按钮点击的。我似乎可以得到它的工作。

I have been playing around with this code for days now if someone can help. I have 10 imagebutton in one layout i have all the button clicks in a case statement which takes care of them.What i need is to have a listview inside all the buttons when clicked.Now i have some good code from Ridcully Listview On Multiple Button Clicks. I just can seem to get it to work

FirstActivity

FirstActivity

                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

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

当我启动的按钮,点击的作品,但没有列表视图??

when i start it the button click works but no listview ??

推荐答案

您必须使用 listView.setListAdapter(适配器); 如图所示的例子code我的回答你的previous问题。你调用一个什么也不做你自己的 setListAdapter 方法,因此的ListView 无适配器可以获取数据。

You have to use listView.setListAdapter(adapter); as shown in the example code of my answer to your previous question. You're calling your own setListAdapter method which does nothing, so the listView has no adapter to get the data from.

这篇关于添加一个ListView到Case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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