2014-06-13 22 views
1

我implmenting實現ActionBar.TabListener片段的活性,所以我有3個選項卡,擁有各自具有填充通過服務器獲取數據的列表ListFragments。我一直在面對這個問題一段時間,無論我研究了多少個關於這個特定問題的問題或者我在關於使用自定義陣列適配器實現ListFragments時看過多少個教程,我都找不到答案。項目在ListFragment不會在列表視圖顯示了使用自定義數組適配器

我遇到的問題是,我不能讓在列表視圖中的數據顯示上的應用程序。我已經設法通過服務器獲取數據,我需要在將適配器設置爲ListView之前填充自定義數組適配器。我甚至調試過代碼,它說在調用設置數組適配器到Listview後,數據填充到適配器和Listview中。但是,我無法將Listview中的數據顯示在應用程序中。我一直在研究這個問題一段時間,我通過教程和論壇上發佈的任何建議(即使這one)的問題來研究這個問題,我還沒有找到任何有助於我解決我的問題。如果有人能指出我做錯了什麼,並提出解決這個問題的建議,我很樂意聽取任何意見。

代碼用於ListFragment活動

<?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:gravity="center_horizontal"  
android:orientation="vertical" 
android:background="#FFFFFF"> 

<ListView 
    android:id="@android:id/list" 
    android:drawSelectorOnTop="false" 
    android:tag="my_jobs" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:layout_width="match_parent" /> 

</LinearLayout> 

代碼在ListView的ListFragment活動

public class MyJobsActivity extends ListFragment{ 

private ArrayList<Job> myJobs; 

private static ListView listView; 

private static ArrayList<Job> jobList;  

ActionBar titleBar; 

MyJobsActivity disAllList; 


@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    myJobs = (ArrayList<Job>) getArguments().getSerializable(Constants.MYJOBS); 
    jobList = new ArrayList<Job>();  

    Job datJob; 
    for(int i = 0; i < myJobs.size(); i++){ 
     datJob = new Job(); 
     datJob.setJobId(myJobs.get(i).getJobId()); 
     datJob.setJobTitle(myJobs.get(i).getJobTitle()); 
     datJob.setCompany(myJobs.get(i).getCompany()); 
     datJob.setLocation(myJobs.get(i).getLocation()); 
     jobList.add(datJob); 
    } 
    MyJobsAdapter datAdapter = new MyJobsAdapter(getActivity(), R.layout.job_row, jobList); 
    listView.setAdapter(datAdapter); 

} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    try{ 
     Intent datIntent = new Intent(getActivity(),JobActivity.class); 
     Job job = jobList.get(position); 
     datIntent.putExtra(Constants.JOBID, job.getJobId()); 
     datIntent.putExtra(Constants.JOBTITLE, job.getJobTitle()); 
     startActivity(datIntent); 
    } 
    catch(RuntimeException e){ 
     e.printStackTrace(); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){   
    View rootView = inflater.inflate(R.layout.activity_my_jobs, container, false); 
    listView = (ListView) rootView.findViewById(android.R.id.list); 

    return rootView; 
} 


} 

XML文件爲定製陣列適配器

public class MyJobsAdapter extends ArrayAdapter<Job> { 

private Activity activity; 
private ArrayList<Job> data; 
private HashMap<Integer, Boolean> selection;   
private static LayoutInflater inflater=null; 
private TextView jobPosition, company, location; 
private CheckBox jobChecked; 
private View actionView; 
private int height, prevSize; 
private ActionMode datMode; 

public MyJobsAdapter(Activity a, int layoutResourceId, ArrayList<Job> jobs) { 
    super (a, layoutResourceId, jobs); 
    this.selection = new HashMap<Integer, Boolean>(); 
    this.activity = a; 
    this.data = jobs; 
} 

private class ViewHolder { 
    TextView jobTitle; 
    TextView companyName; 
    TextView location; 
} 

/*public void setData(ArrayList<Job> d){ 
    data = d; 
    if(data != null){ 
     for (Job job : d){ 
      add(job); 
     } 
    } 
    this.notifyDataSetChanged(); 
} */ 

public void setNewSelection(int position, boolean value){ 
    prevSize = selection.size(); 
    selection.put(position, value); 
    this.notifyDataSetChanged(); 
} 

public boolean isPositionChecked(int position, boolean value){ 
    boolean result = selection.get(position); 
    return result == true ? result : false; 
} 

public void removedSelection(int position){ 
    prevSize = selection.size(); 
    selection.remove(position); 
    this.notifyDataSetChanged(); 
} 

public void clearSelection(){ 
    prevSize = 0; 
    selection = new HashMap<Integer, Boolean>(); 
    this.notifyDataSetChanged(); 
}  


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


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

public void setMode(ActionMode mode){ 
    datMode = mode; 
} 

public int getSelectedNumberOfItems(){ 
    return selection.size(); 
} 

public HashMap<Integer, Boolean> getSelectedList(){ 
    return selection; 
} 

public ArrayList<Job> getData(){ 
    return data; 
} 

public int getHeight(){ 
    return height; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder jobHolder = null; 
    Job rowItem = getItem(position); 
    if(convertView==null){ 
     convertView = inflater.inflate(R.layout.job_row, parent, false); 
     convertView.setBackgroundResource(R.drawable.list_selector); 
     jobHolder = new ViewHolder(); 
     jobHolder.jobTitle = (TextView) convertView.findViewById(R.id.Position); 
     jobHolder.companyName = (TextView) convertView.findViewById(R.id.Company); 
     jobHolder.location = (TextView) convertView.findViewById(R.id.Location); 
     convertView.setTag(jobHolder); 
    } else{ 
     jobHolder = (ViewHolder) convertView.getTag(); 
    } 
    /* jobPosition = (TextView)vi.findViewById(R.id.Position); 
    company = (TextView)vi.findViewById(R.id.Company); 
    location = (TextView)vi.findViewById(R.id.Location); 
    jobChecked = (CheckBox)vi.findViewById(R.id.JobSelected); 

    jobChecked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 
      StringBuilder lSelectedString = new StringBuilder(selection.size()).append(Constants.SELECTED); 
      if(isChecked){ 
       setNewSelection(position,true); 
       datMode.setTitle(lSelectedString.toString()); 
      } 
      else{ 
       removedSelection(position); 
       datMode.setTitle(lSelectedString.toString()); 
       if(selection.size() < 1) 
        datMode.finish(); 
      } 

     } 
    }); 

    if(selection.size() == 0){ 
     jobChecked.setVisibility(View.GONE); 
    } 
    else{ 
     jobChecked.setVisibility(View.VISIBLE); 
     jobChecked.setChecked(selection.get(position) == null ? false : true); 
    } 

    vi.setBackgroundResource(selection.get(position) == null ? color.white_color : color.holo_blue_bright); 

    */ 

    jobHolder.jobTitle.setText(rowItem.getJobTitle()); 
    jobHolder.companyName.setText(rowItem.getCompany()); 
    jobHolder.location.setText(rowItem.getLocation()); 

    return convertView; 

} 

}  

