我正在尝试对android ListView
进行一些练习。代号如下:
listfragment
:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view= inflater.inflate(R.layout.frag_getfriends,container,false);
friendsListView=(ListView)view.findViewById(android.R.id.list);
infoTextView=(TextView)view.findViewById(android.R.id.empty);
swipe=(SwipeRefreshLayout)view.findViewById(R.id.swipe_refresh);
swipe.setOnRefreshListener(this);
arrayAdapter=new DataListAdapter(getActivity(),R.layout.userinfo_list_config,null);
friendsListView.setAdapter(arrayAdapter);
friendsListView.setOnItemClickListener((parent,itemView,position,id)->{
if(friends!=null){
User friend=friends.get(position);
Toast.makeText(getActivity(),friend.getName(),Toast.LENGTH_SHORT).show();
}
});
presenter.startQuery(true);
return view;
}
Adapter
构造函数是:
public DataListAdapter(Context context, int itemLayoutResoureId, ArrayList<User>friends){
super(context,itemLayoutResoureId,friends);
this.context=context;
this.users=new ArrayList<>();
users.addAll(friends);
}
一开始,listview
在此应用在线提取某些数据之前无法显示数据:
@Override
public void setDataList(ArrayList<User> friendsList) {
this.friends=friendsList;
arrayAdapter.clear();
arrayAdapter.addAll(friendsList);
arrayAdapter.notifyDataSetChanged();
friendsListView.setAdapter(arrayAdapter);
}
稍后我可以做一些刷新。每次刷新都会发生,回调
setDataList
被触发并listview
更新。
然而,该应用程序崩溃了。 Logcat显示:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mrdreamer.iknow/com.example.mrdreamer.iknow.Social.GetFriends}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
at java.util.ArrayList.addAll(ArrayList.java:188)
at com.example.mrdreamer.iknow.Social.DataListAdapter.<init>(DataListAdapter.java:35)
at com.example.mrdreamer.iknow.Social.GetFriendsFrag.onCreateView(GetFriendsFrag.java:42)
at android.app.Fragment.performCreateView(Fragment.java:2220)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
问题在于:
arrayAdapter=new DataListAdapter(getActivity(),R.layout.userinfo_list_config,null);
最后一个参数不能为null,我明白了。 现在我应该更改为SimpleCursorAdapter还是只对当前代码进行一些更改? :)提前谢谢。我是android developpement的新手。我非常感激。:)
答案 0 :(得分:0)
您可以尝试使用dis` / *********适配器类使用BaseAdapter扩展并使用OnClickListener实现************ / 公共类CustomAdapter扩展BaseAdapter实现OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
/********** Take passed values **********/
activity = a;
data=d;
res = resLocal;
/*********** Layout inflator to call external xml layout () ***********/
inflater = ( LayoutInflater )activity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public TextView textWide;
public ImageView image;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.tabitem, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( ListModel ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.text.setText( tempValues.getCompanyName() );
holder.text1.setText( tempValues.getUrl() );
holder.image.setImageResource(
res.getIdentifier(
"com.androidexample.customlistview:drawable/"+tempValues.getImage()
,null,null));
/******** Set Item Click Listner for LayoutInflater for each row *******/
vi.setOnClickListener(new OnItemClickListener( position ));
}
return vi;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked=====");
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
/**** Call onItemClick Method inside CustomListViewAndroidExample Class ( See Below )****/
sct.onItemClick(mPosition);
}
}
}`
答案 1 :(得分:0)
您不需要使用friendsListView.setAdapter(arrayAdapter);再次在setDataList中,因为您已经在listview上设置了适配器。你已经调用了notifydatasetchange。用新项更新适配器就足够了。