gpt4 book ai didi

java - 滚动后 listView Items View 元素混合

转载 作者:行者123 更新时间:2023-11-29 21:34:42 25 4
gpt4 key购买 nike

我在 android 项目中有一个简单的 ǼistView .Of ListView Items 是两种类型。1.歌曲类型2.文件夹类型ListView 项目模型 'SongModel',有 TYPE 字段歌曲模型

public class SongModel {


public String title;
public int type;

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}

在 ListView 项目 View 中,我有 3 个元素:用于歌曲名称的 TextView,用于歌曲或文件夹照片的 ImageView,以及用于歌曲播放的 ImageButton。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/song_photo"
android:layout_width="119dp"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />



<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />


</LinearLayout>

<ImageButton
android:id="@+id/add_queue"
android:layout_width="wrap_content"
android:layout_height="542dp"
android:src="@drawable/ic_launcher" />

在 Adapter 类中,我检查,如果 TYPE=1 我在项目中查看绘画歌曲照片。如果 TYPE=2 我绘画文件夹照片,我将 ImageButton 可见性设置为 View.GONE。

package com.example.adapter;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class SongAdapter extends ArrayAdapter<SongModel> {
private ArrayList<SongModel> entries;
private Activity activity;

static class ViewHolder {

public ImageButton song_play;
public TextView song_name;
public ImageView song_photo;


}
public SongAdapter(Activity a, int textViewResourceId,ArrayList<SongModel> entries) {

super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.song_name = (TextView) rowView.findViewById(R.id.title);
viewHolder.song_photo = (TextView) rowView.findViewById(R.id.song_photo);
viewHolder.song_play = (ImageView) rowView .findViewById(R.id.song_play);
rowView.setTag(viewHolder);
}

ViewHolder holder = (ViewHolder) rowView.getTag();
SongModel song = entries.get(position);

holder.song_name.setText(song. getTitle());

if (song.getType()==2)
{

holder.song_play.setVisibility(View.Gone);
holder.song_photo.setImageResource(R.drawable.folder);

}
else
{
holder.song_photo.setImageResource(R.drawable.song);
}
rowView.setTag(getItemViewType(position));
return rowView;
}
}

在 Activity 代码中

 package com.example.adapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {
private ArrayList<SongModel> fetch;
private ListView MusicItemlistView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MusicItemlistView=(ListView)findViewById(R.id.listview);
fetch = new ArrayList<SongModel>();

try {
JSONObject jsonData=new JSONObject(datajson);
JSONArray dataArray=jsonData.getJSONArray("responseBody");
SongModel sg;
JSONObject js;
for(int i=0;i<dataArray.length();i++)
{
sg=new SongModel();
js=(JSONObject) dataArray.get(i);
sg.setAlbum(js.getString("artist"));
sg.setFileName(js.getString("fileName"));
sg.setType(Integer.parseInt(js.getString("type")));
sg.setDuration(Integer.parseInt(js.getString("duration")));
fetch.add(sg);
}
MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter (this,R.id.listview, fetch);
MusicItemlistView.setAdapter(adapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

启动应用程序时,工作正常,所有项目绘制真实,如果文件夹没有ImageButton,如果歌曲有ImageButton。

滚动后listView Items View元素混入,folder item添加ImageButton,而song Items没有ImageButton。

请帮助我,纠正错误。

最佳答案

试试这个:

if (song.getType()==2)
{

holder.song_play.setVisibility(View.Gone);
holder.song_photo.setImageResource(R.drawable.folder);

}
else
{
holder.song_play.setVisibility(View.VISISBLE);
holder.song_photo.setImageResource(R.drawable.song);
}

关于java - 滚动后 listView Items View 元素混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18671410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com