0

オンラインの場合、私はニュース Android アプリに取り組んでいます。RSSフラックスでニュースを取得し、RSSItemAdapter

このコードで:

import java.io.InputStream;
import java.io.IOException;
import java.net.URL;

class RSSItemAdapter extends ArrayAdapter<RSSItem> {

    private final Context context;
    final Comment comment = null;
    private CommentsDataSource datasource;

    public RSSItemAdapter(Context context, int textViewResourceId, List<RSSItem> items) {
        super(context, textViewResourceId, items);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.rssitem, null);
        }
        final RSSItem item = getItem(position);
        TextView tv = (TextView) v.findViewById(R.id.title);
        // tv.setText("<a href="+'"'+item.getUrl()+'"'+">"+item.getTitle()+"</a>");

        // tv.setText(Html.fromHtml("<a href=\"http://www.google.com\">"+item.getTitle()+"</a>"));
        tv.setText(item.getTitle());
        //tv.setMovementMethod(LinkMovementMethod.getInstance());

        TextView tv1 = (TextView) v.findViewById(R.id.description);
        tv1.setText(item.getDescription());

        TextView tv2 = (TextView) v.findViewById(R.id.pubdate);

        Date date = item.getPubDate();  
        Format formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
        String s = formatter.format(date);

        tv2.setText(s);

        TextView tv3 = (TextView) v.findViewById(R.id.lien);
        tv3.setText(item.getUrl());

        ImageView iv = (ImageView) v.findViewById(R.id.img);
        try {
            iv.setImageDrawable(drawableFromURL(item.getImageUrl(), item.getImageTitle()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return v;
    }

    private Drawable drawableFromURL(String url, String srcName) throws java.net.MalformedURLException, IOException 
    {
        return Drawable.createFromStream(((InputStream) new URL(url).getContent()), srcName);
    }

}

メインクラスの後、私はそれを次のように呼び出します:

if (isOnline()) { 
    ListView rssItemList = (ListView) findViewById(R.id.rssListview);
    FeedSource feedSource = new HttpFeedSource();
    RSSItemAdapter adapter = new RSSItemAdapter(this, R.layout.rssitem, feedSource.getFeed());
    rssItemList.setAdapter(adapter);
} 

では、SQLite データベースに保存されている日付を取得して、このようにアダプターで出力するにはどうすればよいでしょうか?

4

1 に答える 1

0

私は解決策を見つけました:

package news.mobile;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter {
    public static final String MYDATABASE_NAME = "mobilealgerie.db";
    public static final String MYDATABASE_TABLE = "mobilealgerie";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_ID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_PUBDATE = "pubdate";

    // create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE =
        "create table " + MYDATABASE_TABLE + " ("
      + KEY_ID + " integer primary key autoincrement, "
      + KEY_TITLE + " text not null ,"
      + KEY_DESCRIPTION + " text not null,"
      + KEY_PUBDATE + " text not null);";

    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteAdapter(Context c) {
        context = c;
    }

    public SQLiteAdapter openToRead() throws SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;  
    }

    public SQLiteAdapter openToWrite() throws SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;  
    }

    public void close() {
        sqLiteHelper.close();
    }

    public long insert(String TITLE, String DESCRIPTION, String PUBDATE) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_TITLE, TITLE);
        contentValues.put(KEY_DESCRIPTION, DESCRIPTION);
        contentValues.put(KEY_PUBDATE, PUBDATE);
        return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
    }

    public int deleteAll() {
        return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
    }

    public Cursor queueAll() {
        String[] columns = new String[] {KEY_ID, KEY_TITLE, KEY_DESCRIPTION, KEY_PUBDATE};
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
            null, null, null, null, null);
        return cursor;
    }

    public class SQLiteHelper extends SQLiteOpenHelper {
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(SCRIPT_CREATE_DATABASE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
        }
    }
}

于 2012-09-10T13:50:39.220 に答える