0

没有日志错误,但我无法为距离评级和类型设置视图,当涉及一组视图时,我是否需要将其放入集合中并显示。

  • 我可以设置第一个数组的名称和第二个数组的时间
  • 但我无法显示第一个数组的其他视图

项目.java

public class Item{
    private String Name;
    private String Time;
    private String Distance;
    private String Rating;
    private String Type;



    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getTime() {
        return Time;
    }
    public void setTime(String time) {
        Time = time;
    }

    public String getDistance() {
        return Distance;
    }
    public void setDistance(String distance) {
        Distance = distance;
    }

    public String getRating() {
        return Rating;
    }
    public void setRating(String rating) {
        Rating = rating;
    }

    public String getType() {
        return Type;
    }
    public void setType(String type) {
        Type = type;
    }


}

我的适配器.java

 public class MyAdapter extends ArrayAdapter<Item> {

        private List<Item> items;

        public MyAdapter(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, time, distance, rating, type;


            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);
            distance = (TextView) v.findViewById(R.id.RestaurantDistanceID);
            rating = (TextView) v.findViewById(R.id.RestaurantRatingID);
            type = (TextView) v.findViewById(R.id.RestaurantTypeID);

            tt.setText(items.get(position).getName());
            time.setText(items.get(position).getTime());
            distance.setText(items.get(position).getDistance());
            rating.setText(items.get(position).getRating());
            type.setText(items.get(position).getType());

            return v;
        }
    }

MainActivity.java

public class MainActivity extends Activity {

    // url to make request
    private static String url = "http://54.218.73.244:7002/";
    String item;
    private HashMap<Integer, String> TimeMap = new HashMap<Integer, String>();
    ListView yourListView;
    List<Item> yourData = new ArrayList<Item>();
    MyAdapter customAdapter;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        yourListView = (ListView) findViewById(R.id.listViewID);
        // 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() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(MainActivity.this, "",
                    "Please Wait", true, false);

        }

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

            String _response = null;
            String _response1 = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                httpclient.getParams().setParameter(
                        CoreProtocolPNames.PROTOCOL_VERSION,
                        HttpVersion.HTTP_1_1);
                HttpGet request = new HttpGet(url);
                HttpResponse response = httpclient.execute(request);
                HttpEntity resEntity = response.getEntity();
                _response = EntityUtils.toString(resEntity);
                JSONObject jsonObject = new JSONObject(_response);
                JSONArray first_array = jsonObject.getJSONArray("restaurants");
                JSONArray second_array = jsonObject
                        .getJSONArray("RestaurantTimings");
                for (int i = 0; i < first_array.length(); i++) {
                    JSONObject c = second_array.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 = first_array.getJSONObject(i);

                    String NAME = c.getString("restaurantNAME");

                    String TYPE = c.getString("restaurantTYPE");
                    String DISTANCE= c.getString("restaurantDISTANCE");
                    String RATING= c.getString("restaurantRATING");

                    item.setName(NAME);

                    yourData.add(item);
                }

                HttpClient httpclient1 = new DefaultHttpClient();
                httpclient.getParams().setParameter(
                        CoreProtocolPNames.PROTOCOL_VERSION,
                        HttpVersion.HTTP_1_1);


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException 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();

            customAdapter = new MyAdapter(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) {
                    item = yourData.get(position).getName();

                    // String sendingurl="url1?param1=value1";

                    Intent i = new Intent(MainActivity.this,
                            RestaurantDesc.class);
                    i.putExtra("REST", item.toString());
                    // i.putExtra("key", yourData.get(position).getUrl());
                    //i.putExtra("CC_RES", item.toString());
                    startActivity(i);

                }
            });
        }

    }

}

有关如何解决此错误的任何想法。

4

1 回答 1

0

尝试这样

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      final ViewHolder holder;

        if (convertView == null ) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.itemlistrow, null);

         holder.tt = (TextView)convertView .findViewById(R.id.RestaurantNameID);
         holder.time = (TextView)convertView .findViewById(R.id.RestaurantTimeID);
         holder.distance = (TextView)convertView .findViewById(R.id.RestaurantDistanceID);
         holder.rating = (TextView)convertView .findViewById(R.id.RestaurantRatingID);
         holder.type = (TextView)convertView .findViewById(R.id.RestaurantTypeID);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }   

        holder.tt.setText(items.get(position).getName());
        holder.time.setText(items.get(position).getTime());
        holder.distance.setText(items.get(position).getDistance());
        holder.rating.setText(items.get(position).getRating());
        holder.type.setText(items.get(position).getType());

        return convertView;

    }

    private static class ViewHolder {
        TextView  tt;
        TextView time;
        TextView distance;
        TextView rating;
         TextView type;

    }
}
于 2013-09-20T12:32:24.673 回答