gpt4 book ai didi

android - 如何在 fragment 中拥有更多自定义适配器的项目?

转载 作者:行者123 更新时间:2023-11-30 02:32:47 25 4
gpt4 key购买 nike

在我的 Fragment 中,我正在显示来自网络新闻页面的 RSS 提要,但我只能从 one 获取这些数据网址。然后我在我的 Custom Listview 中显示这个单一 Url 的数据。那么,有没有一种方法可以显示来自多个显示在不同 ListView 但在同一 Fragment 中的 Url 的数据?也许我问的不清楚,如果是的话请评论并检查我在下面制作的图片:

enter image description here

这是我的 fragment 中的代码:

try {
URL rssUrl = new URL("URL1");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);

myRssFeed = myRSSHandler.getFeed();

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


if (myRssFeed!=null)
{
ListView list = (ListView)view.findViewById(android.R.id.list);
CustomList adapter = new CustomList(getActivity(),myRssFeed.getList());
adapter.addAll();
list.setAdapter(adapter);

}
else
Toast.makeText(getActivity(), "....",
Toast.LENGTH_LONG).show();
return view;
}

编辑Url 的自定义 ListView:

public class CustomList extends ArrayAdapter<RSSItem> {

private static Activity context = null;
private final List<RSSItem> web;
private SharedPreferences mPrefs;
final String read2 = "text1";
final String testoread2 = "img1";

public CustomList(Activity context, List<RSSItem> web) {
super(context, R.layout.custom_list, web);
CustomList.context = context;
this.web = web;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View rowView = inflater.inflate(R.layout.custom_list, null, true);
mPrefs = PreferenceManager.getDefaultSharedPreferences(context);

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

....


return rowView;

}

如有任何建议,我们将不胜感激。

编辑2我现在拥有的:

try {
URL rssUrl = new URL("URL1");
URL rssUrl2 = new URL("URL2");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();

SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
SAXParser mySAXParser2 = mySAXParserFactory.newSAXParser();

XMLReader myXMLReader = mySAXParser.getXMLReader();
XMLReader myXMLReader2 = mySAXParser2.getXMLReader();

RSSHandler myRSSHandler = new RSSHandler();
RSSHandler2 myRSSHandler2 = new RSSHandler2();

myXMLReader.setContentHandler(myRSSHandler);
myXMLReader2.setContentHandler(myRSSHandler2);

InputSource myInputSource = new InputSource(rssUrl.openStream());
InputSource myInputSource2 = new InputSource(rssUrl2.openStream());

myXMLReader.parse(myInputSource);
myXMLReader2.parse(myInputSource2);

myRssFeed = myRSSHandler.getFeed();
myRssFeed2 = myRSSHandler2.getFeed();

最佳答案

不是将 ListView 放在 xml 中,而是动态加载它:

ListView list = new ListView(this);
CustomList adapter = new CustomList(getActivity(),myRssFeed.getList());
adapter.addAll();
list.setAdapter(adapter);

然后在您的 xml 中放置一个具有这些属性的 LinearLayout:

android:id="@+id/layoutMain"
android:orientation="vertical"
android:layout_height="wrap_content"

回到 Java - 将 ListView 添加到 LinearLayout:

LinearLayout layoutMain = (LinearLayout)findViewById(R.id.layoutMain);
layoutMain.addView(list);

重复此步骤几次以使用多个列表填充您的 fragment

编辑:

关于 RssHandlers - 请创建一个函数来处理这些:

RSSHandler createRssHandler(SAXParserFactory mySAXParserFactory, String url)
{
URL rssUrl = new URL(url);
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);

return myRSSHandler;
}

现在你可以将它与数组一起使用了:

String[] urls = new String["url1", "url2"];
RssHandlers[] handlers = new RssHandlers[urls.length];

for (int i = 0; i < urls.length; i++)
{
handlers[i] = createRssHandler(mySAXParserFactory, urls[i]);
}

将它与您的列表结合起来会得到类似的结果:

String[] urls = new String["url1", "url2"];
RssHandlers[] handlers = new RssHandlers[urls.length];
LinearLayout layoutMain = (LinearLayout)findViewById(R.id.layoutMain);

for (int i = 0; i < urls.length; i++)
{
handlers[i] = createRssHandler(mySAXParserFactory, urls[i]);

ListView list = new ListView(this);
CustomList adapter = new CustomList(getActivity(), handlers[i].getList());
adapter.addAll();
list.setAdapter(adapter);
layoutMain.addView(list);
}

关于android - 如何在 fragment 中拥有更多自定义适配器的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080605/

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