所以我必须显示一个列表。数据是从数据库中检索的,作为 JSON 文件......我想要的是一些关于我的代码的评论以及最终我做错了什么(如果我是的话)。或者更好的方法来做到这一点。
我检索的列表可以随微调器上选择的任何内容而变化。列表中显示的行数是可变的,因此有时列表不会显示在屏幕底部。
我已经删除了一些无用的代码来解决这个问题。
如果缺少任何东西,请告诉我,我将编辑代码:)
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 和 Image 视图设置。
第二次编辑:尝试使用 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);
}
}
谢谢。