Java ListView随机订购的物品?
我的列表视图是随机排序的项目,当我向下或向上滚动项目位置时,我尝试了许多方法来解决这个问题,但没有成功 我在谷歌上搜索过,找到了太多的方法来解决这个与扩展listview相关的问题,但它不能与我的代码一起工作 请帮忙 这是listview代码Java ListView随机订购的物品?,java,android,listview,Java,Android,Listview,我的列表视图是随机排序的项目,当我向下或向上滚动项目位置时,我尝试了许多方法来解决这个问题,但没有成功 我在谷歌上搜索过,找到了太多的方法来解决这个与扩展listview相关的问题,但它不能与我的代码一起工作 请帮忙 这是listview代码 static class ViewHolder { ImageView play; ImageView download; TextView rtitle; TextView size; Text
static class ViewHolder {
ImageView play;
ImageView download;
TextView rtitle;
TextView size;
TextView downloads;
RatingBar ratingsmall;
ImageView ratebutton;
long tonid;
TextView voters;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//Get the current location object
JSONObject r = (JSONObject) getItem(position);
//Inflate the view
if (convertView == null) {
convertView = mInflater.inflate(R.layout.ringtone_bit, null);
holder = new ViewHolder();
ImageView play = (ImageView) convertView.findViewById(R.id.play);
ImageView download = (ImageView) convertView.findViewById(R.id.download);
ImageView ratebutton = (ImageView) convertView.findViewById(R.id.ratebutton);
TextView rtitle = (TextView) convertView.findViewById(R.id.rtitle);
TextView size = (TextView) convertView.findViewById(R.id.size);
TextView downloads = (TextView) convertView.findViewById(R.id.downloads);
TextView voters = (TextView) convertView.findViewById(R.id.voters);
TextView personname = (TextView) convertView.findViewById(R.id.personname);
TextView date = (TextView) convertView.findViewById(R.id.date);
RatingBar ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall);
//setdate
try {
Date date_g = new Date(r.getLong("timestamp") * 1000);
date.setText(date_g.toLocaleString());
} catch (JSONException e2) {}
//set person name
try {
String client_name = (r.getString("personname").equals("null") == true) ? "ghost" : r.getString("personname");
personname.setText(client_name);
} catch (JSONException e2) {}
//set total votars and vote avarage
try {
float z = (float) r.getInt("rate");
voters.setText(" ( " + r.getLong("voters") + " ) / " + z);
} catch (JSONException e2) {}
//set rating bar
try {
float z = (float) r.getInt("rate");
ratingsmall.setRating(z);
} catch (JSONException e2) {}
//set ringtone Name as defualt device language
try {
String name = (lang.equals("English") == true) ? r.getString("en_name") : r.getString("ar_name");
rtitle.setText(name);
} catch (JSONException e2) {}
//ringtone file size
try {
size.setText(r.getString("size"));
} catch (JSONException e2) {}
//set downloads
try {
downloads.setText(String.valueOf(r.getLong("downloads")));
} catch (JSONException e2) {}
//set ringtone ID toneid
try {
holder.tonid = r.getLong("toneid");
download.setTag(r.getLong("toneid"));
ratebutton.setTag(r.getLong("toneid"));
} catch (JSONException e1) {}
//set download stram url to play icon
try {
play.setTag(r.getString("stream_url"));
} catch (JSONException e) {}
//add play listener test Ringtone before download it
play.setOnClickListener(onClickListener);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
如果
convertView!=空
。您应该阅读另一个使用viewHolder的示例。这次请仔细检查。如果convertView!=空
。您应该阅读另一个使用viewHolder的示例。这次要仔细检查
//Get the current location object
JSONObject r = (JSONObject) getItem(position);
//Inflate the view
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.ringtone_bit, null);
}
ImageView play = ( ImageView ) convertView.findViewById(R.id.play);
ImageView download = ( ImageView ) convertView.findViewById(R.id.download);
ImageView ratebutton = ( ImageView ) convertView.findViewById(R.id.ratebutton);
TextView rtitle = (TextView) convertView.findViewById(R.id.rtitle);
TextView size = (TextView) convertView.findViewById(R.id.size);
TextView downloads = (TextView) convertView.findViewById(R.id.downloads);
TextView voters = (TextView) convertView.findViewById(R.id.voters);
TextView personname = (TextView) convertView.findViewById(R.id.personname);
TextView date = (TextView) convertView.findViewById(R.id.date);
RatingBar ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall);
//setdate
try {
Date date_g = new Date(r.getLong("timestamp")*1000);
date.setText( date_g.toLocaleString() ) ;
} catch (JSONException e2) {}
//set person name
try {
String client_name = ( r.getString("personname").equals( "null" ) == true ) ? "ghost" : r.getString("personname");
personname.setText(client_name);
} catch (JSONException e2) {}
//set total votars and vote avarage
try {
float z = (float) r.getInt("rate");
voters.setText(" ( "+ r.getLong("voters") +" ) / " + z);
} catch (JSONException e2) {}
//set rating bar
try {
float z = (float) r.getInt("rate");
ratingsmall.setRating(z);
} catch (JSONException e2) {}
//set ringtone Name as defualt device language
try {
String name = ( lang.equals( "English" ) == true ) ? r.getString("en_name") : r.getString("ar_name");
rtitle.setText(name);
} catch (JSONException e2) {}
//ringtone file size
try {
size.setText(r.getString("size"));
} catch (JSONException e2) {}
//set downloads
try {
downloads.setText(String.valueOf( r.getLong("downloads") ));
} catch (JSONException e2) {}
//set ringtone ID toneid
try {
holder.tonid = r.getLong("toneid");
download.setTag(r.getLong("toneid"));
ratebutton.setTag(r.getLong("toneid"));
} catch (JSONException e1) {}
//set download stram url to play icon
try {
play.setTag(r.getString("stream_url"));
} catch (JSONException e) {}
//add play listener test Ringtone before download it
play.setOnClickListener(onClickListener);
return convertView;
视图持有者很奇怪,通常会引起混乱,我建议这样实现getView()
视图持有者很奇怪,通常会引起混乱,这就是我建议实现的
getView()
我想您误解了视图持有者的用途。ViewHolder不是用来保存值,而是用来保存视图,这样就不必再次充气。即使从标记获取视图,也需要设置数据
以下是正确使用视图保持架的方法:
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.ringtone_bit, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.play = ( ImageView ) convertView.findViewById(R.id.play);
holder.download = ( ImageView ) convertView.findViewById(R.id.download);
holder.ratebutton = ( ImageView ) convertView.findViewById(R.id.ratebutton);
holder.rtitle = (TextView) convertView.findViewById(R.id.rtitle);
holder.size = (TextView) convertView.findViewById(R.id.size);
holder.downloads = (TextView) convertView.findViewById(R.id.downloads);
holder.voters = (TextView) convertView.findViewById(R.id.voters);
holder.personname = (TextView) convertView.findViewById(R.id.personname);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Fill the data
我想你误解了ViewHolder的用途。ViewHolder不是用来保存值,而是用来保存视图,这样就不必再次充气。即使从标记获取视图,也需要设置数据 以下是正确使用视图保持架的方法:
if(convertView == null)
{
convertView = mInflater.inflate(R.layout.ringtone_bit, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.play = ( ImageView ) convertView.findViewById(R.id.play);
holder.download = ( ImageView ) convertView.findViewById(R.id.download);
holder.ratebutton = ( ImageView ) convertView.findViewById(R.id.ratebutton);
holder.rtitle = (TextView) convertView.findViewById(R.id.rtitle);
holder.size = (TextView) convertView.findViewById(R.id.size);
holder.downloads = (TextView) convertView.findViewById(R.id.downloads);
holder.voters = (TextView) convertView.findViewById(R.id.voters);
holder.personname = (TextView) convertView.findViewById(R.id.personname);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Fill the data
您没有处理
convertView!=空
大小写正确。您还必须填写所有的值。这两种情况之间的唯一区别应该是布局膨胀。您不是在处理convertView!=空
大小写正确。您还必须填写所有的值。这两种情况之间的唯一区别应该是布局膨胀。请您提供任何有用的演示链接,我不知道从哪里开始。您是对的,但您的答案与其说是真实答案,不如说是评论。@m0skit0:我不认为答案需要包含确切的解决方案。我更喜欢给人们指出正确的方向,这样他们就能自己解决问题。我理解你的观点,我完全同意,但yuor answer仍然缺乏关于他应该如何修复代码的信息:“你应该阅读另一个示例”不是答案。你怎么知道另一个例子也不会错?你能给我提供一些有用的演示链接吗?我不知道从哪里开始。你是对的,但你的答案与其说是真实答案,不如说是评论。@m0skit0:我认为答案不需要包含确切的解。我更喜欢给人们指出正确的方向,这样他们就能自己解决问题。我理解你的观点,我完全同意,但yuor answer仍然缺乏关于他应该如何修复代码的信息:“你应该阅读另一个示例”不是答案。你怎么知道另一个例子也不会错呢?谢谢兄弟,上帝保佑你它对我有用。对不起,android开发的目标更新了,再次感谢。这仍然是错误的。您仍然在使用findViewById而不是ViewHolder。我只是展示了一个更直接的示例,说明了它是如何工作的,从这里他可以实现ViewHolder,如果他想做得更好,但这让他可以更清楚地看到它是如何工作的,视图持有者可能会让不了解其工作原理的新开发人员感到困惑。谢谢你,兄弟,上帝保佑你,它对我有用。对不起,对于android开发,这仍然是错误的。你仍然在使用findViewById而不是ViewHolder。我只是展示了一个更直接的例子,说明了它是如何工作的,从这里他可以实现ViewHolder,如果他想做得更好,但这让他可以更清楚地看到它是如何工作的,ViewHolder可能会让不了解其工作原理的新开发人员感到困惑。