【问题标题】:2 ListView in a ListFragment only one onListItemClick2 ListView中的一个ListFragment只有一个onListItemClick
【发布时间】:2013-05-11 15:43:00
【问题描述】:

正如我在标题中提到的,我在 ListFragment 中有 2 个 ListView(我们称它们为 A 和 B),并且只有一个 onListItemClick。

当我单击列表 A 的一行时,我的 onListItemClick 调用正常。但是当我点击列表 B 的一行时,什么也没有发生。

这就是我所拥有的:

<!-- LIST A -->
<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

<!-- Separator -->
<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

<!-- LIST B -->
<ListView
    android:id="@+id/listCompany"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

这是我的 ListFragment:

public class ClientsActivity extends ListFragment {
    OnClientSelectedListener mClientListener;


public interface OnClientSelectedListener {
        public void onEmployeeSelected(int id);
        public void onCompanySelected(int id);
    }

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        mClientListener = (OnClientSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " you must implement OnClientSelectedListener ");
    }
}

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Log.e("onListItemClick","called with " + position + " : "+l.getId() + " and " + android.R.id.list);
        int id = position; 
    if (l.getId() == android.R.id.list) {
           mClienteListener.onEmployeeSelected(id);
       } else if (l.getId() == R.id.listCompany) {
           mClienteListener.onCompanySelected(id);
       }
}

然后在我的主 FragmentActivity 中实现接口 OnClientSelectedListener。我认为这必须与我的 ListViews 中的 android:id 一起使用。当单击列表 B 的一行时,我不知道如何调用 onListItemClick。任何帮助将不胜感激。

解决方案:

在我的 ListFragment 的 onCreateView 中,我有:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_clientes, container, false);

            //LIST A
    listEmploees(view);

            //LIST B
    listCompanies(view);
...
}

private void listCompanies(View view){
    CompanyqliteDao companyDao = new CompanyqliteDao ();
            //I get all the companies from DB
    mCursorCompany = companyDao.listCompany(getActivity());

    if(mCursorCompany.getCount()>0){
        String[] from = new String[] { "name", "phone"};
        int[] to = new int[] { R.id.textViewNameIzq,  R.id.textViewNameCent };
        ListView lvCompanies = (ListView) view.findViewById (R.id.listCompany);

                    //I have a custom adapter
        ListClientCursorAdapter notes = new ListClientsCursorAdapter(contexto, R.layout.activity_fila_cliente, mCursorCompany, from, to, 0);
        lvCompanies .setAdapter(notes);


        lvCompanies.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)   {
                onListItemClick(lvCompanies,view,position,arg);
            }
        });

    }
}


private void listEmploees(View view){
    EmployeeSqliteDao employeeDao = new EmployeeSqliteDao();
            //Get the list from DB
    mCursorEmployee = employeeDao.listEmployees(getActivity());

    if(mCursorEmployee.getCount()>0){
        String[] from = new String[] { "name", "phone"};
        int[] to = new int[] { R.id.textViewNameIzq,  R.id.textViewNameCent};
        ListView lvEmployees = (ListView) view.findViewById (android.R.id.list);

        ListClientsCursorAdapter notes = new ListClientesCursorAdapter(context, R.layout.activity_fila_cliente, mCursorEmployee, from, to, 0);
        lvEmployees.setAdapter(notes);
                    //NOTE THAT I DONT HAVE TO SET A LISTENER FOR THIS ONE, AUTOMATICALLY
                    // THE onListItemClick IS CALLED

    }

【问题讨论】:

    标签: android android-listview android-listfragment


    【解决方案1】:

    您需要获取第二个 ListView 的引用,并且需要将 onItemClickListener 附加到它。

    因为 ListFragment 将为 ID 为 @id/android:list 的 listView 附加 onItemClckListener

    尝试在onCreateView获取参考

    编辑:

      ListView list2=view.findViewById(R.id.listCompany);
       list2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)   {
                onListItemClick(adapter,view,position,arg);
        }
    });
    

    【讨论】:

    • 我照你说的做了,但 eclipse 抱怨:“方法 setOnItemClickListener (AdapterView.OnItemClickListener 类型的 AdapterView 不适用于参数 (ClientsActivity)。我将在其中添加更多代码我的问题请看。
    • @kiduxa 请尝试我的编辑。我没试过,希望能成功
    • 仍然 eclipse 抱怨。 setOnItemClickListener 是否有任何方式调用我已经覆盖的 onListItemClick?
    • @kiduxa 您可能正在导入View 类型的onItemClickListener,请将其更改为新的AdapterView.onItemClickListener
    • @kiduxa 确保您使用的是 AdapterView 类型的 onItemClickListener 检查导入 import android.widget.AdapterView.OnItemClickListener;
    【解决方案2】:

    您需要将您的ListFragment 更改为Fragment,然后分别引用两个ListViews 并分配它们的侦听器:

    ListView list = (ListView)findViewById(R.id.list);
    ListView listCompany = (ListView)findViewById(R.id.listCompany);
    
    list.setAdapter(...)
    list2.listCompany(...)
    

    每个ListFragment 一个ListView,或一个Fragment 中的多个ListViews,但一个ListFragment 中没有两个ListViews

    【讨论】:

    • 哇这个变化真的让我改变了很多东西!你确定吗? : "每个 ListFragment 一个 ListView,或者一个 Fragment 中有多个 ListView,但一个 ListFragment 中没有两个 ListView"
    • 不只是我的意见...另一个回复:stackoverflow.com/a/13959135/793607 还有文档说:ListFragment 托管一个 ListView 对象,该对象可以绑定到不同的数据源,通常是一个数组或一个保存查询结果的游标。以下部分将讨论绑定、屏幕布局和行布局。 (注意,它说的是“ListView 对象”而不是“ListView 对象”
    • 哇,我明白了.. 但是评论的其他人帮助了我。请看一下。我应该这样保留还是改成你说的那样?不知道以后会不会有问题。
    • 如果它适合你,那就用它来运行吧!如果您所拥有的有效,则无需担心理论。
    • 非常感谢您的关注,我非常感谢。你以前和现在都帮助过我。所以谢谢!!
    猜你喜欢
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多