为什么适配器Listview循环视图方法

时间:2015-07-23 03:27:24

标签: android listview android-listview

我有一个MainActivity来读取SDCard中的音乐文件并将歌曲的名称存储在Arraylist中。我想显示歌曲的名称,但问题是适配器循环中的View方法更多。看到我的日志,它返回21次,它是如何做到的,以及我如何解决它。请帮帮我!!!

public class MainActivity extends ActionBarActivity {

ListView listView;
mAdapter adapter;
String []items;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView)findViewById(R.id.listView);
    final ArrayList<File> mySongs = finadSongs(Environment.getExternalStorageDirectory());
    items = new String[mySongs.size()];
    for(int i=0 ; i<mySongs.size();i++){
        items[i]=mySongs.get(i).getName().toString().replace(".mp3","").replace(".wav","");
    }
    adapter = new mAdapter(this,R.layout.check_layout,items);
    listView.setAdapter(adapter);
}
public ArrayList<File> finadSongs(File root){
    ArrayList<File> al = new ArrayList<File>();
    File[] files = root.listFiles();
    for(File singleFile : files){
        if(singleFile.isDirectory()&& !singleFile.isHidden()){
            al.addAll( finadSongs(singleFile));
        }else{
            if(singleFile.getName().endsWith(".mp3")|| singleFile.getName().endsWith(".wav")){
                al.add(singleFile);
            }
        }
    }
    return al;
}

//Adapter
public class mAdapter extends ArrayAdapter<String> {

Activity context;
int layoutId ;
String[]name;
String id;
int i = 0;
public mAdapter(Activity context, int layoutId, String [] arr) {
    super(context, layoutId, arr);
    this.context = context;
    this.layoutId = layoutId;
    this.name = arr;


}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater= context.getLayoutInflater();
    convertView=inflater.inflate(layoutId, null);
    TextView textView = (TextView)convertView.findViewById(R.id.textView);
    CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
    Log.w("", "lengh" + i++);
    for(String songName: name){
        textView.setText(songName);
        Log.w("","name"+songName);
    }

    return convertView;
}

//Log
07-22 23:14:12.353  14691-14691/com.example.minhvu.music1 W/﹕ lengh19
07-22 23:14:12.353  14691-14691/com.example.minhvu.music1 W/﹕ nameUntil You - Shayne Ward
07-22 23:14:12.353  14691-14691/com.example.minhvu.music1 W/﹕ nameYou be long with me Taylor wilf - Taylor wilf
07-22 23:14:12.353  14691-14691/com.example.minhvu.music1 W/﹕ nameYou be long with me Taylor wilf - Taylor wilf-1
07-22 23:14:12.357  14691-14691/com.example.minhvu.music1 W/﹕ lengh20
07-22 23:14:12.357  14691-14691/com.example.minhvu.music1 W/﹕ nameUntil You - Shayne Ward
07-22 23:14:12.357  14691-14691/com.example.minhvu.music1 W/﹕ nameYou be long with me Taylor wilf - Taylor wilf
07-22 23:14:12.357  14691-14691/com.example.minhvu.music1 W/﹕ nameYou be long with me Taylor wilf - Taylor wilf-1

1 个答案:

答案 0 :(得分:0)

您的getView()方法每次调用时都会迭代整个名称数组。我怀疑这是你的意图。通常,您可以在指定的position处获取该项目,并将其仅绑定到您的视图,例如

textView.setText(name[position]);