2016-08-05 36 views
-1

即時通訊使用片段,我已經使用了一個對話框和填充自定義列表視圖現在我想要在列表視圖中獲得項目點擊。當我下面這樣做時,它沒有給出任何迴應。 請檢查某人。listview onitemclick不工作在Android片段對話框

 iv_fav_list.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        dialog = new Dialog(getActivity()); 
        dialog.setContentView(R.layout.dialog_fav_country_list); 

        lv_custom_list_fav_con = (ListView) dialog.findViewById(R.id.list_fav_country); 
        CFLA = new CustomFavCountryListAdapter(countryList, CountryActivityFragment.this.getActivity()); 
        lv_custom_list_fav_con.setAdapter(CFLA); 
        dialog.show(); 
        lv_custom_list_fav_con.setOnItemClickListener(new OnItemClickListener() { 
         @Override 
         public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

          Toast.makeText(CountryActivityFragment.this.getActivity(), "test", Toast.LENGTH_LONG).show(); 

         } 
        }); 

       } 
      }); 

適配器類是

ArrayList<Country> mCountryList; 
    Context mContext; 
    int position; 
    Country cnt; 
    OnDialogListClickListener mlistener; 

    public interface OnDialogListClickListener { 
     void onItemClick(int position); 
    } 

    public CustomFavCountryListAdapter(ArrayList<Country> countryList, Context context, OnDialogListClickListener listener) { 

     this.mCountryList = countryList; 
     this.mContext = context; 
     this.mlistener = listener; 
    } 

    @Override 
    public int getCount() { 
     return mCountryList.size(); 
    } 

    @Override 
    public Object getItem(int i) { 

     return mCountryList.get(i); 

    } 

    @Override 
    public long getItemId(int i) { 
     return mCountryList.get(i).getCountryID(); 
    } 

    @Override 
    public View getView(int i, View view, final ViewGroup viewGroup) { 


     position = i; 
     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View convertView = inflater.inflate(R.layout.custom_fav_country_list, viewGroup, 
       false); 
     TextView tv_fav_con_name = (TextView) convertView.findViewById(R.id.tv_custom_fav_country_name); 
     tv_fav_con_name.setText(mCountryList.get(i).getCountryName()); 

     convertView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mlistener.onItemClick(position); 
      } 
     }); 

     convertView.setTag(mCountryList.get(i).getCountryName()); 
     return convertView; 
    } 


} 
+0

是否在點擊監聽器上調試了您的代碼? –

+0

nope,這甚至沒有迴應。 – jacks

回答

0

在適配器

public interface OnDialogListClickListener { 
    void onItemClick(Country item); 
} 

創建接口傳遞該接口通過適配器的構造參數,並設定爲視setOnClickListener,在getView方法膨脹在適配器。

@Override 
public View getView(int i, View view, final ViewGroup viewGroup) { 


    LayoutInflater inflater = (LayoutInflater) mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View convertView = inflater.inflate(R.layout.custom_fav_country_list, viewGroup, 
      false); 

    Country country = mCountryList.get(i); 
    TextView tv_fav_con_name = (TextView) convertView.findViewById(R.id.tv_custom_fav_country_name); 
    tv_fav_con_name.setText(country.getCountryName()); 

    convertView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mlistener.onItemClick(country); 
     } 
    }); 

    convertView.setTag(country.getCountryName()); 
    return convertView; 
} 

在片段中實現此接口。

private CustomFavCountryListAdapter.OnDialogListClickListener onDialogListClickListener = new CustomFavCountryListAdapter.OnDialogListClickListener() { 
    @Override 
    public void onItemClick(Country item) { 
     Toast.makeText(getActivity(), country.getCountryName(), Toast.LENGTH_SHORT).show(); 
    } 
}; 

創建這樣的適配器。

CFLA = new CustomFavCountryListAdapter(countryList, CountryActivityFragment.this.getActivity(), onDialogListClickListener); 
+0

但這總是給出相同的列表項,我需要它是動態的 – jacks

+0

你可以發佈適配器類嗎? – faranjit

+0

但這總是給出相同的項目,我需要從列表中獲得一個動態項目 – jacks