我正在实现一个实现ActionBar.TabListener的Fragment活动,因此我有3个选项卡,每个选项卡都有ListFragments,其中有一个列表,其中填充了通过服务器获取的数据。我一直面对这个问题已经有一段时间了,无论我对这个特定问题有多少问题,或者我看了多少关于用自定义数组适配器实现ListFragments的教程,我找不到答案。
我遇到的问题是我无法将Listview上的数据显示在应用上。我已经设法通过服务器获取数据,我需要在将适配器设置为ListView之前填充自定义阵列适配器。我甚至调试了代码,它表示在调用将数组适配器设置为Listview之后,数据将填充在Adapter和Listview中。然而,我无法将Listview中的数据显示在应用程序中。我一直在研究这个问题一段时间,我通过论坛中发布的教程和问题来研究这个问题是否有任何建议(即使是one)我还没有找到任何帮助我解决问题的方法。如果有人能够指出我做错了什么并提出解决这个问题的建议,我很乐意感谢任何意见。
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;
}
}
ListFragment Activity的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: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>
自定义数组适配器的代码
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;
}
}
ListView中每行的代码
<?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>
答案 0 :(得分: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成员肯定会在片段被销毁和重新创建时(在方向更改时,在从“最近”列表中恢复应用程序时)让您感到悲伤过程在过渡期间被拆除等)。