Java Listview多重重复操作?
我的Listview中有重复操作问题我在互联网上做了很多研究都失败了我的问题是当我点击项目编号1时,它展开了,当我向下滚动分页时,附加了更多项目,我发现项目编号10也展开了,另一个例子是,如果我切换第2项,则第2项和第12项将同时切换 如何解决此重复问题 这是listview类Java Listview多重重复操作?,java,android,Java,Android,我的Listview中有重复操作问题我在互联网上做了很多研究都失败了我的问题是当我点击项目编号1时,它展开了,当我向下滚动分页时,附加了更多项目,我发现项目编号10也展开了,另一个例子是,如果我切换第2项,则第2项和第12项将同时切换 如何解决此重复问题 这是listview类 public class Ringtones_Listview extends ArrayAdapter<Object> { int resource; String response;
public class Ringtones_Listview extends ArrayAdapter<Object> {
int resource;
String response;
Context context;
private LayoutInflater mInflater;
MediaPlayer mp = null;
ImageView play_clicked;
View vv = null;
String lang = Locale.getDefault().getDisplayLanguage();
ArrayList ob;
Integer clickedposition;
public Ringtones_Listview(Context context, int resource, ArrayList objects) {
super(context, resource,objects);
this.context = context;
this.ob = objects;
this.resource = resource;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return ob.size();
}
public JSONObject getItem(JSONObject position) {
return position;
}
public long getItemId(int position) {
return position;
}
static class ViewHolder {
ImageView play ;
ImageView download ;
TextView rtitle;
TextView size;
TextView downloads;
TextView personname;
TextView date;
RatingBar ratingsmall;
ImageView ratebutton;
long tonid;
TextView voters;
LinearLayout more;
}
@SuppressWarnings("deprecation")
public View getView(final 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();
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);
holder.more = ( LinearLayout ) convertView.findViewById(R.id.more);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
//hide More information
holder.more.setVisibility(View.GONE);
try {
Date date_g = new Date(r.getLong("timestamp") * 1000);
holder.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");
holder.personname.setText(client_name);
} catch (JSONException e2) {}
//set total votars and vote avarage
try {
float z = (float) r.getInt("rate");
holder.voters.setText(" ( "+ r.getLong("voters") +" ) / " + z);
} catch (JSONException e2) {}
//set rating bar
try {
float z = (float) r.getInt("rate");
holder.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");
holder.rtitle.setText(name);
} catch (JSONException e2) {}
//ringtone file size
try {
holder.size.setText(r.getString("size"));
} catch (JSONException e2) {}
//set downloads
try {
holder.downloads.setText(String.valueOf( r.getLong("downloads") ));
} catch (JSONException e2) {}
//set ringtone ID toneid
try {
holder.tonid = r.getLong("toneid");
holder.download.setTag(r.getLong("toneid"));
holder.ratebutton.setTag(r.getLong("toneid"));
holder.play.setId((int) r.getLong("toneid"));
convertView.setId((int) r.getLong("toneid"));
} catch (JSONException e1) {}
//set download stram url to play icon
try {
holder.play.setTag(r.getString("stream_url"));
} catch (JSONException e) {}
//add play listener test Ringtone before download it
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
clickedposition=position;
boolean playit = false;
v.setMinimumHeight(200);
//stop Media player dont play any thing
if ( mp != null )
{
mp.stop();
mp.release();
mp = null;
}
//check for last clicked item
if ( vv != null )
{
//set last played item to play icon
vv.setBackgroundResource(R.drawable.play);
//check if last played is not smae clicked item
if( v.getTag().toString().equals( vv.getTag().toString() ) == false )
{
playit = true;
}else
//its same item dont play stop here
{
vv = null;
}
}else
//nothing played yet play it
{
playit = true;
}
if ( playit )
{
vv = v;
v.setBackgroundResource(R.drawable.preparing_player);
AnimationDrawable frameAnimation = (AnimationDrawable) v.getBackground();
frameAnimation.start();
String stramUrl = String.valueOf(v.getTag());
try {
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(stramUrl);
mp.prepareAsync();
} catch (Exception e) {}
//preparing straming
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mps) {
v.setBackgroundResource(R.drawable.played);
mps.start();
}
});
mp.setOnCompletionListener( new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
v.setBackgroundResource(R.drawable.play);
}
});
}
}
});
return convertView;
}
}
公共类铃声\u Listview扩展了ArrayAdapter{
智力资源;
字符串响应;
语境;
私人停车场;
MediaPlayer mp=null;
单击图像视图播放;
视图vv=null;
字符串lang=Locale.getDefault().getDisplayLanguage();
ArrayList ob;
整数点击位置;
公共铃声\u列表视图(上下文上下文、int资源、ArrayList对象){
超级(上下文、资源、对象);
this.context=上下文;
this.ob=对象;
这个资源=资源;
mInflater=LayoutInflater.from(上下文);
}
public int getCount(){
返回ob.size();
}
公共JSONObject getItem(JSONObject位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
静态类视窗夹{
图像视图播放;
ImageView下载;
TextView rtitle;
文本视图大小;
文本视图下载;
TextView人名;
文本查看日期;
比率杆比率小;
图像查看按钮;
长音调;
TextView选民;
线性布局更多;
}
@抑制警告(“弃用”)
公共视图getView(最终整数位置、视图转换视图、视图组父视图)
{
视窗座;
//获取当前位置对象
JSONObject r=(JSONObject)getItem(位置);
//夸大观点
if(convertView==null)
{
convertView=mInflater.充气(R.layout.ringtone_位,空);
holder=新的ViewHolder();
convertView.setTag(支架);
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.voctors=(TextView)convertView.findViewById(R.id.voctors);
holder.personname=(TextView)convertView.findViewById(R.id.personname);
holder.date=(TextView)convertView.findViewById(R.id.date);
holder.ratingsmall=(RatingBar)convertView.findViewById(R.id.ratingsmall);
holder.more=(LinearLayout)convertView.findViewById(R.id.more);
}
其他的
{
holder=(ViewHolder)convertView.getTag();
}
//隐藏更多信息
holder.more.setVisibility(View.GONE);
试一试{
日期=新日期(r.getLong(“时间戳”)*1000);
holder.date.setText(date_g.tolocalString());
}catch(JSONException e2){}
//设置人名
试一试{
String client_name=(r.getString(“personname”).equals(“null”)==true)?“ghost”:r.getString(“personname”);
holder.personname.setText(客户名称);
}catch(JSONException e2){}
//设定总投票数和投票率
试一试{
浮动z=(浮动)r.getInt(“利率”);
holder.voctors.setText(“(“+r.getLong”(“选民”)+”/“+z”);
}catch(JSONException e2){}
//设置分级栏
试一试{
浮动z=(浮动)r.getInt(“利率”);
支架。额定值小。设定值(z);
}catch(JSONException e2){}
//将铃声名称设置为解除设备语言
试一试{
字符串名称=(lang.equals(“English”)==true)?r.getString(“en_name”):r.getString(“ar_name”);
holder.rtitle.setText(名称);
}catch(JSONException e2){}
//铃声文件大小
试一试{
holder.size.setText(r.getString(“size”));
}catch(JSONException e2){}
//设置下载
试一试{
holder.downloads.setText(String.valueOf(r.getLong)(“下载”));
}catch(JSONException e2){}
//设置铃声ID音调ID
试一试{
holder.tonid=r.getLong(“toneid”);
holder.download.setTag(r.getLong(“toneid”);
holder.ratebutton.setTag(r.getLong(“toneid”);
holder.play.setId((int)r.getLong(“toneid”);
setId((int)r.getLong(“toneid”);
}catch(JSONException e1){}
//将下载stram url设置为播放图标
试一试{
holder.play.setTag(r.getString(“stream_url”);
}catch(JSONException e){}
//下载前添加播放侦听器测试铃声
holder.play.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(最终视图v){
单击位置=位置;
布尔值playit=false;
v、 设置最小高度(200);
//停止媒体播放器不要播放任何东西
如果(mp!=null)
{
mp.stop();
mp.release();
mp=null;
}
//检查上次单击的
clickedposition=position;
if(clickedposition!=null)
{
v.setBackgroundResource(R.drawable.played);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
......
holder.play.setBackgroundResource(R.drawable.play);
if(checkposition!=-1)
{
holder.play.setBackgroundResource(R.drawable.played);
}
return convertView;
}