我在 android 项目中有简单的 ԼistView 。 ListView 项目有两种类型。1. Song Type 2. Folder Type ListView items model 'SongModel',有TYPE字段SongModel
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 项目视图中,我有 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" />
在适配器类中,我检查,如果 TYPE=1,我在项目视图中绘制歌曲照片。如果 TYPE=2,我绘制文件夹照片,我 ImageButton Visibility 设置 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;
}
}
在活动代码中
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;
}
}
启动应用程序时,一切正常,所有项目都绘制为true,如果文件夹没有ImageButton,如果歌曲有ImageButton。
滚动后listView Items View元素混杂,文件夹item添加ImageButton,而歌曲Items没有ImageButton。
请帮助我,以纠正错误。