0

我是初学者。我想在 asyncTask 中显示一个列表视图,所以我有这段代码,但我不知道下面这行是怎么错的:

ListAdapter adapter =new SimpleAdapter(this, result,android.R.layout.simple_list_item_1, new String[] {"planet"}, new int[] {android.R.id.text1});

你能让我修好吗?

我的代码:

 public class MainActivity extends Activity {
    List<Map<String, String>> planetsList;
    ListAdapter simpleAdpt;
    ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView) this.findViewById(R.id.listView1);
        planetsList = new ArrayList<Map<String,String>>();

        initList();
        simpleAdpt = new SimpleAdapter(this, planetsList, android.R.layout.simple_list_item_1, new String[] {"planet"}, new int[] {android.R.id.text1});

        lv.setAdapter(simpleAdpt);
    }


     private void initList() {
            // We populate the planets into planetslist

            planetsList.add(createPlanet("planet", "Mercury"));
            planetsList.add(createPlanet("planet", "Venus"));
            planetsList.add(createPlanet("planet", "Mars"));
            planetsList.add(createPlanet("planet", "Jupiter"));
            planetsList.add(createPlanet("planet", "Saturn"));
            planetsList.add(createPlanet("planet", "Uranus"));
            planetsList.add(createPlanet("planet", "Neptune"));

    }

     private HashMap<String, String> createPlanet(String key, String name) {
            HashMap<String, String> planet = new HashMap<String, String>();
            planet.put(key, name);

            return planet;
    }

    @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;
    }

}
4

1 回答 1

0

我猜您正在使用 AsyncTask 中的“this”引用您的 Activity。试试这个:

ListAdapter adapter =new SimpleAdapter(MainActivity.this, result,android.R.layout.simple_list_item_1, new String[] {"planet"}, new int[] {android.R.id.text1});
于 2013-09-02T05:03:27.820 回答