首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >片段中的ListAdapter

片段中的ListAdapter
EN

Stack Overflow用户
提问于 2013-01-05 14:07:45
回答 2查看 909关注 0票数 0

我正在尝试创建一些条目的列表,从而在php服务器上产生一个查询。我使用一个片段,它调用一个名为AsyncTaskDescarregarLlistatEntrades类。这个类基本上解析了来自php执行的json数据,然后我想填充一个列表,但是由于DescarregarLlistatEntrades没有扩展ListActivity,所以我不能使用方法setListAdapter

下面是代码:

碎片:

代码语言:javascript
复制
public class MyFragmentB extends Fragment {
private ArrayList<HashMap<String, String>> llistatEntrades;
Context cont=getActivity(); //Recupero el contexte per poder pasar-lo.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.llistat_fotos, container, false);

    //hashmap per al llistat de les entrades que hi han al servidor
    llistatEntrades=new ArrayList<HashMap<String, String>>();

    //descarrego totes les imatges en segon plà i omplo el view
    new DescarregarLlistatEntrades(cont, myFragmentView, llistatEntrades).execute();

    //Recupero el el layout de la view per poder fer coses am ella a posteriori.
    ListView lv = (ListView)myFragmentView.findViewById(android.R.id.list);

    return myFragmentView;
}

DescarregarLlistatEntrades:

代码语言:javascript
复制
public class DescarregarLlistatEntrades extends AsyncTask<String, String, String>{
private Context mContext;
private View rootView;
private ArrayList<HashMap<String, String>> llistatEntrades;
private String linkFoto, dataUpload, usuariUpload, comentariUpload;
private ProgressDialog pDialog;
private List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONParser jsonParser = new JSONParser();

private String url_get_entrades = "http://192.168.1.33/testing/recuperaentrades.php"; //passantli per get el id de la setmana 

public DescarregarLlistatEntrades(Context context, View rootView, ArrayList<HashMap<String, String>> llistatEntrades){
    this.mContext=context;
    this.rootView=rootView;
    this.llistatEntrades=llistatEntrades;
}

@Override
protected void onPreExecute() {
    [...]
}

@Override
protected String doInBackground(String... arg0) { 
    params.add(new BasicNameValuePair("setmana", "1"));
    JSONObject json = jsonParser.makeHttpRequest(url_get_entrades, "GET", params);
    JSONArray entradesObj;
    try {
        int success = json.getInt("success");
        //mirem si hi ha success
        if (success==1){
            entradesObj = json.getJSONArray("entrades"); //recupero totes les entrades
            for (int i=0; i<entradesObj.length();i++){
                JSONObject entrada = entradesObj.getJSONObject(i); //recupero cada objecte
                //guardo cada camp de cada entrada a variables:
                this.usuariUpload=entrada.getString("usuariUploader");
                this.linkFoto=entrada.getString("urlFoto");
                this.comentariUpload=entrada.getString("comentariUsuari");
                this.dataUpload=entrada.getString("dataUpload");
                //creo el hashmap dels valors anteriors
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("usuariUpload", this.usuariUpload);
                map.put("linkFoto", this.linkFoto);
                map.put("comentariUpload", this.comentariUpload);
                map.put("dataUpload", this.dataUpload);
                this.llistatEntrades.add(map);
            }
        } else {
            Log.d("debugging","No hi han entrades..");
        }

    } catch (JSONException e) {
        e.printStackTrace();
    } 

    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    this.pDialog.dismiss(); //amago el dialog
    // updating UI
    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(
            this.mContext, this.llistatEntrades,
            R.layout.entrada_llista, new String[] { "usuariUpload",
                    "linkFoto","comentariUpload", "dataUpload"},
            new int[] { R.id.userNameUploader, R.id.titolFoto, R.id.comment, R.id.hora });
    // updating listview
    setListAdapter(adapter); //THIS LINE!!

}

“setListAdapter(ListAdapter)方法未定义为DescarregarLlistatEntrades类型”

在使用片段时,我应该使用另一种方法吗?

请注意,我传递给AsyncTask类、上下文和充气器。也许我要递点别的东西?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-05 14:14:33

您应该将ListView的实例传递给异步任务,因此当它将列表适配器设置为ListView实例时。如果您使用普通的Fragment --您可以将ListView添加到布局中,在onCreateView方法中检索它并传递给异步任务。此外,您也可以使用ListFragment,这样您就可以通过在片段中使用getViewMethod来获得ListView。或者从rootView获取列表视图,如果它是片段布局的根。类似于:

代码语言:javascript
复制
((ListView) rootView.findViewById(R.id.list_view)).setAdapter(adapter);
票数 3
EN

Stack Overflow用户

发布于 2013-01-05 14:50:08

最终代码:这是AsyncTask类:

代码语言:javascript
复制
public DescarregarLlistatEntrades(Context context, View rootView, ArrayList<HashMap<String, String>> llistatEntrades, ListView lv){
    this.mContext=context;
    this.rootView=rootView;
    this.llistatEntrades=llistatEntrades;
    this.lv=lv;
}

和碎片

代码语言:javascript
复制
public class MyFragmentB extends Fragment {
private ArrayList<HashMap<String, String>> llistatEntrades;
Context cont;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    this.cont=getActivity(); //Recupero el contexte per poder pasar-lo.
    View myFragmentView = inflater.inflate(R.layout.llistat_fotos, container, false);

    //hashmap per al llistat de les entrades que hi han al servidor
    llistatEntrades=new ArrayList<HashMap<String, String>>();
    //Recupero el el layout de la view per poder fer coses am ella a posteriori.
    ListView lv = (ListView)myFragmentView.findViewById(android.R.id.list);

    //descarrego totes les imatges en segon plà i omplo el view
    new DescarregarLlistatEntrades(cont, myFragmentView, llistatEntrades, lv).execute();

    return myFragmentView;
}

像魅力一样工作!!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14172527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档