0

我有一个带有 3 个标签的 rss 提要,每个标签都从一个链接中获取 rss。rss 工作正常,但我想在图片链接的这一部分的每个提要下方添加日期和时间并使其像这个图片链接。谁能帮助我。

这是我的完整代码:

RssTabsActivity.java

  public class RssTabsActivity extends TabActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rss_tabs);
    TabHost tabHost = getTabHost();
    Intent artIntent = new Intent().setClass(this, RssChannelActivity.class);
    artIntent.putExtra("rss-url", "http://www1.up.edu.ph/index.php/feed/");
    String artTabName = getResources().getString(R.string.tab_art);
    TabSpec artTabSpec = tabHost.newTabSpec(artTabName)

.setIndicator(artTabName, getResources().getDrawable(R.drawable.rss_tab_art))

.setContent(artIntent);
    tabHost.addTab(artTabSpec);
    Intent techIntent = new Intent().setClass(this, RssChannelActivity.class);
    techIntent.putExtra("rss-url", "http://www1.up.edu.ph/index.php/feed/");
    String techTabName = getResources().getString(R.string.tab_tech);
    TabSpec techTabSpec = tabHost.newTabSpec(techTabName)

.setIndicator(techTabName, getResources().getDrawable(R.drawable.rss_tab_tech))

.setContent(techIntent);
    tabHost.addTab(techTabSpec);
    Intent sportsIntent = new Intent().setClass(this, RssChannelActivity.class);
    sportsIntent.putExtra("rss-url", "http://www1.up.edu.ph/index.php/feed/");
    String sportsTabName = getResources().getString(R.string.tab_sports);
    TabSpec sportsTabSpec = tabHost.newTabSpec(sportsTabName)

.setIndicator(sportsTabName, getResources().getDrawable(R.drawable.rss_tab_sports))

.setContent(sportsIntent);
    tabHost.addTab(sportsTabSpec);
    tabHost.setCurrentTab(1);
}

}

RssChannelActivity.java

public class RssChannelActivity extends Activity {

private RssChannelActivity local;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_rss_channel);
    String rssUrl = (String)getIntent().getExtras().get("rss-url");
    local = this;
    GetRSSDataTask task = new GetRSSDataTask();
    task.execute(rssUrl);
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
    @Override
    protected List<RssItem> doInBackground(String... urls) {
        try {
            RssReader rssReader = new RssReader(urls[0]);
            return rssReader.getItems();

        } catch (Exception e) {
            Log.e("RssChannelActivity", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<RssItem> result) {

        ListView itcItems = (ListView) findViewById(R.id.rssChannelListView);

        ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>
        (local,android.R.layout.simple_list_item_1, result);
        itcItems.setAdapter(adapter);
        itcItems.setOnItemClickListener(new ListListener(result, local));
    }
}

}

RssItem.java

public class RssItem {

private String title;

private String link;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

@Override
public String toString() {
    return title;
}

}

监听器.java

public class ListListener implements OnItemClickListener {

List<RssItem> listItems;

Activity activity;

public ListListener(List<RssItem> aListItems, Activity anActivity) {
    listItems = aListItems;
    activity  = anActivity;
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(listItems.get(pos).getLink()));

    activity.startActivity(i);

}

 }

RssParseHandler.java

public class RssParseHandler extends DefaultHandler {

private List<RssItem> rssItems;

private RssItem currentItem;

private boolean parsingTitle;

private StringBuffer currentTitleSb;

private boolean parsingLink;

public RssParseHandler() {
    rssItems = new ArrayList<RssItem>();
}

public List<RssItem> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes               
    attributes) throws SAXException {
    if ("item".equals(qName)) {
        currentItem = new RssItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;

        currentTitleSb = new StringBuffer();
    } else if ("link".equals(qName)) {
        parsingLink = true;
    }
}

@Override
public void endElement(String uri, String localName, String qName) throws
     SAXException   {
    if ("item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {

        parsingTitle = false;

        if (currentItem != null) {
            // Set item's title here
            currentItem.setTitle(currentTitleSb.toString());
        }

    } else if ("link".equals(qName)) {
        parsingLink = false;
    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    if (parsingTitle) {
        if (currentItem != null) {

            currentTitleSb.append(new String(ch, start, length));
        }
    } else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;
        }
    }
}

}

RssReader.java

public class RssReader {

private String rssUrl;

public RssReader(String rssUrl) {
    this.rssUrl = rssUrl;
}
public List<RssItem> getItems() throws Exception {
    // SAX parse RSS data
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    RssParseHandler handler = new RssParseHandler();

    saxParser.parse(rssUrl, handler);

    return handler.getItems();
}
} 

RssTabsLayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RssTabsActivity" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="15dp"
            android:visibility="invisible" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab_art"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab_tech"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab_sports"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost>


</RelativeLayout>

RssChannelLayout.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" >

<ListView
    android:id="@+id/rssChannelListView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  >
</ListView>

</LinearLayout>
4

1 回答 1

0

您需要创建自定义 ArrayAdapter:

public class CustomArrayAdapter extends ArrayAdapter<RssItem> {

  private final List<RssItem> aListItems
  private final Activity context;

  public InteractiveArrayAdapter(Activity context, List<RssItem> aListItems) {
    super(context, R.layout.yourcustomlayout, aListItems);
    this.context = context;
    this.aListItems = aListItems;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
      LayoutInflater inflator = context.getLayoutInflater();
      view = inflator.inflate(R.layout.yourcustomlayout, null);
      final ViewHolder viewHolder = new ViewHolder();
      viewHolder.text = (TextView) view.findViewById(R.id.title);
      viewHolder.time = (TextView) view.findViewById(R.id.time);
      view.setTag(viewHolder);
    } else {
      view = convertView;
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(aListItems.get(position).getName());
    holder.time.setText(aListItems.get(position).getTime());
    return view;
  }


  static class ViewHolder {
    protected TextView text;
    protected TextView time;
  }
} 

您的自定义 XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/title"
    android:textSize="30px" >
</TextView>

<TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/time"
    android:textSize="30px" >
</TextView>

于 2013-07-22T08:05:03.143 回答