我正在尝试更改单击列表项的文本和复选框响应,但我无法执行此操作。因为我无法获得点击列表项。 该片段是视图寻呼机的一部分。
这是我的代码,
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.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>
participant_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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="80dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="@+id/participant_list_item_id"
android:layout_width="0dp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="2"
android:textSize="20sp"
tools:text="M78" />
<TextView
android:id="@+id/participant_list_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="5"
android:textSize="20sp"
android:fontFamily="sans-serif"
tools:text="Billu Barber" />
<EditText
android:id="@+id/participant_list_item_notes"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_weight="6"
android:textSize="12sp"
android:background="@drawable/participant_list_notes"
android:hint="@string/participant_notes"
android:inputType="textMultiLine"
android:scrollbars="none"
android:imeOptions="actionDone"
android:maxLength="50"
android:padding="4dp" />
<CheckBox
android:id="@+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
</LinearLayout>
答案 0 :(得分:0)
------------------------------------
| 5 | | | | | | 34 |
------------------------------------
-1 0 1 2 3 4 5 6 7
head1 head2
尝试更换CheckBox并添加这两行
<CheckBox
android:id="@+id/participant_list_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:clickable="false"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
答案 1 :(得分:0)
尝试这应该在participant_list_item.xml中工作并从listview中删除
android:descendantFocusability="blocksDescendants"
答案 2 :(得分:0)
你必须这样做
private EditText notes;
private CheckBox checkBox;
并将上述内容点击
notes = (EditText) rootView.findViewById(R.id.participant_list_item_notes);
String text = notes.getText().toString();
Log.v("Participant Text",text);
checkBox = (CheckBox) rootView.findViewById(R.id.participant_list_item_checkbox);
edittext和复选框是listview中的视图 希望这会对你有所帮助
答案 3 :(得分:0)
<强>机器人:可点击=真强> 确保已将可点击属性添加到true。 在participantlist.xml文件中:
<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:clickable=true/>
请告诉我这个建议是否错误?
答案 4 :(得分:0)
找到答案。
不要使用setOnItemClickListener进行项目点击。使用项目视图使用setOnClickListener()在适配器方法内单击。在onClick()
中做一些事情@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View listItemView = convertView;
if (listItemView == null)
{
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.participant_list_item, parent, false);
}
listItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(mContext, "click item",Toast.LENGTH_LONG).show();
}
});
Participant currentParticipant = getItem(position);
if(currentParticipant.getParticipantGender().equals("F"))
{
TextView idView = (TextView) listItemView.findViewById(R.id.participant_list_item_id);
idView.setTextColor(getColor(mContext, R.color.colorPrimary));
idView.setText(currentParticipant.getParticipantId());
}
else if(currentParticipant.getParticipantGender().equals("M"))
{
TextView idView = (TextView) listItemView.findViewById(R.id.participant_list_item_id);
idView.setTextColor(getColor(mContext, R.color.colorAccent));
idView.setText(currentParticipant.getParticipantId());
}
TextView nameView = (TextView) listItemView.findViewById(R.id.participant_list_item_name);
nameView.setText(currentParticipant.getParticipantName());
final EditText notesView = (EditText) listItemView.findViewById(R.id.participant_list_item_notes);
notesView.setText(currentParticipant.getParticipantNotes());
CheckBox checkedView = (CheckBox) listItemView.findViewById(R.id.participant_list_item_checkbox);
if (currentParticipant.getParticipantCheck().equals("1"))
{
checkedView.setChecked(true);
}
else
{
checkedView.setChecked(false);
}
return listItemView;
}
从participant_list_item.xml
中删除android:descendantFocusability="blocksDescendants"
感谢每一位的帮助。