HI, everybody Andrews research engineer, ask questions as
Imitation online custom written a ArrayAdapter, as follows:
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{
private int resourceId;
Context mContext;
LayoutInflater mLayoutInflater;
public MessageArrayAdapter (Context context, int textViewResourceId, Listcms) {
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) {
/ / If there is only one message
} Else {
/ / If multiple messages
for (int i = 0; iif (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;
}
}
Then, in the Activity call as follows:
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() {} 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);.
Discovery opens the application, even if the data is not displayed.
The MainActivity realized OnScrollListener, made an asynchronous thread, load the data when the finger paddling when they can show up, but every time it loads all the data is displayed, not only demonstrate the new additions
private class GetDataTask extends AsyncTask{
@ 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 _idint 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() {} 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);
}
}
Please advice, find a day do not know where the problem.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
See friends said in the main thread which can not have network operations or database operations, so that a few lines of code to get the data into several DEMO initialization data from the database in the Activity, found that this can show up.
So you can think straight points to open the display data in the database should be how to operate it? Also use asynchronous way?
Reply:
The most critical is that when clicking refresh, why always repeat refresh all data?
Reply:
There is no master come urged Online.
Reply:
Partial refresh with notifyDataSetChanged
Reply:
4th Floor, positive solution, add your adapter data list, with notifyDataSetChanged, achieved partial refresh
Reply:
mAdapter.notifyDataSetChanged ();
Code related to this method ah
No comments:
Post a Comment