Java 将listview添加到给定错误的片段

Java 将listview添加到给定错误的片段,java,android,android-fragments,android-listview,custom-adapter,Java,Android,Android Fragments,Android Listview,Custom Adapter,我正在尝试将自定义listview添加到扩展片段的类中。但它在4个地方给出了错误。代码如下: TopStoriesFragment.java public class TopStoriesFragment extends Fragment { public String thehindu = "http://www.thehindu.com/news/?service=rss"; public String toi = "http://timesofindia.feedspor

我正在尝试将自定义listview添加到扩展片段的类中。但它在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);
    }
}
公共类TopStoriesFragment扩展片段{
公共字符串thehindu=”http://www.thehindu.com/news/?service=rss";
公共字符串toi=”http://timesofindia.feedsportal.com/c/33039/f/533965/index.rss";
公共字符串=”http://feeds.reuters.com/reuters/topNews";
公共字符串deccanherald=”http://www.deccanherald.com/rss-internal/top-stories.rss";
公共字符串currenturl=印度教;
AlertDialog-levelDialog;
公共字符串伪造新闻=”http://thepuntended.wordpress.com/";
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u top\u rated,容器,false);
feedTitle=(TextView)rootView.findViewById(R.id.feedTitle);
feedDescription=(TextView)rootView.findViewById(R.id.feedDescription);
startReadRss();
返回rootView;
}
公共类RssLoadingTask扩展异步任务{
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
displayRss();
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
preReadRss();
}
@凌驾
受保护的void onProgressUpdate(void…值){
//TODO自动生成的方法存根
//super.onProgressUpdate(值);
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
readRss();
返回null;
}
}
私有RSSFeed myRssFeed=null;
文本视图feedTitle;
文本视图反馈描述;
//TextView feedPubdate;
//文本视图feedLink;
公共类MyCustomAdapter扩展了ArrayAdapter{
公共MyCustomAdapter(上下文,int textViewResourceId,
(列表){
super(上下文、textViewResourceId、列表);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
//返回super.getView(position、convertView、parent);
视图行=转换视图;
if(行==null){
//-------------------------------错误1-------------------------------
LayoutInflater充气机=getLayoutInflater();
//-------------------------------错误1-------------------------------
行=充气机。充气(R.layout.row,父级,false);
}
TextView listTitle=(TextView)row.findViewById(R.id.listTitle);
listTitle.setText(myRssFeed.getList().get(position.getTitle());
TextView listPubdate=(TextView)行
.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position.getPubdate());
如果(位置%2==0){
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
}否则{
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}
返回行;
}
}
私有void startReadRss(){
新的RssLoadingTask().execute();
}
私有void preReadRss(){
feedTitle.setText(“--wait--”);
FeedDescription.setText(“”);
//feedPubdate.setText(“”);
//feedLink.setText(“”);
//-------------------------------错误2(已解决)-------------------------------
setListAdapter(空);
//-------------------------------错误2-------------------------------
Toast.makeText(getActivity(),“正在阅读RSS,请稍候”,Toast.LENGTH\u LONG)
.show();
}
私有void readRss(){
试一试{
URL rssUrl=新URL(当前URL);
SAXParserFactory mySAXParserFactory=SAXParserFactory
.newInstance();
SAXParser mySAXParser=mySAXParserFactory.newSAXParser();
XMLReader myXMLReader=myaxParser.getXMLReader();
RSSHandler-myRSSHandler=新的RSSHandler();
setContentHandler(myRSSHandler);
InputSource myInputSource=新的InputSource(rssUrl.openStream());
parse(myInputSource);
myRssFeed=myRSSHandler.getFeed();
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有void displayRss(){
如果(myRssFeed!=null){
Calendar c=Calendar.getInstance();
//String strcurrenttime=“\n(读取时间--”
//+c.get(日历小时)
//+“小时数:”
//+c.get(Calendar.MINUTE)+“Mins.\n”+c.get(Calendar.AM\u PM);
SimpleDataFormat sdf=新SimpleDataFormat(
“mm-dd-yyyy HH:mm:ss a”);
字符串strcurrenttime=sdf.format(c.getTime());
feedTitle.setText(myRssFeed.getTitle());
feedDescription.setText(strcurrenttime);
//feedPubdate.setText(myRssFeed.getPubdate());
//feedLink.setText(myRssFeed.getLink());
MyCustomAdapter=新的MyCustomAdapter(getActivity(),R.layout.R
<?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>
public LayoutInflater inflater

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    this.inflater = inflater;
}
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
    Context.LAYOUT_INFLATER_SERVICE);
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);
    ...
}
private void displayRss() {
    ...
    listView.setAdapter(adapter); // instead of "setAdapter(adapter)"
}
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
    }
});