Android 在mysql中从json请求创建自定义ListView

Android 在mysql中从json请求创建自定义ListView,android,mysql,json,listview,Android,Mysql,Json,Listview,嘿,伙计们,我正在尝试在我的应用程序中创建一个自定义ListView,通过json请求从mysql数据库加载数据。我用了 android.R.id.simple_list_item_1 但现在我已经创建了自己的自定义xml文件,其中包含两个文本视图,分别表示名称和价格。 我的代码是 public void ListDrwaer() { List<Map<String, String>> stocksList = new ArrayList<Map&

嘿,伙计们,我正在尝试在我的应用程序中创建一个自定义ListView,通过json请求从mysql数据库加载数据。我用了

android.R.id.simple_list_item_1
但现在我已经创建了自己的自定义xml文件,其中包含两个文本视图,分别表示名称和价格。 我的代码是

public void ListDrwaer() {
        List<Map<String, String>> stocksList = new ArrayList<Map<String, String>>();

        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
            TextView tv1 = (TextView)findViewById(R.id.textView1);
            TextView tv2 = (TextView)findViewById(R.id.textView2);

            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = jsonChildNode.optString("name");
                String price = jsonChildNode.optString("price");
                stocksList.add(createStockList(name, price));
            }


        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }
        String[] from = { "name", "price" };
        int[] to = { R.id.textView1, R.id.textView2 };

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, stocksList,
                R.layout.list_item,
                from, to);
        listView.setAdapter(simpleAdapter);
    }

    private HashMap<String, String> createStockList(String name, String price) {
        HashMap<String, String> stockNameNo = new HashMap<String, String>();
        stockNameNo.put("name", name);
        stockNameNo.put("price", price);
        return stockNameNo;
    }
public void ListDrwaer(){
List stocksList=new ArrayList();
试一试{
JSONObject jsonResponse=新的JSONObject(jsonResult);
JSONArray jsonMainNode=jsonResponse.optJSONArray(“metoxes”);
TextView tv1=(TextView)findViewById(R.id.textView1);
TextView tv2=(TextView)findViewById(R.id.textView2);
for(int i=0;i
我想在textview 1中显示股票名称,在textview 2中显示价格
欢迎任何帮助。提前感谢

你应该这样试试

String[] from = { "name", "price" };
int[] to = { R.id.textView1, R.id.textView2 };

SimpleAdapter adapter = new SimpleAdapter(this, list,
    R.layout.list_item, from, to);
setListAdapter(adapter);

有关更多信息,请参见

,但您建议使用android布局简单列表项,我想使用我自己的列表项。如何设置textview1的名称和textview 2的编号?在中的字符串[]中,我必须有“metoxes”,因为这是来自数据库的json数组。否则我会得到一个空的listview。不需要它,您必须传递hashmap类型的arraylist,就是这样。您可以遵循我指定的示例,直到我得到一个空的列表视图。如果我像这样做,字符串[]from={“metoxes”,“price”};int[]to={R.id.textView1,R.id.textView2};我在textview2中得到了价格,但只得到了价格。使用“metoxes”,我得到了一个空列表。为什么要在for循环中将文本设置为TextView