下面是該適配器和主代碼+主XML代碼
ArticleSnippet代碼:
package org.antihero.reader;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ArticleSnippetAdapter extends BaseAdapter {
private List<RssMessage> elements;
private Context mContext;
public ArticleSnippetAdapter(Context mContext, List<RssMessage> elements) {
this.mContext = mContext;
this.elements = elements;
}
// TODO refactor this to limit number of views having an array ...
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout rowLayout;
final RssMessage message = elements.get(position);
if (convertView == null) {
rowLayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.article_snippet, parent, false);
} else {
rowLayout = (RelativeLayout) convertView;
}
TextView title = (TextView) rowLayout.findViewById(R.id.ArticleTitle);
title.setText(message.getTitle());
TextView intro = (TextView) rowLayout.findViewById(R.id.ArticleIntro);
intro.setText(message.getDescription());
Log.i("antiheroReader", "ArticleSnippetAdapter::getView();");
return rowLayout;
}
public int getCount() {
Log.i("antiheroReader", "ArticleSnippetAdapter::getCount(); "+elements.size());
return elements.size();
}
public Object getItem(int position) {
Log.i("antiheroReader", "ArticleSnippetAdapter::getItem();");
return elements.get(position);
}
public long getItemId(int position) {
Log.i("antiheroReader", "ArticleSnippetAdapter::getItemId();");
return position;
}
public void add(RssMessage element) {
Log.i("antiheroReader", "ArticleSnippetAdapter::add();");
elements.add(element);
}
public void empty() {
Log.i("antiheroReader", "ArticleSnippetAdapter::empty();");
elements.clear();
}
}
主代碼:
package org.antihero.reader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import org.antihero.reader.RssMessage;
import org.antihero.reader.AndroidSaxFeedParser;
public class AntiheroReader extends Activity {
private ProgressDialog myProgressDialog = null;
private List<RssMessage> messages;
private ArticleSnippetAdapter articleSnippetAdapter;
private ListView articleSnippetView;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i("antiheroReader", "handleMessage");
articleSnippetAdapter.notifyDataSetChanged();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messages = new ArrayList<RssMessage>();
articleSnippetAdapter = new ArticleSnippetAdapter(this, messages);
articleSnippetView = (ListView) findViewById(R.id.ArticleSnippets);
articleSnippetView.setAdapter(articleSnippetAdapter);
OnClickListener myProgressBarShower = new View.OnClickListener() {
@Override
public void onClick(View v) {
// myProgressDialog is null.
Log.i("antiheroReader", AntiheroReader.this.toString());
myProgressDialog = ProgressDialog.show(AntiheroReader.this, "Please wait...", "Loading data ...", true, false);
Log.i("antiheroReader", myProgressDialog.toString());
// Need to run stuff in the thread or the dialog isn't appearing
new Thread() {
public void run() {
URL feedUrl;
InputStream feedStream;
try{
if(myProgressDialog.isShowing()) {
Log.i("antiheroReader", "Dialog show");
} else {
Log.i("antiheroReader", "Dialog NOT show");
}
try {
feedUrl = new URL("http://192.168.56.1/index.xml");
//feedUrl = new URL("http://www.lalibre.be/rss/");
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
try {
feedStream = feedUrl.openConnection().getInputStream();
//Toast.makeText(AntiheroReader.this, feedStream.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
throw new RuntimeException(e);
}
Log.i("antiheroReader", "ProgressDialog start parsing");
AndroidSaxFeedParser feed = new AndroidSaxFeedParser(feedStream);
List<RssMessage> messages = feed.parse();
articleSnippetAdapter.empty();
myProgressDialog.dismiss();
Log.i("antiheroReader", "ProgressDialog OFF");
int count = messages.size();
for(int i=0; i<count; i++) {
articleSnippetAdapter.add(messages.get(i));
Log.i("antiheroReader", messages.get(i).getTitle());
//Toast.makeText(AntiheroReader.this, messages.get(i).getTitle(), Toast.LENGTH_LONG).show();
}
handler.sendEmptyMessage(0);
} catch (Exception e) { }
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
}
};
Button buttonHome = (Button) findViewById(R.id.ButtonHome);
buttonHome.setOnClickListener(myProgressBarShower);
}
}
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"
android:background="#FFFFFF">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- Logo -->
<ImageView
android:id="@+id/LogoLaLibre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logo_lalibre">
</ImageView>
<!-- Date -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:background="#770037">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/ButtonHome"
style="@style/MenuButtonHome"
android:layout_width="22px"
android:layout_height="wrap_content"
android:text="@string/section_home"/>
<Button
android:id="@+id/ButtonActu"
style="@style/MenuButtonActu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/section_actu"/>
<Button
android:id="@+id/ButtonEconomie"
style="@style/MenuButtonEconomie"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/section_economie"/>
<Button
android:id="@+id/ButtonCulture"
style="@style/MenuButtonCulture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/section_culture"/>
<Button
android:id="@+id/ButtonSport"
style="@style/MenuButtonSport"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/section_sports"/>
<Button
android:id="@+id/ButtonSociete"
style="@style/MenuButtonSociete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/section_societe"/>
</LinearLayout>
</HorizontalScrollView>
<!-- Snippets prototype -->
<ListView
android:id="@+id/ArticleSnippets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#fff"
android:dividerHeight="0sp">
</ListView>
</LinearLayout>
最後的RelativeLayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ArticleSnippet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="@drawable/background_snippet_padding">
<TextView
android:id="@+id/ArticleBreadCrumb"
android:layout_marginLeft="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ArticlePicture"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="#000"
android:text="Actu - Politique" />
<TextView
android:id="@+id/ArticleTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ArticleBreadCrumb"
android:layout_alignLeft="@id/ArticleBreadCrumb"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="#000"
android:text="Test général en sciences, histoire et géo" />
<TextView
android:id="@+id/ArticleIntro"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ArticlePicture"
android:layout_alignLeft="@id/ArticlePicture"
android:textSize="10sp"
android:textColor="#000"
android:text="Semaine particulière pour les élèves de deuxième et cinquième primaire ainsi que de deuxième secondaire. La Communauté française leur a en effet concocté une évaluation externe non certificative en éveil scientifique et éveil historique et géographique" />
<ImageView
android:id="@+id/ArticlePicture"
android:layout_width="80sp"
android:layout_height="56sp"
android:background="@drawable/pict_article">
</ImageView>
</RelativeLayout>
我希望它能幫助....
您可以更新問題以顯示您使用包含RelativeLayout的佈局文件的Java代碼?謝謝! – CommonsWare 2009-11-30 16:52:49
我剛剛在下面發佈代碼.... – 2009-12-01 08:35:12