列表项单击片段内的侦听器

时间:2017-02-14 10:43:16

标签: android listview android-fragments

我正在尝试更改单击列表项的文本和复选框响应,但我无法执行此操作。因为我无法获得点击列表项。 该片段是视图寻呼机的一部分。

enter image description here

这是我的代码,

tabindex

participant_list.xml

public class ParticipantsFragment extends Fragment {

    private ParticipantAdapter mAdapter;
    SwipeRefreshLayout refreshLayout;
    private String mParticipantId, mEventId, mGender;
    ParticipantsAsyncTask task;


    public ParticipantsFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mEventId = getArguments().getString(Config.BUNDLE_KEY_EVENT_ID);
        mParticipantId = getArguments().getString(Config.BUNDLE_KEY_PARTICIPANT_ID);
        mGender = getArguments().getString(Config.BUNDLE_KEY_GENDER);

        Log.v("fragh", mEventId + " " + mParticipantId);

    }

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

        final View rootView = inflater.inflate(R.layout.participant_list, container, false);

        TextView textView = (TextView) rootView.findViewById(R.id.no_participant);

        task = new ParticipantsAsyncTask();
        task.execute();

        ListView participantListView = (ListView) rootView.findViewById(R.id.participant_list);
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout_participant_list);



        participantListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.v("Clicked Participant",mAdapter.getItem(position).getParticipantName());

                EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
                String text = notes.getText().toString();

                Log.v("Participant Text",text);

                CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
                String check;
                if(checkBox.isChecked())
                {
                    check = "1";
                }
                else
                {
                    check = "0";
                }

            }
        });

        Button submit = (Button) rootView.findViewById(R.id.submit_participant);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        mAdapter = new ParticipantAdapter(getContext(), new ArrayList<Participant>());

        participantListView.setAdapter(mAdapter);
        participantListView.setEmptyView(textView);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                task = new ParticipantsAsyncTask();
                task.execute();
                refreshLayout.setRefreshing(false);
            }
        });


        return rootView;

    }

    private class ParticipantsAsyncTask extends AsyncTask<Void, Void, List<Participant>> {

        private ProgressDialog progress;

        @Override
        protected void onPreExecute() {
            progress = new ProgressDialog(getContext());
            progress.setMessage("Gathering Data...");
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setIndeterminate(true);
            progress.setCancelable(false);
            progress.show();
        }

        @Override
        protected void onPostExecute(List<Participant> data) {

            // Clear the adapter of previous participant data
            mAdapter.clear();

            // If there is a valid list of {@link Event}s, then add them to the adapter's
            // data set. This will trigger the ListView to update.
            if (data != null && !data.isEmpty()) {
                mAdapter.addAll(data);
            }
        }

        @Override
        protected List<Participant> doInBackground(Void... params) {

            List<Participant> result;

            if (!mGender.isEmpty()) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(Config.KEY_EVENT_ID, mEventId);
                map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
                map.put(Config.KEY_GENDER, mGender);

                result = QueryUtils.extractParticipantData(map, Config.FEVER_GENDER_FILTER_PARTICIPANT_URL);

            } else {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(Config.KEY_EVENT_ID, mEventId);
                map.put(Config.KEY_PARTICIPANT_ID, mParticipantId);
                Log.v("law", mEventId + ", " + mParticipantId);
                result = QueryUtils.extractParticipantData(map, Config.FEVER_ALL_PARTICIPANT_URL);

            }

            progress.dismiss();
            return result;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        mAdapter.clear();
    }
}

participant_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/no_participant"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:gravity="center_horizontal"
    android:text="@string/no_participant_found"
    android:textColor="@color/primaryText"
    android:textSize="24sp"
    android:visibility="gone" />

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout_participant_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

        <ListView
            android:id="@+id/participant_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@color/divider"
            android:dividerHeight="1dp"
            android:descendantFocusability="beforeDescendants"
            android:drawSelectorOnTop="true"
            android:headerDividersEnabled="true" />

</android.support.v4.widget.SwipeRefreshLayout>

<Button
    android:id="@+id/submit_participant"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:textSize="20sp"
    android:background="@color/colorAccent"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我的猜测是AdapterView.OnItemClickListener您使用rootView来访问单元格属性。像:

EditText notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
...
CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);

您应该使用View view中的onItemClick参数:

EditText notes = (EditText) view.findViewById(R.id.participant_list_item_notes);
...
CheckBox checkBox = (CheckBox) view.findViewById(R.id.participant_list_item_checkbox);

如果它能解决您的问题,请告诉我。

编辑:

如果问题是点击了该项目,请检查https://stackoverflow.com/a/20755698/2174489

相关问题