gpt4 book ai didi

Android - ListFragment 和自定义适配器

转载 作者:行者123 更新时间:2023-11-30 03:09:39 26 4
gpt4 key购买 nike

我是 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 列表.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>

非常感谢!

最佳答案

首先当您使用 ListFragment 时,您需要在布局 (fragment_list.xml) 上声明两个元素:


  1. ID 为 @android:id/empty 的 TextView
    • 当 Adapter 为空显示文本时由 android 使用


  1. ID 为 @android:id/list 的 ListView
    • 当 Adapter 完全生成 ListView 时由 android 使用

这个 id 在 android 核心中,然后 android 隐式地处理它,添加、刷新和删除元素。

查看您的代码,您正在为 ListView 声明一个对象。这是错误的。

删除此对象的声明。

其次,您的适配器。您是 ArrayList 的扩展,在您的构造函数上需要调用 super 构造函数,给定一个 Context、一个 resource、一个 ListAndroid Developer Reference: ArrayAdapter .ArrayAdapter(上下文上下文,int 资源,T[] 对象)默认情况下,int 资源 为 0(零)。

总结

  1. 将两个带有 id 的项目添加到您的 layout.xml。

    • ID 为 @android:id/empty 的 TextView
    • ID 为 @android:id/list 的 ListView
  2. 在您的 Fragment_List 类上删除 ListView 的所有实例。

  3. 在给定上下文、资源、元素列表的适配器上调用 super 构造函数。

  4. 从您的 Fragment_List 类调用一个新的构造函数。 Android Developer Reference: ListFragment .假设您需要针对 ListFragment.setAdapter() 调用 ListFragment.setListAdapter()

  5. 试试吧!我认为这是可行的!

希望对你有帮助!

关于Android - ListFragment 和自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21193798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com