0

你好,我正在做一个大学项目,我需要一些指导


我正在尝试在表格视图中显示 JSON 数据

JSON:: http://54.218.73.244:7000/

MainActivity.java

public class MainActivity extends Activity {

    // url to make request
    private static String url = "http://54.218.73.244:7000/";
    List<Item> yourData = new ArrayList<Item>();

    ProgressDialog progressDialog;

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

        //Instantiating ProgressDialog with onCreate method
        progressDialog=new ProgressDialog(MainActivity.this);
        new ParsingAsync().execute();

    }

    private class ParsingAsync extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);


        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            // Creating JSON Parser instance
            JSONObjParser jParser = new JSONObjParser();

            // getting JSON string from URL
            JSONArray json = jParser.getJSONFromUrl(url);

            try {
                for (int i = 0; i < json.length(); i++) {
                    JSONObject c = json.getJSONObject(i);

                    // Storing each json item in variable
                    String NAME=c.getString("restaurantNAME");

                    yourData.add(new Item(NAME));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            progressDialog.dismiss();
            ListView yourListView = (ListView) findViewById(R.id.listViewID);
            ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
            yourListView.setAdapter(customAdapter);
            yourListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // When clicked, show a toast with the TextView text
                    if(position == 0)
                    {
                        //code specific to first list item    
                        Intent myIntent = new Intent(MainActivity.this,CopperChimneyDesc.class);
                        startActivity(myIntent);
                    }else if(position == 1)
                    {
                        Intent myIntent = new Intent(MainActivity.this,AroyDesc.class);
                        startActivity(myIntent);                    
                    }

                }
            });
        }

    }

}

CopperChimney_desc_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5sp"
        android:text="BACK" />

</LinearLayout>

  • 虽然我已经能够实现将 JSON 数据填充到 textview 和 listview
  • 如何从上面的 URL 显示 JSON 到表格视图
4

1 回答 1

0

我认为你是用IOS编码的,所以你很困惑。在android中我们使用ListView,但ios端使用TableView。只需查看此链接以获取教程:http ://www.vogella.com/articles/AndroidListView/

于 2013-08-07T07:52:22.183 回答