0

我正在尝试学习将数据从服务器填充到 Listview。

我是安卓新手

MainActivity.java

public class MainActivity extends Activity {

    // url to make request
    private static String url = "http://URL:7002/";
    private static String url1 = "http://URL:7002/XXX";

    //private HashMap<Integer, String> TimeMap = new HashMap<Integer, String>();

    List<Item> yourData = new ArrayList<Item>();
    List<Item> yourData1 = 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);

            // getting JSON string from URL
            JSONArray json1 = jParser.getJSONFromUrl(url1);

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


                    // Storing each json item in variable
                    int id = c.getInt("_id");
                    String TIME = c.getString("RestaurantTime");

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



            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();


            //TextView timedisplay=(TextView) findViewById(R.id.RestaurantTimeID);


            ListView yourListView = (ListView) findViewById(R.id.listViewID);
            ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
            yourListView.setAdapter(customAdapter);

// remaining code

ListAdapter.java

public class ListAdapter extends ArrayAdapter<Item> {

    private List<Item> items;

    public ListAdapter(Context context, int resource, List<Item> items) {
        super(context, resource, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        TextView tt = null;
        TextView time=null;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.itemlistrow, null);

            tt = (TextView) v.findViewById(R.id.RestaurantNameID);
            time = (TextView) v.findViewById(R.id.RestaurantTimeID);

        }

        Item p = items.get(position);
        if (p != null) {
            if (tt != null) {
                tt.setText(""+p.getName());
            }
            if (time != null) {
                time.setText(""+p.getTime());
            }

        }
        return v;
    }
}

项目.java

public class Item{
    private String Name;
    private String Time;

    public Item(String name){
        this.Name = name;
    }
    public String getName(){
        return Name;
    }

    public void Time(String time){
        this.Time = time;
    }

    public String getTime(){
        return Time;
    }
}
  • 我该如何解决这个问题
  • 我已经在上面发布了必修课
  • 使用列表填充数据是我在看

有任何想法吗,

谢谢。

4

1 回答 1

0

您将TIME设置为yourdata1并将NAME设置为yourdatayourdata1的对象仅包含 TIME 并且NAME 为 nullyourdata的对象仅包含 NAME 并且TIME 为 null

请在一个对象中设置 NAME 和 TIME并将其存储在一个 List中,该 List 将传递给 ListAdapter。

我希望你能理解我。

        @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);

            // getting JSON string from URL
            JSONArray json1 = jParser.getJSONFromUrl(url1);

            // assume json's length is the same as json1's
            try {
                for (int i = 0; i < json1.length(); i++) {
                    JSONObject c = json1.getJSONObject(i);
                    Item item = new Item();

                    // Storing each json item in variable
                    int id = c.getInt("_id");
                    String TIME = c.getString("RestaurantTime");
                    item.setTime(TIME);

                    c = json.getJSONObject(i);

                    // Storing each json item in variable
                    String NAME=c.getString("restaurantNAME");
                    item.setName(NAME);
                    yourData.add(item);
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;

        }

public class Item{
    private String Name;
    private String Time;
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getTime() {
        return Time;
    }
    public void setTime(String time) {
        Time = time;
    }
}
于 2013-08-17T04:32:45.050 回答