如何分别为多个ListView实现onItemClick()? [英] How to Implement onItemClick() for multiple ListViews separately?

查看:40
本文介绍了如何分别为多个ListView实现onItemClick()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一本书开发一个应用程序,该应用程序有13个单元,每个单元包含许多练习,每个练习包含许多扫描图像.

I am developing an app for a book, which have 13 units, each unit contain many exercises and each exercise contains many scanned images.

现在我有一个Main Menu Activity(主菜单活动)和2个片段,fragmentA用于在ListView中显示练习,fragmentB用于显示练习的扫描图像.

Now I have a Main Menu Activity, and 2 fragments, fragmentA for showing the exercises in ListView and fragmentB for showing the scanned images of exercises.

我的主菜单活动包含13个按钮,每个单元的每个按钮用于加载该单元的练习.

My Main Menu Activity contain 13 buttons, each button for every unit, to load that unit's exercises.

我为每个单元分别创建了练习的ArrayList,以便在用户点击特定单元时将它们加载到fragmentA中.

I have created ArrayLists of exercises for each unit separately, to load them into fragmentA when user tap on a specific unit.

我已经在Main Menu Activity(主菜单活动)中为每个单元的按钮单击创建了一个Switch Case,它将特定单元的ArrayList加载到listView的fragmentA中.

I have created a Switch Case in the Main Menu Activity for button clicks of each unit, which loads the ArrayList of that specific unit in fragmentA in a listview.

Onbackpress会将用户从fragmentA带回到主菜单.

Onbackpress it takes user back from fragmentA to Main Menu.

现在,我正在尝试实现OnItemClick(),以实现对ListView的点击,这些点击已针对每个单元的每次练习加载到fragmentA中.

Now I am trying to implement OnItemClick() for clicks on ListViews which are loaded into fragmentA for each exercise of each unit.

如果我在fragmentA OncreatView内设置烤面包,它将为它们分配所有单元的所有ListViews.但是我想对每个单元的每次练习分别进行操作,即对我所有ArrayLists中的所有条目进行单独操作.

If I set toast inside fragmentA OncreatView it assign them all of the ListViews of all the units. But I want to do separately for each exercise of each unit i.e for all the entries in my all ArrayLists.

就像使用捆绑软件将数据从ArayLists传递到fragmentA一样,

As am using bundle to pass the data from ArayLists into fragmentA, like this

 @Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button1:
            bundle = new Bundle();
            bundle.putStringArrayList(LISTVIEW_DATA, unit1);
            break;
        case R.id.button2:
            bundle = new Bundle();
            bundle.putStringArrayList(LISTVIEW_DATA, unit2);
            break;
        case R.id.button3:
            bundle = new Bundle();
            bundle.putStringArrayList(LISTVIEW_DATA,  unit3);
            break;
 default:
            break;
    }

    setListFragment(bundle);
}
private void setListFragment(Bundle arguments) {
    Fragment fragment = new ExFragment();

    if (arguments != null) {
        fragment.setArguments(arguments);
    }

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.replace(R.id.container, fragment);
    ft.addToBackStack("");
    ft.commit();
}

这是我在fragmentA中的onItemClick.

This is my onItemClick in fragmentA.

@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)

我知道这是一个非常基本的问题,但是已经花了几个小时了.

I know its pretty basic question but am on it for hours.

谢谢

我有一个13个ArrayList的fragmentA,一次只显示一个,并且正在寻找一种将onitemclick分配给13个列表视图中的所有项的方法.

I have one fragmentA for 13 ArrayLists, showing only one at a time, and am looking for a way to assign onitemclick to all the items in 13 listviews.

推荐答案

对于遇到与我相同情况的任何人,我为每种情况分配了一个名为 DATA_TYPE 的标签,并制作了创建视图中的每个标签> switch case ,并解决了问题

For anyone who is going through the same situation as I was, I assigned a tag named DATA_TYPE to each case and made switch case for each tag in on create view of fragment and solved the issue

  @Override
public void onClick(View view) {
switch (view.getId()){
    case R.id.button1:
        bundle = new Bundle();
        bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE1);
        bundle.putStringArrayList(LISTVIEW_DATA, items1);
        break;
    case R.id.button2:
        bundle = new Bundle();
        bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE2);
        bundle.putStringArrayList(LISTVIEW_DATA, items2);
        break;
    case R.id.button3:
        bundle = new Bundle();
        bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE3);
        bundle.putStringArrayList(LISTVIEW_DATA, items3);
        break;
    default:
        break;
}

setListFragment(bundle);
}

MainActivityFragment 是片段类的名称.希望对别人有帮助

while MainActivityFragment is the name of fragment class. Hope it helps to someone

这篇关于如何分别为多个ListView实现onItemClick()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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