80,359
社区成员
发帖
与我相关
我的任务
分享
mport java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.ccxe.bean.Article;
import cn.com.ccxe.bean.ComplexMessage;
import cn.com.ccxe.message.R;
import cn.com.ccxe.util.AsyncImageLoaderTask;
public class MessageArrayAdapter extends ArrayAdapter<ComplexMessage> {
private int resourceId;
Context mContext;
LayoutInflater mLayoutInflater;
public MessageArrayAdapter(Context context, int textViewResourceId,List<ComplexMessage> cms) {
super(context, textViewResourceId,cms);
this.resourceId = textViewResourceId;
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ComplexMessage cm = getItem(position);
View view;
if (convertView == null) {
view = mLayoutInflater.inflate(resourceId, parent, false);
} else {
view = convertView;
}
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.messageArrayAdapterLinerlayout);
Article[] arts = cm.getNews().getArticles();
if(arts != null && arts.length == 1){
//如果只有一条消息
}else{
//如果有多条消息
for(int i=0;i<arts.length;i++){
if(i ==0){
Article art = arts[i];
FrameLayout frameLayout = new FrameLayout(mContext);
FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
frameLayoutParams.leftMargin=20;
frameLayoutParams.rightMargin = 20;
frameLayout.setLayoutParams(frameLayoutParams);
ImageView iv = new ImageView(mContext);
android.view.ViewGroup.LayoutParams ivLayoutParams = new android.view.ViewGroup.LayoutParams(linearLayout.getWidth(),linearLayout.getWidth()/2);
iv.setLayoutParams(ivLayoutParams);
iv.setTag(art.getPicurl());
new AsyncImageLoaderTask().execute(art.getPicurl(),iv);
TextView tv = new TextView (mContext);
android.view.ViewGroup.LayoutParams tvLayoutParams = new android.view.ViewGroup.LayoutParams(linearLayout.getWidth(),linearLayout.getWidth()/4);
tv.setLayoutParams(tvLayoutParams);
tv.setText(art.getTitle());
frameLayout.addView(iv);
frameLayout.addView(tv);
linearLayout.addView(frameLayout);
}else{
}
}
}
return view;
}
}
DatabaseHelper dbHelper = DBUtil.getDatabaseHelper(MainActivity.this);
Cursor c =null;
try{
SQLiteDatabase sqliteDatabase = dbHelper.getReadableDatabase();
c = sqliteDatabase.rawQuery("SELECT * FROM message ORDER BY _id DESC limit 0,10 ", new String[]{});
while (c.moveToNext()) {
int _id = c.getInt(c.getColumnIndex("_id"));
idMax=idMax>_id?idMax:_id;
idMin=idMin>_id?_id:idMin;
String message = c.getString(c.getColumnIndex("message"));
Gson g=new Gson();
ComplexMessage cm=g.fromJson(message,new TypeToken<ComplexMessage>(){}.getType());
mListItems.add(cm);
}
}catch(Exception e){
Log.e(MainActivity.class.toString(), e.toString());
}finally{
if(c != null){
c.close();
}
}
mAdapter = new MessageArrayAdapter(this, R.layout.list_view1_item, mListItems);
// You can also just use setListAdapter(mAdapter)
actualListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
mPullRefreshListView.getRefreshableView().setSelection(mAdapter.getCount()-1);
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
@Override
protected String[] doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(String[] result) {
DatabaseHelper dbHelper = DBUtil.getDatabaseHelper(MainActivity.this);
SQLiteDatabase sqliteDatabase = dbHelper.getReadableDatabase();
Cursor c = sqliteDatabase.rawQuery("select * from message where _id < ? order by _id desc limit 0,10 ", new String[]{idMin+""});
int count = c.getCount();
try{
if(c.getCount() > 0){
while (c.moveToNext()) {
int _id = c.getInt(c.getColumnIndex("_id"));
idMax=idMax>_id?idMax:_id;
idMin=idMin>_id?_id:idMin;
String message = c.getString(c.getColumnIndex("message"));
Gson g=new Gson();
ComplexMessage cm=g.fromJson(message,new TypeToken<ComplexMessage>(){}.getType());
mListItems.addFirst(cm);
}
}
}catch(Exception e){
Log.e(MainActivity.class.toString(), e.toString());
}finally{
c.close();
}
mAdapter.notifyDataSetChanged();
// // Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
mPullRefreshListView.getRefreshableView().setSelection(count-1);
super.onPostExecute(result);
}
}