程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> sampleadapter-求大神幫忙解答下,學習安卓的過程中,關於SampleAdapter的問題

sampleadapter-求大神幫忙解答下,學習安卓的過程中,關於SampleAdapter的問題

編輯:編程解疑
求大神幫忙解答下,學習安卓的過程中,關於SampleAdapter的問題

package com.aa.cao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter simp_adapter;
private List>datalist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview.setAdapter(simp_adapter);
    simp_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
    datalist=new ArrayList<Map<String,Object>>();
}

private List<Map<String,Object>> getData(){
    for(int i=0;i<20;i++){
        Map<String,Object>map=new HashMap<String, Object>();
        map.put("pic", R.drawable.ic_launcher);
        map.put("text", "慕課網"+i);
        datalist.add(map);
    }
    return datalist;
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

不知道為什麼,一運行就報錯,實在找不出問題,求大神幫忙解答下,感謝!

最佳回答:


 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview.setAdapter(simp_adapter);
    simp_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
    datalist=new ArrayList<Map<String,Object>>();
}
  1. listview要先實例化,如listview = (ListView)findViewById(R.id.xxx),xxx是listview的id
  2. 同樣的道理simp_adapter也要在 listview.setAdapter之前實例化
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved