我的代码如下。问题是我检查了我的日志后没有调用getView()
方法
我的片段类是
public class AlertListFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public ArrayList<AlertModel> mModelLIst=new ArrayList<AlertModel>();
public AlertAdapter adapter;
ListView mAlertList;
CommentsDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_alert_list, container, false);
mAlertList=(ListView)view.findViewById(R.id.alertlist);
System.out.println("details in fragment");
datasource = new CommentsDataSource(getActivity());
datasource.open();
ArrayList<AlertModel> source = datasource
.getAllContacts();
if (source.size() > 0) {
System.out.println("response number of items"
+ source.size());
adapter = new AlertAdapter(getActivity(), source,getActivity().getResources());
mAlertList.setAdapter(adapter);
}
return inflater.inflate(R.layout.fragment_alert_list, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
我的适配器类是
public class AlertAdapter extends BaseAdapter implements View.OnClickListener {
private Activity activity;
private ArrayList data;
ArrayList<String> btnpositions = new ArrayList<String>();
public Resources res;
private static LayoutInflater inflater = null;
ViewHolder holder;
AlertModel tempValue = null;
public AlertAdapter(Activity activity, ArrayList data, Resources res) {
System.out.println("response number of items inside adapter view: ");
this.activity = activity;
this.data = data;
this.res = res;
}
public static class ViewHolder {
public TextView msubject, mbody;
public ImageView mTYpe;
public LinearLayout mTYpeLay;
}
@Override
public int getCount() {
System.out.println("response number of items inside adapter count: "+data.size());
if (data.size() <= 0)
return 1;
System.out.println("response number of items inside adapter count: "+data.size());
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int arg0, View vis, ViewGroup arg2) {
System.out.println("response number of items inside adapter view: "+arg0);
View vi = vis;
int crntposition = arg0;
Context context;
if (vis == null) {
inflater = (LayoutInflater) activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.alert_list_item, null);
} else
holder = (ViewHolder) vi.getTag();
holder = new ViewHolder();
holder.msubject = (TextView) vi.findViewById(R.id.txtsubject);
holder.mbody = (TextView) vi.findViewById(R.id.txtbody);
holder.mTYpe = (ImageView) vi.findViewById(R.id.imgtype);
holder.mTYpeLay=(LinearLayout)vi.findViewById(R.id.typelay);
if (data.size() <= 0) {
holder.msubject.setText("No Data");
} else {
vi.setTag(holder);
vi.setTag(R.id.txtsubject, holder.msubject);
vi.setTag(R.id.txtbody, holder.mbody);
vi.setTag(R.id.imgtype, holder.mTYpe);
holder.msubject.setTag(crntposition);
holder.mbody.setTag(crntposition);
holder.mTYpe.setTag(crntposition);
tempValue = null;
tempValue = (AlertModel) data.get(crntposition);
/************ Set Model values in Holder elements ***********/
context = arg2.getContext();
holder.msubject.setText(tempValue.getmMessage());
if(tempValue.getmMessage().equalsIgnoreCase("1"))
{
holder.mTYpeLay.setBackgroundResource(R.color.quote_blue);
holder.mTYpe.setBackgroundResource(R.drawable.q);
}
else if(tempValue.getmMessage().equalsIgnoreCase("2"))
{
holder.mTYpeLay.setBackgroundResource(R.color.event_yellow);
holder.mTYpe.setBackgroundResource(R.drawable.e);
}
else
{
holder.mTYpeLay.setBackgroundResource(R.color.wishes_red);
holder.mTYpe.setBackgroundResource(R.drawable.bday);
}
}
vi.setOnClickListener(new OnItemClickListener(crntposition));
return vi;
}
@Override
public void onClick(View v) {
Log.e("click","yess");
}
private class OnItemClickListener implements View.OnClickListener {
private int mPosition;
OnItemClickListener(int position) {
mPosition = position;
System.out.println("pos"+mPosition);
}
@Override
public void onClick(View arg0) {
}
}
}
片段的xml文件是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fencer.geo.ti.geofencer.fragments.AlertListFragment">
<TextView
android:layout_width="wrap_content"
android:text="fragment"
android:id="@+id/txtt"
android:layout_height="wrap_content" />
<!-- TODO: Update blank fragment layout -->
<ListView android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_below="@+id/txtt"
android:background="@color/second_grey"
android:id="@+id/alertlist"
/>
</RelativeLayout>
这是我的日志
05-14 15:10:04.362 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items13
05-14 15:10:04.363 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items inside adapter view:
05-14 15:10:04.363 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items inside adapter count: 13
05-14 15:10:04.363 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items inside adapter count: 13
05-14 15:10:04.363 13684-13684/fencer.geo.ti.geofencer I/System.out﹕ response number of items inside adapter count: 13
我的输出是
答案 0 :(得分:0)
你必须错误地返回onCreateView
,
return inflater.inflate(R.layout.fragment_alert_list, container, false);
您将返回一个全新的膨胀视图,ListView完全不了解您的适配器。将其更改为
return view;