所以我必須顯示一個列表。從數據庫中檢索數據,作爲JSON文件... 我想要的是關於我的代碼的一些評論,最終我做錯了什麼(如果我是)。或者更好的方法來做到這一點。加載數據,ListView和AsyncTask(需要諮詢)
我檢索的列表可以隨着微調器上的選擇而改變。 列表中顯示的行數是可變的,所以有時列表不會顯示在屏幕的底部。
我已經刪除了一些無用的代碼來解決這個問題。
如果有任何缺失,告訴我,我會編輯代碼:)
public class EcranFavoris extends Activity implements OnScrollListener, OnItemClickListener, OnClickListener, OnItemSelectedListener
{
/* ... */
Spinner spinner;
ArrayAdapter<CharSequence> spinner_adapter;
ProgressDialog mProgressDialog;
ListView listViewFavoris;
/* This extends BaseAdapter */
FavorisListAdapter adapter;
ArrayAdapter<CharSequence> spinner_adapter;
ArrayList<Profil> profil_array;
/* ... */
public static long currentId = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.favoris_layout);
/* ... */
/* This contains the elements of my list. */
profil_array = new ArrayList<Profil>();
mProgressDialog = ProgressDialog.show(this, "Patientez...", "Chargement des favoris.", true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
listViewFavoris = (ListView)findViewById(R.id.favoris_listView);
adapter = new FavorisListAdapter(this, profil_array);
listViewFavoris.setAdapter(adapter);
listViewFavoris.setOnScrollListener(this);
listViewFavoris.setOnItemClickListener(this);
/* This is an AsyncTask i'm using for loading the data from the WebService. */
new WebServiceWork().execute(profil_array);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
if(firstVisibleItem + visibleItemCount == QUANTITY*(currentPage-1))
{
if(pagesLeft == true && !(loadingMore))
{
Log.w("DV", "Loading more");
loadingMore = true;
mProgressDialog = ProgressDialog.show(this, "Patientez...", "Chargement des Favoris.", true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
new WebServiceWork().execute(this.profil_array);
}
else
{
if(pagesLeft == false)
Log.w("Favoris", "No pages left.");
if(loadingMore)
Log.w("Favoris", "Loading more");
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState){}
private class WebServiceWork extends AsyncTask<ArrayList<Profil>, Void, ArrayList<Profil>>
{
//ArrayList<String> profil_string_array;
boolean pagesLeft;
protected void onPreExecute()
{
loadingMore = true;
}
@Override
protected ArrayList<Profil> doInBackground(ArrayList<Profil>... profil_arrays)
{
WebService ws;
int oldSize = profil_arrays[0].size();
/* ... */
//profil_arrays[0].addAll(ws.getProfilList());
/* getProfilList() return an ArrayList<Profil> */
profil_arrays[0] = ws.getProfilList();
pagesLeft = ws.pagesLeft();
return profil_arrays[0];
}
@Override
protected void onPostExecute(ArrayList<Profil> profil_arrays)
{
EcranFavoris.this.profil_array = profil_arrays;
EcranFavoris.this.adapter.notifyDataSetChanged();
EcranFavoris.this.mProgressDialog.dismiss();
EcranFavoris.this.loadingMore = false;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(id != EcranFavoris.currentId)
{
currentPage = 1;
EcranFavoris.currentId = id;
this.pagesLeft = true;
//profil_array = new ArrayList<Profil>();
new WebServiceWork().execute(profil_array);
}
Log.w("DebugFavs","the item: " + parent.getItemAtPosition(pos) + "has been selected. id = " + Long.toString(id));
((TextView)view).setTextColor(Color.WHITE);
((TextView)view).setPadding(0, 0, 0, 4);
}
/* ... */
}
編輯:這裏的適配器。 我沒有發佈函數setListInfo(),因爲它只是基本的TextView和圖像視圖設置。
2ND編輯:嘗試使用ViewHolders,但我的列表不顯示,並且在Logcat中沒有錯誤。
public class FavorisListAdapter extends BaseAdapter
{
private static final int TYPE_MEMBRE = 0;
private static final int TYPE_PUB = 1;
/* La structure utilisée est une liste de "Profil", type que j'ai creer. */
private List<Profil> p_list;
private Context context;
private LayoutInflater inflater;
private static int nbrPub = 0;
public FavorisListAdapter(Context context, List<Profil> profil_list)
{
this.context = context;
p_list = profil_list;
inflater = LayoutInflater.from(context);
}
public int getItemViewType(int position)
{
if(position % 4 == 0)
return TYPE_PUB;
return TYPE_MEMBRE;
}
public int getCount()
{
return p_list.size();
}
public int getViewTypeCount()
{
return 2;
}
public Object getItem(int pos)
{
return p_list.get(pos);
}
public long getItemId(int pos)
{
return pos;
}
static class PubViewHolder
{
WebView pub;
public void init(RelativeLayout layoutItem)
{
pub = (WebView)layoutItem.findViewById(R.id.visites_webView);
}
}
static class MembreViewHolder
{
TextView first_name;
TextView age;
TextView online;
TextView lastVisit;
ImageView image;
public void init(RelativeLayout layoutItem)
{
first_name = (TextView)layoutItem.findViewById(R.id.visites_nameAge);
age = (TextView)layoutItem.findViewById(R.id.visites_cityDistance);
online = (TextView)layoutItem.findViewById(R.id.visites_online);
lastVisit = (TextView)layoutItem.findViewById(R.id.visites_lastVisite);
image = (ImageView)layoutItem.findViewById(R.id.visites_image);
}
}
public View getView(int position, View convertView, ViewGroup parent)
{
RelativeLayout layoutItem = null;
View v = convertView;
int type = getItemViewType(position);
MembreViewHolder membreHolder;
PubViewHolder pubHolder;
if(v == null)
{
switch(type)
{
case TYPE_MEMBRE:
layoutItem = (RelativeLayout)inflater.inflate(R.layout.visites_list_row_layout, parent, false);
membreHolder = new MembreViewHolder();
membreHolder.init(layoutItem);
v.setTag(membreHolder);
this.setListInfo(membreHolder, position);
break;
case TYPE_PUB:
layoutItem = (RelativeLayout)inflater.inflate(R.layout.visites_list_pub_layout, parent, false);
pubHolder = new PubViewHolder();
pubHolder.init(layoutItem);
v.setTag(pubHolder);
this.setListInfo(pubHolder, position);
break;
}
}
else
{
switch(type)
{
case TYPE_MEMBRE:
membreHolder = (MembreViewHolder)v.getTag();
this.setListInfo(membreHolder, position);
break;
case TYPE_PUB:
pubHolder = (PubViewHolder)v.getTag();
this.setListInfo(pubHolder, position);
}
}
return layoutItem;
}
private void setListInfo(PubViewHolder p, int position)
{
p.pub.getSettings().setJavaScriptEnabled(true);
}
private void setListInfo(MembreViewHolder m, int position)
{
m.first_name.setText(p_list.get(position).getChamp("first_name") + ", " + p_list.get(position).getChamp("age") + " ans");
m.age.setText(p_list.get(position).getChamp("city"));
if(p_list.get(position).getChamp("online").equals("Oui"))
{
m.online.setText("En ligne");
m.online.setTextColor(Color.GREEN);
}
else
{
m.online.setText("Hors ligne");
m.online.setTextColor(Color.GRAY);
}
String last_visit = p_list.get(position).getChamp("visit_date");
String formatted = last_visit.substring(6, 8) + "/" + last_visit.substring(4, 6) + "/"
+ last_visit.substring(0, 4);
m.lastVisit.setText(formatted);
}
}
謝謝。
請提供您的自定義適配器的代碼 – AMerle 2012-07-27 08:08:15