如何以编程方式选择ListView项目

时间:2016-02-01 21:01:57

标签: android listview android-fragments

问题:如何以编程方式突出显示ListView中的选项?

背景:我已经看过很多关于这类问题的线索,但似乎都没有。我在一个片段中有一个列表视图,该片段由使用自定义适配器从数据库创建的列表填充。当片段打开时,我希望默认选择其中一个项目(取决于编辑模式是否为1 /启用),我已准备好此项目的位置。我目前使用的是clientList.setChoiceMode(ListView.CHOICE_MODE_SINGLE),并尝试使用setItemChecked突出显示该选项,但它不起作用。

Fragment OnCreate

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    FragmentActivity faActivity = (FragmentActivity) super.getActivity();
    final LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.fragment__client, container, false);

    addJob = (AddJob)getActivity();

    clientList = (ListView) linearLayout.findViewById(R.id.listView);

    clientList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    dbHandler = new DraycottDataHandler(super.getActivity().getApplicationContext());

    if (dbHandler.getClientCount() != 0)
        Clients.addAll(dbHandler.getAllClients()); //Loads clients into Client list

    populateList(); //Sets adapter for the listview.


    if(addJob.getEdit() == 1) { //Get edit mode from activity
        NewJobClass editJob = addJob.getEditJob();


        for (int i = 0; i < Clients.size(); i++) {
            ClientClass client = Clients.get(i);

            if (client.getID() == editJob.getClientID()) {
                clientList.setItemChecked(i, true); //Highlight item (doesnt do anything).
            }
        }
    }


    clientList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedClient = Clients.get(position);
            listener.getClientData(selectedClient);

        }
    });


    return linearLayout;

}

ListView XML

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.simple.jack.application.Fragment_Client">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:choiceMode="singleChoice"
        android:listSelector="#D3D3D3"
        android:clickable="true"/>

</LinearLayout>

如果需要更多代码,我很乐意提供它。

Similar Question and Answer

这个问题与我的相似,但答案给了我一个InflateException。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

此时视图尚未就绪,请尝试发布:

linearLayout.post(new Runnable(){
@override
public void run(){
//your code that selects the item
}});

或在onViewCreated上调用它

答案 1 :(得分:0)

您确定正在调用clientList.setItemChecked(i, true);吗? 您可以尝试添加代码以在onViewCreated方法中进行选择。

相关问题