即时通讯新的Android开发。我有使用ListFragment和自定义适配器的问题。 我的列表未被对象填充。你能检查我的代码吗?如果我滚动到底部,您将看到一个对象,但之后崩溃。Android - ListFragment和自定义适配器
ListFragmen.java
public class Fragment_List extends ListFragment {
View item_view ;
private List<News> arNews = new ArrayList<News>();
Activity activity = getActivity();
private ArrayAdapter<News> arrayAdapter;
private ListView mListView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
arrayAdapter = new MyListAdapter();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
item_view= inflater.inflate(R.layout.item_view,null);
View v1=inflater.inflate(R.layout.fragment_list, container, false);
mListView= (ListView) v1.findViewById(android.R.id.list);
populateNewsList();
return v1;
}
public Fragment_List() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView.setAdapter(arrayAdapter);
}
private void populateNewsList() {
arNews.add(new News("Ciao",R.drawable.figura_0icona,"uno"));
arNews.add(new News("pippo",R.drawable.figura_1icona,"due"));
arNews.add(new News("pluto",R.drawable.figura_2icona,"tre"));
arNews.add(new News("Ciao",R.drawable.figura_3icona,"quattro"));
arNews.add(new News("pippo",R.drawable.figura_4icona,"cinque"));
arNews.add(new News("pluto",R.drawable.figura_5icona,"sei"));
arNews.add(new News("Ciao",R.drawable.figura_6icona,"sette"));
arNews.add(new News("pippo",R.drawable.figura_7icona,"otto"));
arNews.add(new News("pluto",R.drawable.figura_8icona,"nove"));
// TODO Auto-generated method stub
}
private class MyListAdapter extends ArrayAdapter<News>
{
public MyListAdapter(){
super(getActivity(),R.layout.item_view,arNews);
Toast.makeText(getContext(), "ciao"+arNews.size(), Toast.LENGTH_SHORT).show();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//make sure have a view to work with (may have given null)
View itemView = convertView;
if(itemView==null)
{
itemView=item_view;
if(itemView==null)
Toast.makeText(getContext(), "null", Toast.LENGTH_SHORT).show();
}
//find news to work with
News currentNews = arNews.get(position);
// fill the view
ImageView imageView = (ImageView) itemView.findViewById(R.id.news_icon);
imageView.setImageResource(currentNews.getFotoId());
//make
TextView makeText = (TextView) itemView.findViewById(R.id.news_anteprima);
makeText.setText(currentNews.getAntemprima());
Toast.makeText(getContext(), "testo"+currentNews.getAntemprima(), Toast.LENGTH_SHORT).show();
return itemView;
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
News clickedNews = arNews.get(position);
String message = "You clicked position" + position + " anteprima :" + clickedNews.getAntemprima();
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
super.onListItemClick(l, v, position, id);
}
}
fragment_list.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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@android:id/list"
android:layout_width="600dp"
android:layout_height="426dp" >
</ListView>
</LinearLayout>
非常感谢您!
你可以发布错误堆栈跟踪? –