代碼的每一行

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/light_gray" 
    android:paddingTop="8dip" 
    android:paddingBottom="8dip" 
    android:descendantFocusability="blocksDescendants"> 

<RelativeLayout 
    android:id="@+id/JobRow" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<CheckBox 
    android:id="@+id/JobSelected" 
    android:layout_centerVertical ="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/Position" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/dark_gray_position" 
     android:layout_toRightOf="@+id/JobSelected" 
     android:typeface="sans" 
     android:fontFamily="Roboto Regular" 
     android:textSize="22sp" 
     android:paddingLeft="4dip"/> 

    <TextView 
     android:id="@+id/Company" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/blue_company" 
     android:textSize="18sp" 
     android:fontFamily="Roboto Regular"   
     android:paddingLeft="4dip" 
     android:layout_below="@+id/Position" 
     android:layout_toRightOf="@+id/JobSelected"/> 

    <TextView 
     android:id="@+id/Location" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:layout_below="@+id/Company" 
     android:layout_toRightOf="@+id/JobSelected" 
     android:paddingBottom="8dip" 
     android:paddingLeft="4dip"     
      android:textColor="@color/steel_gray_location" 
     android:fontFamily="Roboto Regular" 
     android:textSize="14sp"/> 

    </RelativeLayout> 

</RelativeLayout> 

回答

3

那麼有可能必須與該片的生命週期因您的MyJobsActivity靜力學的幾個問題,但是對於顯示數據本身:

在你MyJobsActivity.onActivityCreated(或任何你結束你的關聯與基礎ListFragment自定義適配器),你需要使用setListAdapter而不是setAdapter

ListFragment文檔(見綁定到數據):

必須使用ListFragment.setListAdapter()到列表中與適配器關聯。不要直接調用ListView.setAdapter(),否則重要的初始化將被忽略。

是有點更清晰,使用的衆多優勢之一/延伸的ListFragment是,它會在內部處理您的自定義接口和底層的ListView之間的綁定,但必須將適配器添加到ListFragment本身(即setListAdapter),不在ListView上。

其他一些意見:

事實上,除非你需要一個自定義佈局,我建議完全刪除您onCreateView的實現。這將允許ListFragment爲其內部ListView使用其默認佈局,初始化和處理,並且您將獲得(默認)處理空列表,加載進度條等。

鑑於您發佈的代碼,我建議您不要在您的MyJobsActivity中使用私有ListView成員 - 它的父級ListFragment已經爲您保留了基礎ListView的副本,並且如果您通過setListAdapter正確設置適配器,你不需要顯式對象(如果你這樣做,使用ListFragment的getListView())。無論如何,如果我自己的試驗和挑戰有任何跡象,那麼靜態的ListView和ArrayList成員肯定會導致你在片段被銷燬並重新創建時產生悲傷(如果方向發生變化,應用程序從「最近」列表中恢復,如果if這個過程在過渡期間已經被拆除,等等)。

+0

嗨,Brian!對不起,爲時已晚!直到現在,我決定把這個功能放在後面。 我參考了你列出的所有建議。我認爲灰箱中的那個是問題,你的建議有幫助。你肯定給了其餘的很多提示。感謝您的幫助! –

相關問題