簡體   English   中英

將listview添加到片段給出錯誤

[英]Adding a listview to fragment giving error

我試圖將自定義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);
    }
}

Top_rated 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)方法必須重寫或實現超類型方法(通過在方法中添加public來解決)

希望我能幫助你。

1. getLayoutInflater()方法給您錯誤,因為您必須從de onCreateView方法中保存充氣器的實例

public LayoutInflater inflater

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    this.inflater = inflater;
}

然后,您必須從該實例中使用它,而要使用getLayoutInflater()

另一種方法是將充氣器作為MyCustomAdapter構造函數中的參數傳遞。

通過擴展ListFragment而不是Fragment可以解決您的問題,但是您說不能這樣做。 因此,讓我們一次解決您的問題:

1:Fragment類型的getLayoutInflater(Bundle)方法不適用於參數()

正如@ G3rcar建議的那樣,您可以聲明LayoutInflater成員字段,並使用傳遞給onCreateView方法的LayoutInflater對其進行初始化,然后在以后的代碼中使用該字段。 另外,您可以隨時在MyCustomAdapater.getView()使用以下方法檢索LayoutInflater

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

2:對於類型TopStoriesFragment而言,未定義方法setListAdapter(null)(通過擴展ListFragment而不是Fragment來解決。)

從您的代碼來看,您可以簡單地消除此調用。 或者,請參閱我對下一個問題的建議。

3:對於類型TopStoriesFragment,未定義方法setListAdapter(TopStoriesFragment.MyCustomAdapter)(通過擴展ListFragment而不是Fragment來解決。)

解決此問題和上一個問題的方法是,當您第一次構建片段的視圖時,存儲對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:類型為TopStoriesFragment的onListItemClick(ListView,View,int,long)方法必須重寫或實現超類型方法(通過在方法中添加public來解決)

您可以為ListView顯式設置一個OnItemClickListener ,以響應項目單擊:

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方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM