本文介绍了列表视图不填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在这里帮助。我试图从填充JSON响应列表视图。响应被张贴,由于某种原因,它没有显示在列表中?任何帮助此外,它是停在列表视图,只是没有看到任何东西。

 扩展ListActivity {

//进度对话框
私人ProgressDialog pDialog;

//创建JSON解析器对象
JSONParser jParser =新JSONParser();

ArrayList的< HashMap的<字符串,字符串>> ideasList;

// URL获得所有产品列表
私有静态字符串url_all_products =htt​​p://theideavault.no-ip.org//android_connect/get_all_products.php;

// JSON节点名称
私有静态最后弦乐TAG_SUCCESS =成功;
私有静态最后弦乐TAG_PRODUCTS =点子;
私有静态最后弦乐TAG_IID =IID;
私有静态最后弦乐TAG_NAME =理念;

//产品JSONArray
JSONArray想法= NULL;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.all_idea);

    //哈希映射为的ListView
    ideasList =新的ArrayList< HashMap的<字符串,字符串>>();

    //加载产品在后台线程
    新LoadAllProducts()执行();

    //获取列表视图
    ListView的LV = getListView();

    //在选课单品
    //启动编辑产品屏幕
    lv.setOnItemClickListener(新OnItemClickListener(){

        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,
                INT位置,长的id){
            //从选择列表项获得价值
            字符串IID =((TextView中)view.findViewById(R.id.iid))。gettext的()
                    的ToString();

            //开始的新意图
            意图=新的意图(getApplicationContext()
                    EditIdeaActivity.class);
            //发送PID到下一个活动
            in.putExtra(TAG_IID,IID);

            //开始新的活动,期待一些回响应
            startActivityForResult(在,100);
        }
    });

}

//从编辑产品的活动响应
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,因此code,数据);
    //如果结果是code 100
    如果(结果code == 100){
        //如果结果是code 100接收
        //意味着用户编辑/删除产品
        //重新载入该画面
        意向意图= getIntent();
        完();
        startActivity(意向);
    }

}

/ **
 *背景异步任务,以通过HTTP请求负载的所有产品
 * * /
类LoadAllProducts扩展的AsyncTask<字符串,字符串,字符串> {

    / **
     *在启动后台线程显示进度对话框
     * * /
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        pDialog =新ProgressDialog(AllIdeasActivity.this);
        pDialog.setMessage(从数据库Reteiving思想Wooooooo ......。);
        pDialog.setIndeterminate(假);
        pDialog.setCancelable(假);
        pDialog.show();
    }

    / **
     *得到所有产品的网址
     * * /
    保护字符串doInBackground(字符串参数... args){
        //大厦参数
        名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
        //从URL获取JSON字符串
        JSONObject的JSON = jParser.makeHtt prequest(url_all_products,GET,则params);

        //检查你的日志猫JSON效应初探
        Log.d(所有的想法:json.toString());

        尝试 {
            //检查成功TAG
            INT成功= json.getInt(TAG_SUCCESS);

            如果(成功== 1){
                发现//产品
                //获取产品阵列
                创意= json.getJSONArray(TAG_PRODUCTS);

                //遍历所有产品
                的for(int i = 0; I< ideas.length();我++){
                    JSONObject的C = ideas.getJSONObject(我);

                    //保存在变量中的每个JSON项目
                    字符串IID = c.getString(TAG_IID);
                    字符串的想法= c.getString(TAG_NAME);

                    //创建新的HashMap
                    HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();

                    //添加每个子节点HashMap中的key =>值
                    map.put(TAG_IID,IID);
                    map.put(TAG_NAME,想法);

                    //添加HashList到ArrayList中
                    ideasList.add(图)
                }
            } 其他 {
                //没有发现产品
                //启动添加新产品活动
                意图I =新的意图(getApplicationContext()
                        NewIdeaActivity.class);
                //关闭所有previous活动
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(ⅰ);
            }
        }赶上(JSONException E){
            e.printStackTrace();
        }

        返回null;
    }

    / **
     *在完成后台任务之后关闭该进度对话框
     * ** /
    保护无效onPostExecute(字符串file_url){
        让所有的产品之后//关闭该对话框
        pDialog.dismiss();
        //从后台线程更新UI
        runOnUiThread(新的Runnable(){
            公共无效的run(){
                / **
                 *更新解析JSON数据到ListView控件
                 * * /
                ListAdapter适配器=新SimpleAdapter(
                        AllIdeasActivity.this,ideasList,
                        R.layout.list_idea,新的String [] {TAG_IID,
                                TAG_NAME},
                        新的INT [] {R.id.iid,R.id.idea});
                //更新列表视图
                setListAdapter(适配器);
            }
        });

    }

}}
 

解决方案

伟大的错误,你所做的是同时初始化TAG_PRODUCTS变量。

错误:

私有静态最后弦乐TAG_PRODUCTS =点子;

修正值:

私有静态最后弦乐TAG_PRODUCTS =产品;

顺便说一句,你取的想法或产品?因为你的反应是产品和你的code介绍点子:)

Can anyone help here. I'm trying to populate a listview from a json response. the response is posting, for some reason it isn't showing the list? anything helpsAlso it is stopping on the list view, just not seeing anything.

extends ListActivity {

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> ideasList;

// url to get all products list
private static String url_all_products = "http://theideavault.no-ip.org//android_connect/get_all_products.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "ideas";
private static final String TAG_IID = "iid";
private static final String TAG_NAME = "idea";

// products JSONArray
JSONArray ideas = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_idea);

    // Hashmap for ListView
    ideasList = new ArrayList<HashMap<String, String>>();

    // Loading products in Background Thread
    new LoadAllProducts().execute();

    // Get listview
    ListView lv = getListView();

    // on seleting single product
    // launching Edit Product Screen
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String iid = ((TextView) view.findViewById(R.id.iid)).getText()
                    .toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    EditIdeaActivity.class);
            // sending pid to next activity
            in.putExtra(TAG_IID, iid);

            // starting new activity and expecting some response back
            startActivityForResult(in, 100);
        }
    });

}

// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) {
        // if result code 100 is received
        // means user edited/deleted product
        // reload this screen again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllProducts extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AllIdeasActivity.this);
        pDialog.setMessage("Reteiving Ideas from database. Wooooooo...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("All Ideas: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                ideas = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < ideas.length(); i++) {
                    JSONObject c = ideas.getJSONObject(i);

                    // Storing each json item in variable
                    String iid = c.getString(TAG_IID);
                    String idea = c.getString(TAG_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_IID, iid);
                    map.put(TAG_NAME, idea);

                    // adding HashList to ArrayList
                    ideasList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),
                        NewIdeaActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AllIdeasActivity.this, ideasList,
                        R.layout.list_idea, new String[] { TAG_IID,
                                TAG_NAME},
                        new int[] { R.id.iid, R.id.idea });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

}}
解决方案

The great mistake you did is while initialize TAG_PRODUCTS variable.

Mistake:

private static final String TAG_PRODUCTS = "ideas";

Correct value:

private static final String TAG_PRODUCTS = "products";

BTW, are you fetching ideas or products? because your response is of products and your code describes Idea :)

这篇关于列表视图不填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:27