Android 打开多个Listview项单击一个类

Android 打开多个Listview项单击一个类,android,listview,Android,Listview,希望你们能帮忙 我有一个活动,处理所有10个图像按钮点击和列表视图意图。我想做的是为所有列表视图按钮点击1布局。在这个布局中,调用不同的数据。当我开始这个项目时,我有很多活动,直到一个伟大的堆栈溢出用户指出,我可以使它更简单,我做了,并使它非常清楚 package com.example.testtest; import android.app.Activity; import android.graphics.Typeface; import android.os.Bundl

希望你们能帮忙

我有一个活动,处理所有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 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
}
}
如果有人能告诉我如何创建一个类,我也可以从我的代码中调用所有列表视图中的所有项目点击,那就太好了

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

您正在使用ListView,但没有使用它的任何回调?这里,这是我用于ListView的代码。我把活动放在数组中,但你可以放任何东西。通过修改R.layout.mfd_视图,可以为每个列表项放置所需的内容。一个按钮,如果你需要的话。希望这有帮助。我还在学习自己

import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MyListFragment extends ListFragment {
String fragmentBackStack;
MyMapHandler handler;
OnViewSelectedListener mListener;

/**
 * An array of POJOs used to hold the info about the fragments we'll be
 * swapping between This should be inserted into an array adapter of some
 * sort before being passed onto ListAdapter
 */
private static final ViewDetails[] ACTIVITY_DETAILS = {
        new ViewDetails(R.string.action_largeTach,
                R.string.largeTach_description, LargeTachActivity.class),
        new ViewDetails(R.string.action_map, R.string.map_description,
                MyMapHandler.class),
        new ViewDetails(R.string.action_navigation,
                R.string.navigation_description, NavigationActivity.class),
        new ViewDetails(R.string.action_raceMode,
                R.string.raceMode_description, RaceModeActivity.class),
        new ViewDetails(R.string.action_settings,
                R.string.settings_description, SettingsFragment.class),
        new ViewDetails(R.string.action_extraInfo,
                R.string.extraInfo_description, ExtraInfoActivity.class) };

/**
 * @author PyleC1
 * 
 *         A POJO that holds a class object and it's resource info
 */
public static class ViewDetails {
    private final Class<? extends Activity> viewActivity;
    private int titleId;
    private int descriptionId;

    /**
     * @param titleId
     *            The resource ID of the string for the title
     * @param descriptionId
     *            The resource ID of the string for the description
     * @param activityClass
     *            The fragment's class associated with this list position
     */
    ViewDetails(int titleId, int descriptionId,
            Class<? extends Activity> viewActivity) {

        super();
        this.titleId = titleId;
        this.descriptionId = descriptionId;
        this.viewActivity = viewActivity;
    }

    public Class<? extends Activity> getViewActivity() {
        return viewActivity;
    }
}

/**
 * @author PyleC1
 * 
 *         Extends the ArrayAdapter class to support our custom array that
 *         we'll insert into the ListAdapter so the user can pick between
 *         MFD screens at boot time.
 */
private static class CustomArrayAdapter extends ArrayAdapter<ViewDetails> {
    public CustomArrayAdapter(Context context, ViewDetails[] activities) {
        super(context, R.layout.mfd_view, R.id.mfdTitle, activities);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MFDView mfdView;

        if (convertView instanceof MFDView) {
            mfdView = (MFDView) convertView;
        } else {
            mfdView = new MFDView(getContext());
        }

        ViewDetails details = getItem(position);

        mfdView.setTitleId(details.titleId);
        mfdView.setDescriptionId(details.descriptionId);

        return mfdView;
    }
}

public void onAttach(Activity activity) {
    super.onAttach(activity);

    ListAdapter listAdapter = new CustomArrayAdapter(getActivity(),
            ACTIVITY_DETAILS);
    setListAdapter(listAdapter);

    try {
        mListener = (OnViewSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnViewSelectedListener!");
    }
}

@Override
public void onResume() {
    super.onResume();
}

public interface OnViewSelectedListener {
    public void onViewSelected(Class<? extends Activity> activityClass);
}

public void onListItemClick(ListView l, View v, int position, long id) {
    ViewDetails details = (ViewDetails) getListAdapter().getItem(position);

    mListener.onViewSelected(details.viewActivity);
}
}

请注意,任何调用此片段的活动都必须实现OnViewSelectedListener接口。如果将其作为子类添加到主活动中,则不需要这样做。公共void onListItemClickarg0、arg1、arg2、arg3回调正常。您只需交换片段或从内部调用活动。

如果我了解您需要什么,您可以创建一个类,其中包含类似于静态Arraylist的内容,以便在每次单击某个项时追加该内容。所以创建一个类,比如

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

    public Data()
    {
        // empty constructor but could be used if needed
    }
Data.dataArray.add("stuff");
然后在下一个活动中检索此处的项目

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

此外,这只是一个首选项,但由于所有按钮都执行相同的操作,因此可以不用初始化它们并在所有按钮上设置侦听器。在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语句,因为它们都做相同的事情,并且在活动声明中不需要实现OnClickListener

当我单击任何一个imagebutton时,一切都很好活动打开很好,listview就在那里并且可以从这里开始工作我想从这里打开一个新的活动/布局所有的listview项目都已被锁定。如果我遗漏了什么,请让我知道,为未来干杯…我们最终会到达那里;那么您正试图根据用户选择的内容创建一个新的动态布局?您可能需要将一个包放在一起,并将其发送到新活动。然后在新活动的布局中,使用0dp和各种android:layout\u权重值设置视图容器。然后,您可以在新活动的onCreateBundle中显示/隐藏片段。在开始调用FragmentManager之前,请确保调用setContentViewint。我不清楚您到底想要什么。你想点击列表中的项目,然后点击一个按钮打开一个新的活动,对于所有点击过的项目?嗨,魔术师,我想要的是当我点击我的任何listview项目时,所有这些项目都将打开一个新的布局,在其中我可以使用不同的信息重复使用,所以所有项目点击都是一个类。或者,如果我让我自己更难,请让我知道。你肯定可以…我一直这样做。只要无论你点击什么,你都希望得到相同的布局,尽管它可能会略有不同,并且仍然是可管理的。你可以创建一个类来存储静态ArrayList或类似的东西,也可以通过Intent发送数据。extrasI在我的脑海中有一个图像,上面的布局应该是什么样子…如果可以的话,我可以这样做。我希望有静态文本,并在其旁边调用不同的文本。你能帮我吗,伙计?我不介意帮你但是你需要开始,告诉我们你被困在哪里。我已经发布了一个答案,可能会帮助你开始。有太多的可能性取决于你到底需要什么。我一直在努力让事情发生,但我就是不明白。我在想我想要做什么,但我对编码还不熟悉。我很难解释我想要的是什么。这就是我现在想做的魔术。我已经完成了前两张图片,但这是我发现很难编码的最后一个类/图片。最后一个类将有不同的文本/数据/主题信息,从列表视图点击第二张图片。这就是为什么我问这个问题,因为我不想用自己的数据/信息创建50个类。我觉得有很多方法可以创建一个类,只需更改视图或在textview中插入不同的数据。你给我的代码一开始可能就是我需要的,只是我不明白,我想我有点明白你想要什么。使用第一个示例中的类应该可以。单击某个项目时,关联的数据存储在该类中。很难说如何设置它,因为我不知道你需要什么。你可以自己动手
不同的字符串、int、ArrayList、HashMap`……这取决于什么适合您的情况。这是一种将数据与表示级别分开的好方法,您的意思是您被阻止了?我不明白为什么,因为你没有那么多反对票,你已经接受了答案。据我所知,唯一的办法就是花时间,通过给出高质量的答案,努力建立知名度。我真的不知道为什么你会被阻止谢谢小费我会尽我最大的努力。我不知道我自己我的项目也刚刚步入正轨…干杯