2017-02-15 91 views
1

我想改變單擊列表項的文本和複選框響應,但我不能這樣做。由於我無法獲取點擊列表項。 該片段是查看尋呼機的一部分。setOnItemClickListener不工作在片段

enter image description here

這裏是我的代碼,

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

找到答案atlast。

請勿使用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" 

刪除此感謝大家的幫助,每一個

0
<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" /> 

嘗試更換您的複選框,並添加這兩個線路

android:focusable="false" 
android:clickable="false" 
0

試試這個應該participant_list_item.xml工作,並從列表視圖中刪除

android:descendantFocusability="blocksDescendants" 
+0

謝謝先生。它的工作,但現在我無法改變複選框的響應和編輯文本。 –

+0

嘗試爲edittext添加TextWatcher,併爲您的pozo中的複選框設置一個布爾變量,這可能會幫助您更改值 –

+0

我正在讀取日誌。在那裏,我看到,當我點擊TextView時,Touch up dispatch的日誌是針對ListView的,但是當我點擊edittext或複選框時,Touch up dispatch是針對edittext或textview的。任何想法!! –

0

你必須做這樣的

private EditText notes; 
private CheckBox checkBox; 

並把它們放在上面onclick

 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 希望認爲這將有助於你

+0

之前,我不能夠得到點擊列表項。 –

0

機器人:可點擊=真 確保您已經添加點擊屬性爲true。 在participantslist.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/> 

讓我知道,如果這個建議是錯誤的?

+0

這件事已解決,但現在我無法編輯edittext和更新複選框。 –

+0

你的意思是說,點擊複選框的行爲是不正確的? – sivaram