如何防止SimpleAdapter崩溃?

时间:2011-07-21 12:07:57

标签: java android

我正在尝试在main.java中填充ListView ...我有这个:

...
    ListViewMain=(ListView)findViewById(R.id.lv1);

    popVidList pvl = new popVidList();
    pvl.populateVideoList(ListViewMain);
...

并在popVidList.java中:

package rob.youtube.com;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class popVidList extends Activity {

    private ArrayList <HashMap<String, Object>> vidArray;
    private static final String ListVidText1 = "Mythbusters";
    private static final String ListVidText2 = "by Jamie";
    private static final String ListVidText3 = "3 month ago";
    private static final String IMGKEY = "iconfromraw";


public void populateVideoList( ListView ListViewMain )
{

    vidArray = new ArrayList<HashMap<String,Object>>();
    HashMap<String, Object> hm;

    hm = new HashMap<String, Object>();
    hm.put(ListVidText1, "Mythbusters");
    hm.put(ListVidText2, "by Jamie | 132.123 views");
    hm.put(ListVidText3, "3 month ago | +328 / - 14");
    hm.put(IMGKEY, R.drawable.vid_mb);

    vidArray.add(hm);

    SimpleAdapter adapter = new SimpleAdapter(this, vidArray, R.layout.listview_video, new String[]{ListVidText1,ListVidText2,ListVidText3,IMGKEY}, new int[]{R.id.listvid_text1, R.id.listvid_text2, R.id.listvid_text3, R.id.listvid_img});   
    /// CRASHES HERE. maybe 'this' should be replaced by something?

    ListViewMain.setAdapter(adapter);
    ListViewMain.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

}

}

然而在发布时它会崩溃。在将其移出到单独的文件之前,此方法在main.java中运行良好。 我想这可能与这一行有关:

SimpleAdapter adapter = new SimpleAdapter(this, vidArray, R.layout.listview_video, new String[]{ListVidText1,ListVidText2,ListVidText3,IMGKEY}, new int[]{R.id.listvid_text1, R.id.listvid_text2, R.id.listvid_text3, R.id.listvid_img});

也许我应该用某些东西取代“这个”?...

0 个答案:

没有答案