2013-03-26 43 views
0

以下是我的代碼。它顯示提取的RSS標題,日期和說明。我想要做的是,當用戶點擊標題時,必須用圖像下拉描述。怎麼做?我卡住了。我做了很多搜索。它還有另一個問題,就是在元素之間顯示文本「obj」。如何在android下拉列表?

This is the snapshot of my application

RSSItem.java 



public class RSSItem { 
    public String title; 
    public String date; 
    public String link; 
    public String description; 
} 

RSSListActivities.java 

    package com.varma.samples.rssreader.ui; 

    import java.net.URL; 
    import java.util.ArrayList; 
    import java.util.List; 

    import javax.xml.parsers.SAXParser; 
    import javax.xml.parsers.SAXParserFactory; 

    import org.xml.sax.InputSource; 
    import org.xml.sax.XMLReader; 

    import android.app.ListActivity; 
    import android.app.ProgressDialog; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.net.Uri; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.TextView; 

    import com.varma.samples.rssreader.R; 
    import com.varma.samples.rssreader.data.RSSItem; 
    import com.varma.samples.rssreader.xmlparser.RSSParser; 

    public class RSSListActivity extends ListActivity { 
    private ArrayList<RSSItem> itemlist = null; 
    private RSSListAdaptor rssadaptor = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     itemlist = new ArrayList<RSSItem>(); 

     new RetrieveRSSFeeds().execute(); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     RSSItem data = itemlist.get(position); 

     Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(data.link)); 

     startActivity(intent); 
    } 

    private void retrieveRSSFeed(String urlToRssFeed,ArrayList<RSSItem> list) 
    { 
     try 
     { 
      URL url = new URL(urlToRssFeed); 
      SAXParserFactory factory = SAXParserFactory.newInstance(); 
      SAXParser parser = factory.newSAXParser(); 
      XMLReader xmlreader = parser.getXMLReader(); 
      RSSParser theRssHandler = new RSSParser(list); 

      xmlreader.setContentHandler(theRssHandler); 

      InputSource is = new InputSource(url.openStream()); 

      xmlreader.parse(is); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    private class RetrieveRSSFeeds extends AsyncTask<Void, Void, Void> 
    { 
     private ProgressDialog progress = null; 

     @Override 
     protected Void doInBackground(Void... params) { 
      retrieveRSSFeed("RSS-URL",itemlist); 

      rssadaptor = new RSSListAdaptor(RSSListActivity.this, R.layout.rssitemview,itemlist); 

      return null; 
     } 

     @Override 
     protected void onCancelled() { 
      super.onCancelled(); 
     } 

     @Override 
     protected void onPreExecute() { 
      progress = ProgressDialog.show(
        RSSListActivity.this, null, "Loading RSS Feeds..."); 

      super.onPreExecute(); 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      setListAdapter(rssadaptor); 

      progress.dismiss(); 

      super.onPostExecute(result); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      super.onProgressUpdate(values); 
     } 
    } 

    private class RSSListAdaptor extends ArrayAdapter<RSSItem>{ 
     private List<RSSItem> objects = null; 

     public RSSListAdaptor(Context context, int textviewid, List<RSSItem> objects) { 
      super(context, textviewid, objects); 

      this.objects = objects; 
     } 

     @Override 
     public int getCount() { 
      return ((null != objects) ? objects.size() : 0); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public RSSItem getItem(int position) { 
      return ((null != objects) ? objects.get(position) : null); 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = convertView; 

      if(null == view) 
      { 
       LayoutInflater vi = (LayoutInflater)RSSListActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       view = vi.inflate(R.layout.rssitemview, null); 
      } 

      RSSItem data = objects.get(position); 

      if(null != data) 
      { 
       TextView title = (TextView)view.findViewById(R.id.txtTitle); 
       TextView date = (TextView)view.findViewById(R.id.txtDate); 
       TextView description = (TextView)view.findViewById(R.id.txtDescription); 

       title.setText(android.text.Html.fromHtml(data.title).toString()); 
       date.setText("on " + android.text.Html.fromHtml(data.date).toString()); 
       description.setText(android.text.Html.fromHtml(data.description).toString()); 
      } 

      return view; 
     } 
    } 

    } 

RSSParser.java 

package com.varma.samples.rssreader.xmlparser; 

import java.util.ArrayList; 

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

import com.varma.samples.rssreader.data.RSSItem; 

public class RSSParser extends DefaultHandler { 
private final static String TAG_ITEM = "item"; 
private final static String[] xmltags = { "title", "link", "pubDate", "description" }; 

private RSSItem currentitem = null; 
private ArrayList<RSSItem> itemarray = null; 
private int currentindex = -1; 
private boolean isParsing = false; 
private StringBuilder builder = new StringBuilder(); 

public RSSParser(ArrayList<RSSItem> itemarray) { 
    super(); 

    this.itemarray = itemarray; 
} 

@Override 
public void characters(char[] ch, int start, int length) throws SAXException { 
    super.characters(ch, start, length); 

    if(isParsing && -1 != currentindex && null != builder) 
    { 
     builder.append(ch,start,length); 
    } 
} 

@Override 
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException { 
    super.startElement(uri, localName, qName, attributes); 

    if(localName.equalsIgnoreCase(TAG_ITEM)) 
    { 
     currentitem = new RSSItem(); 
     currentindex = -1; 
     isParsing = true; 

     itemarray.add(currentitem); 
    } 
    else 
    { 
     currentindex = itemIndexFromString(localName); 

     builder = null; 

     if(-1 != currentindex) 
      builder = new StringBuilder(); 
    } 
} 

@Override 
public void endElement(String uri, String localName, String qName) throws SAXException { 
    super.endElement(uri, localName, qName); 

    if(localName.equalsIgnoreCase(TAG_ITEM)) 
    { 
     isParsing = false; 
    } 
    else if(currentindex != -1) 
    { 
     if(isParsing) 
     { 
      switch(currentindex) 
      { 
       case 0: currentitem.title = builder.toString();   break; 
       case 1: currentitem.link = builder.toString();   break; 
       case 2: currentitem.date = builder.toString();   break; 
       case 3: currentitem.description= builder.toString(); break; 
      } 
     } 
    } 
} 

private int itemIndexFromString(String tagname){ 
    int itemindex = -1; 

    for(int index= 0; index<xmltags.length; ++index) 
    { 
     if(tagname.equalsIgnoreCase(xmltags[index])) 
     { 
      itemindex = index; 

      break; 
     } 
    } 

    return itemindex; 
} 
} 

main.xml中

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

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

rssitemview.xml

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

    <TextView 
     android:id="@+id/txtTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:textSize="20dip" 
     android:padding="5dip" 
     android:text="Title"/> 

    <TextView 
     android:id="@+id/txtDate" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textStyle="italic" 
     android:textSize="10dip" 
     android:padding="5dip" 
     android:text="Date"/> 

    <TextView 
     android:id="@+id/txtDescription" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textStyle="normal" 
     android:textSize="10dip" 
     android:padding="5dip" 
     android:text="Description"/> 

</LinearLayout> 

回答

0

簡單的解決方案。 初始設置能見度文本視圖 txtTitle可見 txtDate無形 txtDescription無形

實施監聽

變化知名度按照烏拉圭回合的要求

+0

我做到了..但如何改變聽衆內部的可見性? – 2013-03-26 05:18:55

+0

title.setOnClickListener(新View.OnClickListener(){ @Override 公共無效的onClick(最終視圖v){// 單擊的視圖變得集中 mFocusedPosition = itemPosition; date.setvisibility(View.visible);描述.setvisibility( View.visible) notifyDataSetChanged(); } }); – sri 2013-03-26 05:31:30

0

則可以使用以下方式完成(我會只提供必要的代碼,因爲RSSParser和圖片加載例如顯然超出了這個問題的範圍):

public class RSSListActivity extends ListActivity { 
    private ArrayList<RSSItem> itemlist = null; 
    private RSSListAdaptor rssadaptor = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.rss_activity); 

     itemlist = new ArrayList<RSSItem>(); 

     // Put hardcoded items temporary 
     for(int i = 0; i < 25; i++) { 
      itemlist.add(new RSSItem("RSS item " + i, 
        "2013/03/" + i, 
        "http://www.northlight-images.co.uk/gallery_images/pics/hood_canal.jpg", 
        "Description of RSS test image #" + i + "Description of RSS test image #" + i + "Description of RSS test image #" + i)); 
     } 

     rssadaptor = new RSSListAdaptor(RSSListActivity.this, R.layout.rssitemview,itemlist); 

     setListAdapter(rssadaptor); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     RSSItem data = itemlist.get(position); 

     Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(data.link)); 

     startActivity(intent); 
    } 

    private void retrieveRSSFeed(String urlToRssFeed,ArrayList<RSSItem> list) { 
     try { 
      URL url = new URL(urlToRssFeed); 
      SAXParserFactory factory = SAXParserFactory.newInstance(); 
      SAXParser parser = factory.newSAXParser(); 
      XMLReader xmlreader = parser.getXMLReader(); 

      InputSource is = new InputSource(url.openStream()); 

      xmlreader.parse(is); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private static class RSSListAdaptor extends ArrayAdapter<RSSItem>{ 
     private List<RSSItem> objects = null; 

     private int mFocusedPosition = -1; 

     private static final int COUNT_ITEM_TYPES = 2; 
     private static final int ITEM_TYPE_NORMAL = 0; 
     private static final int ITEM_TYPE_EXPANDED = 1; 

     public RSSListAdaptor(Context context, int textviewid, List<RSSItem> objects) { 
      super(context, textviewid, objects); 

      this.objects = objects; 
     } 

     @Override 
     public int getCount() { 
      return ((null != objects) ? objects.size() : 0); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public RSSItem getItem(int position) { 
      return ((null != objects) ? objects.get(position) : null); 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = convertView; 
      final int itemType = getItemViewType(position); 

      if(null == view) { 
       final LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       view = vi.inflate(itemType == ITEM_TYPE_NORMAL ? R.layout.rssitemview : R.layout.rssitemview_expanded, null); 
      } 

      final RSSItem data = objects.get(position); 

      if(null != data) { 
       // TODO: ViewHolder pattern should be used here 
       final TextView title = (TextView)view.findViewById(R.id.txtTitle); 
       final TextView date = (TextView)view.findViewById(R.id.txtDate); 
       final TextView description = (TextView)view.findViewById(R.id.txtDescription); 
       final ImageView image = (ImageView)view.findViewById(R.id.image); 

       if (null != title) { 
        title.setText(android.text.Html.fromHtml(data.title).toString()); 
        title.setFocusable(false); 

        final int itemPosition = position; 

        title.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(final View v) { 
          // Clicked view becomes focused 
          mFocusedPosition = itemPosition; 
          notifyDataSetChanged(); 
         } 
        }); 
       } 

       if (null != date) { 
        date.setText("on " + android.text.Html.fromHtml(data.date).toString()); 
       } 

       if (null != description) { 
        description.setText(android.text.Html.fromHtml(data.description).toString()); 
       } 

       if (null != image) { 
        image.setImageURI(Uri.parse(data.link)); 
       } 
      } 

      return view; 
     } 

     @Override 
     public int getViewTypeCount() { 
      return COUNT_ITEM_TYPES; 
     } 

     @Override 
     public int getItemViewType(final int position) { 
      return (position == mFocusedPosition) ? ITEM_TYPE_EXPANDED : ITEM_TYPE_NORMAL; 
     } 
    } 

} 

這裏:

rssitemview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="10dip"> 

    <TextView 
      android:id="@+id/txtTitle" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:textSize="20dip" 
      android:padding="5dip" 
      android:text="Title" /> 

</LinearLayout> 

和rssitemview_expanded.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="10dip"> 

    <TextView 
      android:id="@+id/txtTitle" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:textSize="20dip" 
      android:padding="5dip" 
      android:text="Title" /> 

    <TextView 
      android:id="@+id/txtDate" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="italic" 
      android:textSize="10dip" 
      android:padding="5dip" 
      android:text="Date" /> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="30dp" 
     android:layout_height="30dp" /> 

    <TextView 
      android:id="@+id/txtDescription" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="normal" 
      android:textSize="10dip" 
      android:padding="5dip" 
      android:text="Description" /> 

</LinearLayout> 

關於第二個問題:「它還有另外一個問題,就是它顯示文本 「目標文件」在元素之間「。,請提出另一個問題並提供必要的信息(例如rss數據的例子)。