2012-08-23 69 views
4

根據我正在處理的應用程序,我需要創建一個列表視圖,然後在用戶單擊每個項目時處理這些事件。一切工作正常,除了列表視圖中的第一項。這是不可點擊的第一次列表視圖加載後,我向上或向下滾動列表視圖它完美的作品。還有一件事,第一次列表視圖被加載如果我點擊第一個項目然後第二個項目,一個Toast消息首先顯示第二個項目的名稱,然後顯示第一個項目的名稱。因此,直到列表視圖滾動或點擊之前,列表視圖中的第一個項目纔會處於活動狀態。這是我的代碼片段。任何想法如何處理它?ListView中的第一項在Android中無法正常工作

public EyeDiseasesFragment(File diseasesJsonFile) { 
    this.diseasesJsonFile = diseasesJsonFile; 
    mainUrl = Config.mainUrl; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 

    diseasesList = new ArrayList<Diseases>(); 

    new LoadContent().execute(); 

} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try { 
     listener = (MainFragmentController) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(activity.toString() 
       + " must implement OnArticleSelectedListener"); 
    } 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
} 

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

    View view = inflater.inflate(R.layout.diseases, container, false); 

    mList = (ListView) view.findViewById(R.id.diseases); 

    return view; 

} 

class DiseasesAdapter extends BaseAdapter { 

    LayoutInflater inflater; 

    public DiseasesAdapter() { 
     inflater = (LayoutInflater) getActivity().getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

    @Override 
    public Diseases getItem(int arg0) { 
     return diseasesList.get(arg0); 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return arg0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup arg2) { 

     final Diseases item; 

     ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.diseases_item, null); 

      holder.title = (TextView) convertView.findViewById(R.id.disease_item_header); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     item = getItem(position); 
     holder.title.setText(item.diseaseName); 

     convertView.setLayoutParams(new ListView.LayoutParams(
       LayoutParams.MATCH_PARENT, HelperStaticMethods.getDIP(
         getActivity(), 50))); 

     convertView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       listener.onDiseasItemClick(item); 

      } 
     }); 

     return convertView; 
    } 
} 

class ViewHolder { 
    TextView title; 
} 

class LoadContent extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 

     setDiseasesInformation(getActivity()); 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 

     mList.setCacheColorHint(Color.TRANSPARENT); 
     mList.setDivider(null); 
     mList.setVerticalFadingEdgeEnabled(false); 
     DiseasesAdapter adapter = new DiseasesAdapter(); 
     mList.setAdapter(adapter); 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
    } 

} 

編輯

移動下面的代碼爲statement這裏convertView爲Null解決了這個問題。

if (convertView == null) { 

    // ... after inflating the view 
    convertView.setLayoutParams(new ListView.LayoutParams(
       LayoutParams.MATCH_PARENT, HelperStaticMethods.getDIP(getActivity(), 50))); 
} 
+0

嘗試從適配器中移出setOnClickListener。在onCreate()部分實現它:'listview.setOnItemClickListener()' – nistv4n

+0

我試過在onActivityCreated中做(因爲在片段的生命週期中它出現在定義了列表視圖的onCreateView之後)並且它不起作用。任何其他想法? – osayilgan

+0

發現此問題的任何解決方案? – superuserdo

回答

2

能不能請你移動代碼

convertView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      listener.onDiseasItemClick(item); 

     } 
    }); 

if (convertView == null){ 
..... 
} 

,以確保監聽器設置一次。

+0

它一樣,它沒有工作。 – osayilgan

相關問題