所以我必须显示一个清单。数据作为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和图像视图设置。
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);
}
}
感谢。
答案 0 :(得分:0)
一些评论:
ProgressDialog.show(this, "Patientez...", "Chargement des Favoris.", true);
。请改用ProgressDialog.show(this, EcranFavoris.this.getString(R.string.title), EcranFavoris.this.getString(R.string.message), true);
,在需要显示字符串的任何地方(不在logcat中,没关系)。
这使您可以轻松地进行多语言应用程序。preExecute
方法中显示对话框,并在postExecute
方法中将其关闭。