gpt4 book ai didi

java - 将 ListView 添加到 fragment 给出错误

转载 作者:行者123 更新时间:2023-12-01 18:30:13 25 4
gpt4 key购买 nike

我正在尝试将自定义 ListView 添加到扩展 fragment 的类中。但它在 4 个地方给出了错误。这是代码:

TopStoriesFragment.java

public class TopStoriesFragment extends Fragment {

public String thehindu = "http://www.thehindu.com/news/?service=rss";
public String toi = "http://timesofindia.feedsportal.com/c/33039/f/533965/index.rss";
public String reuters = "http://feeds.reuters.com/reuters/topNews";
public String deccanherald = "http://www.deccanherald.com/rss-internal/top-stories.rss";
public String currenturl = thehindu;
AlertDialog levelDialog;
public String fakingnews = "http://thepuntended.wordpress.com/";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

feedTitle = (TextView) rootView.findViewById(R.id.feedtitle);
feedDescribtion = (TextView) rootView.findViewById(R.id.feeddescribtion);

startReadRss();

return rootView;
}

public class RssLoadingTask extends AsyncTask<Void, Void, Void> {

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
displayRss();
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
preReadRss();
}

@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
// super.onProgressUpdate(values);
}

@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
readRss();
return null;
}

}

private RSSFeed myRssFeed = null;

TextView feedTitle;
TextView feedDescribtion;

// TextView feedPubdate;
// TextView feedLink;

public class MyCustomAdapter extends ArrayAdapter<RSSItem> {

public MyCustomAdapter(Context context, int textViewResourceId,
List<RSSItem> list) {
super(context, textViewResourceId, list);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);

View row = convertView;
if (row == null) {

//------------------------------- Error 1-------------------------------
LayoutInflater inflater = getLayoutInflater();
//------------------------------- Error 1-------------------------------

row = inflater.inflate(R.layout.row, parent, false);
}

TextView listTitle = (TextView) row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listPubdate = (TextView) row
.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());

if (position % 2 == 0) {
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
} else {
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}

return row;
}
}

private void startReadRss() {
new RssLoadingTask().execute();
}

private void preReadRss() {
feedTitle.setText("--- wait ---");
feedDescribtion.setText("");
// feedPubdate.setText("");
// feedLink.setText("");

//------------------------------- Error 2 (Solved)-------------------------------
setListAdapter(null);
//------------------------------- Error 2-------------------------------

Toast.makeText(getActivity(), "Reading RSS, Please wait.", Toast.LENGTH_LONG)
.show();
}

private void readRss() {

try {
URL rssUrl = new URL(currenturl);
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();
}
}

private void displayRss() {
if (myRssFeed != null) {
Calendar c = Calendar.getInstance();
// String strCurrentTiime = "\n(Time of Reading - "
// + c.get(Calendar.HOUR_OF_DAY)
// + " Hrs. : "
// + c.get(Calendar.MINUTE) + " Mins.)\n" +c.get(Calendar.AM_PM);

SimpleDateFormat sdf = new SimpleDateFormat(
"MMMM-dd-yyyy HH:mm:ss a");
String strCurrentTiime = sdf.format(c.getTime());

feedTitle.setText(myRssFeed.getTitle());
feedDescribtion.setText(strCurrentTiime);
// feedPubdate.setText(myRssFeed.getPubdate());
// feedLink.setText(myRssFeed.getLink());

MyCustomAdapter adapter = new MyCustomAdapter(getActivity(), R.layout.row,
myRssFeed.getList());

//------------------------------- Error 3 (Solved)-------------------------------
setListAdapter(adapter);
//------------------------------- Error 3-------------------------------

}
}

@Override
//------------------------------- Error 4 (Solved)-------------------------------
protected void onListItemClick(ListView l, View v, int position, long id) {
//------------------------------- Error 4-------------------------------
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(), ShowDetails.class);
Bundle bundle = new Bundle();
bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
bundle.putString("keyDescription", myRssFeed.getItem(position)
.getDescription());
bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
intent.putExtras(bundle);
startActivity(intent);
}
}

顶级 Fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:orientation="vertical" >

<TextView
android:id="@+id/feedtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Black"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textStyle="bold" />

<TextView
android:textColor="@color/Black"
android:id="@+id/feeddescribtion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />

</LinearLayout>

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@drawable/divider" />

<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No Data" />

</LinearLayout>

有错误的4处已在行间标出。错误是:

**1:Fragment 类型中的方法 getLayoutInflater(Bundle) 不适用于参数 () **

2:TopStoriesFragment 类型未定义 setListAdapter(null) 方法(通过扩展 ListFragment 而不是 Fragment 来解决。)

3:TopStoriesFragment 类型未定义 setListAdapter(TopStoriesFragment.MyCustomAdapter) 方法(通过扩展 ListFragment 而不是 Fragment 来解决。)

4:TopStoriesFragment类型的方法onListItemClick(ListView, View, int, long)必须重写或实现父类(super class)型方法(通过在方法中添加public来解决)

最佳答案

您的问题似乎可以通过扩展 ListFragment 而不是 Fragment 来解决,但您说您不能这样做。因此,让我们一次一个地解决您的问题:

1: The method getLayoutInflater(Bundle) in the type Fragment is not applicable for the arguments ()

正如 @G3rcar 所建议的,您可以声明一个 LayoutInflater 成员字段,使用传递给 onCreateView 方法的 LayoutInflater 对其进行初始化,然后使用你后面的代码中的那个。或者,您可以随时在 MyCustomAdapter.getView() 中检索 LayoutInflater,方法是:

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);

2: The method setListAdapter(null) is undefined for the type TopStoriesFragment (Solved by extending ListFragment instead of Fragment.)

从您的代码来看,您可以简单地消除此调用。或者,请参阅我对下一个问题的建议。

3: The method setListAdapter(TopStoriesFragment.MyCustomAdapter) is undefined for the type TopStoriesFragment (Solved by extending ListFragment instead of Fragment.)

解决这个问题和上一个问题的方法是在第一次构造 fragment View 时存储对 ListView 的引用:

TextView feedTitle;
TextView feedDescribtion;
ListView listView;

public View onCreateView(...) {
...
feedTitle = (TextView) rootView.findViewById(R.id.feedtitle);
feedDescribtion = (TextView) rootView.findViewById(R.id.feeddescribtion);
listView = (ListView) rootView.findViewById(android.R.id.list);
...
}

然后就可以直接为listView设置适配器了:

private void displayRss() {
...
listView.setAdapter(adapter); // instead of "setAdapter(adapter)"
}

4: The method onListItemClick(ListView, View, int, long) of type TopStoriesFragment must override or implement a supertype method (Solved by adding public to the method)

您可以显式设置OnItemClickListenerListView 响应项目点击:

listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
... // body of your current onListItemClick() method
}
});

然后,您可以从 TopStoriesFragment 中删除 onListItemClick 方法。

关于java - 将 ListView 添加到 fragment 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24535900/

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