从本地Json文件读取后的空列表

时间:2016-05-24 19:28:19

标签: android json listview

我想从列表中的本地jason文件填充一些消息,但它不会在列表中显示任何消息。有人有任何想法帮助我

本地json文件资产文件夹:

friends

解析jason文件并在listview中填充它的主要活动:

{
      "events": [
{
  "event": "Taste of summer"
},
{
  "event": " House Party at Millennium Park"
},
{
  "event": " Farmers Markets"
},
{
  "event": "Blues Festival Preview Events"
},
{
  "event": "Cultural Alliance - Heritage and Fashion: At a Global Crossroad?"
}
    ]
    }

列表视图布局:

public class ListtestActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listtest);



            // Reading json file from assets folder
            StringBuffer sb = new StringBuffer();
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(getAssets().open("listev.json")));
                String temp;
                while ((temp = br.readLine()) != null)
                    sb.append(temp);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if ((br != null)) {
                    try {
                        br.close(); // stop reading
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            try {
                JSONObject jsonObjMain = new JSONObject(sb.toString());
                JSONArray jsonArray = jsonObjMain.getJSONArray("events");

                ArrayList<String> messages = new ArrayList<String>();
                for (int i = 0; i < jsonArray.length(); i++) {
                    // Creating JSONObject from JSONArray
                    JSONObject object = jsonArray.getJSONObject(i);
                   // String message = object.getString("event");
                    String message = jsonArray.getString(i);
                 //I try both but not working!!!
                    messages.add(message);
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListtestActivity.this,
                        android.R.layout.simple_list_item_1, messages);
                ListView list = (ListView) findViewById(R.id.listView);
                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(ListtestActivity.this, "TEST List View", Toast.LENGTH_SHORT).show();

                    }
                });

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


    }

谢谢

1 个答案:

答案 0 :(得分:0)

你必须这样做:

MainActivity

public class MainActivity extends AppCompatActivity {

    ArrayList<String> arrayList=new ArrayList<String>();
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=(ListView)findViewById(R.id.live_list_view);

        String json="{\n" +
                "      \"events\": [\n" +
                "{\n" +
                "  \"event\": \"Taste of summer\"\n" +
                "},\n" +
                "{\n" +
                "  \"event\": \" House Party at Millennium Park\"\n" +
                "},\n" +
                "{\n" +
                "  \"event\": \" Farmers Markets\"\n" +
                "},\n" +
                "{\n" +
                "  \"event\": \"Blues Festival Preview Events\"\n" +
                "},\n" +
                "{\n" +
                "  \"event\": \"Cultural Alliance - Heritage and Fashion: At a Global Crossroad?\"\n" +
                "}\n" +
                "    ]\n" +
                "    }";



        try {
            JSONObject jsonRootObject = new JSONObject(json);

            //Get the instance of JSONArray that contains JSONObjects
            JSONArray jsonArray = jsonRootObject.optJSONArray("events");

            if(jsonArray.length()>0) {
                //Iterate the jsonArray and print the info of JSONObjects
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String data = jsonObject.getString("event");
                    arrayList.add(data);


                }
            }
            else
            {
                Toast.makeText(getApplicationContext(),"No Elements in JsonArray",Toast.LENGTH_SHORT).show();
            }
           // output.setText(data);
        } catch (JSONException e) {e.printStackTrace();}

        ArrayAdapter arrayAdapter=new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(arrayAdapter);

    }

}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.win7.jsonarray.MainActivity">

    <ListView
        android:id="@+id/live_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    </ListView>
</RelativeLayout>

这将有效。