Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击多个按钮时的Listview_Android - Fatal编程技术网

Android 单击多个按钮时的Listview

Android 单击多个按钮时的Listview,android,Android,我在一个活动中有10个图像按钮。单击每个按钮将在此布局中启动一个新的活动。我希望有一个列表视图不会填充整个布局。因此我猜它必须是自定义的 package com.baha.beallhasall; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.O

我在一个活动中有10个图像按钮。单击每个按钮将在此布局中启动一个新的活动。我希望有一个列表视图不会填充整个布局。因此我猜它必须是自定义的

  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;
}
     }
package com.baha.beallhasall;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.ImageButton;
导入android.widget.ListView;
公共类FirstActivityPage扩展了活动实现OnClickListener{
@凌驾
创建时受保护的void(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(此);
btn2.setOnClickListener(此);
btn3.setOnClickListener(此);
btn4.setOnClickListener(此);
btn5.setOnClickListener(此);
btn6.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.imageButton1:
Intent intent0=新Intent(FirstActivityPage.this,FourActivityPage.class);
起始触觉(intent0);
打破
案例R.id.imageButton2:
Intent intent1=新的Intent(FirstActivityPage.this,FourActivityPage.class);
星触觉(intent1);
打破
案例R.id.imageButton3:
Intent intent2=新的Intent(FirstActivityPage.this,FiveActivityPage.class);
星触觉(intent2);
打破
案例R.id.imageButton4:
Intent intent3=新的Intent(FirstActivityPage.this,sixtActivityPage.class);
起始触觉(intent3);
打破
案例R.id.imageButton5:
Intent intent4=新Intent(FirstActivityPage.this、seveActivityPage.class);
起始触觉(强度4);
打破
案例R.id.imageButton6:
Intent intent5=新的Intent(FirstActivityPage.this,ThirdActivityPage.class);
星触觉(intent5);
打破
}
}
私有void setListAdapter(ArrayAdapter ArrayAdapter){
//TODO自动生成的方法存根
}
私有ListView getListView(){
//TODO自动生成的方法存根
返回null;
}
}
这是我的第一个点击按钮的活动。我的所有图像按钮活动的列表视图是否可以在自己的类中,而不是在这个case语句中。因为我认为我在这里的所有按钮代码都很好,但我真的不认为一个类应该处理所有这些代码。那么这是什么呢这是最好的办法

这张图片只是我的项目布局

这张图片是我试图用文字表达的,但由于我是这个代码世界的新手,所以更容易用这种方式向你们展示

谢谢

新代码

      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;
 }
      }
package com.example.testtest;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.ImageButton;
导入android.widget.ListView;
公共类MainActivity扩展活动实现OnClickListener{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout\u按钮);
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(此);
btn2.setOnClickListener(此);
btn3.setOnClickListener(此);
btn4.setOnClickListener(此);
btn5.setOnClickListener(此);
btn6.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
//如果按下其中一个图像按钮。。。
案例R.id.imageButton1:
案例R.id.imageButton2:
案例R.id.imageButton3:
案例R.id.imageButton4:
案例R.id.imageButton5:
案例R.id.imageButton6:
Intent Intent=新的Intent(这个,Listviewact.class);
//将按下按钮的ID传递给listview活动
intent.putExtra(“buttonId”,v.getId());
星触觉(意向);
打破
//如有必要,您可以在此处放置其他单击的处理。。。
}
}
私有void setListAdapter(ArrayAdapter ArrayAdapter){
//TODO自动生成的方法存根
}
私有ListView getListView(){
//TODO自动生成的方法存根
返回null;
}
}
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
   }
   }
package com.example.testtest;
导入android.app.Activity;
导入android.os.Bundle;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.ListView;
公共类Listviewact扩展活动{
创建时的公共void(Bundle b){
super.onCreate(b);
setContentView(R.layout.listview\u布局);
}
恢复时公开作废(){
super.onResume();
int buttonId=getIntent().getIntExtra(“buttonId”,0);
int buttonix=getbuttonix(buttonId);
//根据buttonId查找并设置图像
int imageId=IMAGE_id[buttonix];//要为给定按钮显示的图像
ImageView图像视图=(图像视图
         <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>
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...        
    }
}
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
    }
}
<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>