不知道是不是数据接口没有对上的问题,帮帮我!
public class CollectionActivity extends AppCompatActivity implements NewsAdapter.CallBack {
private List<News> newsList = new ArrayList<>();
private ListView collection;
private NewsAdapter adapter;
private MyDatabaseHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection);
helper = new MyDatabaseHelper(this, "UserDB.db", null, 1);
initView();
initNews();
collection.setAdapter(adapter);
collection.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String url = newsList.get(i).getNews_url();
Intent intent = new Intent(CollectionActivity.this, ShowNewsActivity.class);
intent.putExtra("url", url);
startActivity(intent);
}
});
}
private void initNews() {
new Thread(new Runnable() {
@Override
public void run() {
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from Collection_News", null);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
//遍历Cursor对象,取出数据并打印
String news_url = cursor.getString(cursor.getColumnIndex("news_url"));
String news_title = cursor.getString(cursor.getColumnIndex("news_title"));
String news_date = cursor.getString(cursor.getColumnIndex("news_date"));
String news_author = cursor.getString(cursor.getColumnIndex("news_author"));
String news_picurl = cursor.getString(cursor.getColumnIndex("news_picurl"));
Bitmap bitmap = HttpUtils.decodeUriAsBitmapFromNet(news_picurl);
News news = new News(bitmap, news_title, news_url, news_picurl, news_date, news_author);
newsList.add(news);
} while (cursor.moveToNext());
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "收藏夹为空!", Toast.LENGTH_SHORT).show();
}
});
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
cursor.close();
db.close();
}
}).start();
/* SharedPreferences sp = getSharedPreferences("collection", MODE_PRIVATE);
HashMap<String, String> map = (HashMap<String, String>) sp.getAll();
Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> data = iter.next();
//取出标题
String title = data.getKey();
//取出网址
String url = data.getValue();
//往数据源中填充数据
News news = new News(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher), title, url);
newsList.add(news);
}*/
}
private void initView() {
collection = (ListView) findViewById(R.id.listview_collection);
adapter = new NewsAdapter(this, R.layout.list_view_item, newsList, this);
}
@Override
public void click(View view) {
int position = Integer.parseInt(view.getTag().toString());
SQLiteDatabase db = helper.getReadableDatabase();
db.execSQL("delete from Collection_News where news_title=?",
new String[]{newsList.get(position).getNews_title()});
db.close();
newsList.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(this, "该新闻已被移除收藏夹!", Toast.LENGTH_SHORT).show();
/*
int position = Integer.parseInt(view.getTag().toString());
SharedPreferences sp = getSharedPreferences("collection", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove(newsList.get(position).getNews_title());
editor.apply();
newsList.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(this, "该新闻已被移除收藏夹!", Toast.LENGTH_SHORT).show();
*/
}
}
public class News {
private Bitmap news_img;
private String news_title;
private String news_url;
private String news_picurl;
private String uniquekey;
private String date;
private String author_name;
public News(Bitmap news_img, String news_title, String news_url, String news_picurl, String date, String author_name) {
this.news_img = news_img;
this.news_title = news_title;
this.news_url = news_url;
this.news_picurl = news_picurl;
this.date = date;
this.author_name = author_name;
}
public News(Bitmap news_img, String news_title, String news_url, String news_picurl, String uniquekey, String date, String author_name) {
this.news_img = news_img;
this.news_title = news_title;
this.news_url = news_url;
this.news_picurl = news_picurl;
this.uniquekey = uniquekey;
this.date = date;
this.author_name = author_name;
}
public String getNews_picurl() {
return news_picurl;
}
public void setNews_picurl(String news_picurl) {
this.news_picurl = news_picurl;
}
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAuthor_name() {
return author_name;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public Bitmap getNews_img() {
return news_img;
}
public void setNews_img(Bitmap news_img) {
this.news_img = news_img;
}
public String getNews_title() {
return news_title;
}
public void setNews_title(String news_title) {
this.news_title = news_title;
}
public String getNews_url() {
return news_url;
}
public void setNews_url(String news_url) {
this.news_url = news_url;
}
}