自定义列表适配器想要在加载json

时间:2015-12-17 14:46:08

标签: android android-arrayadapter custom-adapter android-json

我正在使用自定义适配器来查看json数据。如果我再次重新加载,数据会再次重复。我试过adapter.clear()。如何刷新自定义适配器列表视图。我的代码是。

从json开始,值设置为适配器

for(int i=0;i<data.length();i++){
                    String j_hotel_id=data.getJSONObject(i).getString("hotel_id");
                    String j_hotel_name=data.getJSONObject(i).getString("hotel_name");
                    String j_hotel_place=data.getJSONObject(i).getString("city");
                    String j_hotel_date=data.getJSONObject(i).getString("created_date");

                    hotel_id1.add(j_hotel_id);  //Arraylist                 
                    hotel_name1.add(j_hotel_name);  //Arraylist                         
                    hotel_place1.add(j_hotel_place);    //Arraylist                         
                    h_created_date1.add(j_hotel_date);  //Arraylist 
                    //count++;
                }
list_hotel.setAdapter(new HotelCustomAdapter(this, hotel_id1,hotel_name1,hotel_place1)); // passing to custom Adapter and list_hotel is the ListView id

自定义适配器

        package ridio.helixtech;

    import java.util.ArrayList;
    import java.util.List;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
    import ridio.helixtech.RateCustomAdapter.Holder;

    public class HotelCustomAdapter extends BaseAdapter{ 

        List<String> h_id=new ArrayList<String>();
        List<String> h_name=new ArrayList<String>();
        List<String> h_place=new ArrayList<String>();
        List<String> h_date=new ArrayList<String>();
        Context context;
          private static LayoutInflater inflater=null;


        public HotelCustomAdapter(HotelMaster hotelAdd1, List<String> hotel_id1, List<String> hotel_name1,
                List<String> hotel_place1, List<String> h_created_date1) {
            h_id=hotel_id1;
            h_name=hotel_name1;
            h_place=hotel_place1;
            h_date=h_created_date1;
            context=hotelAdd1;
             inflater = ( LayoutInflater )context.
                     getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        public HotelCustomAdapter(HotelMaster hotelAdd1, List<String> hotel_id1, List<String> hotel_name1,
                List<String> hotel_place1) {
            // TODO Auto-generated constructor stub
            h_id=hotel_id1;
            h_name=hotel_name1;
            h_place=hotel_place1;
            context=hotelAdd1;
             inflater = ( LayoutInflater )context.
                     getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return h_id.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public class Holder
        {
            TextView tv;
            TextView tv1;
            TextView tv2;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final Object[] id_obj=h_id.toArray();
            Object[] name_obj=h_name.toArray();
            Object[] place_obj=h_place.toArray();
            Holder holder=new Holder();
            View rowView;       
                 rowView = inflater.inflate(R.layout.hotel_custom_listview, null);
                 holder.tv=(TextView) rowView.findViewById(R.id.h_id);
                 holder.tv1=(TextView) rowView.findViewById(R.id.h_name);
                 holder.tv2=(TextView) rowView.findViewById(R.id.h_place);
             holder.tv.setText(""+id_obj[position]);
             holder.tv1.setText((CharSequence) name_obj[position]);
             holder.tv2.setText((CharSequence) place_obj[position]);
             rowView.setOnClickListener(new OnClickListener() {            
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "You Clicked "+id_obj[position], Toast.LENGTH_LONG).show();
                }
            });   
            return rowView;
        }

    }

如何从此代码刷新适配器。

我再次尝试传递计数值,从最后一次计数中获取json数组,而不是从数据库加载。如果我使用全局值到for循环,第一次它没有加载json第二次完美加载值。除了在循环中,我没有改变任何值。我完全糊涂了。我使用的代码如下。

 int count=0; //Global value
 for(int i=count;i<data.length();count++){
                    String j_hotel_id=data.getJSONObject(count).getString("hotel_id");
                    String j_hotel_name=data.getJSONObject(count).getString("hotel_name");
                    String j_hotel_place=data.getJSONObject(count).getString("city");
                    String j_hotel_date=data.getJSONObject(count).getString("created_date");

                    hotel_id1.add(j_hotel_id);  //Arraylist                 
                    hotel_name1.add(j_hotel_name);  //Arraylist                         
                    hotel_place1.add(j_hotel_place);    //Arraylist                         
                    h_created_date1.add(j_hotel_date);  //Arraylist 
                    //count++;
                }

1 个答案:

答案 0 :(得分:1)

更改数据后,请尝试adapter.invalidate()adapter.notifyDataSetChanged()

在你的情况下: list_hotel.getAdapter().invalidate();list_hotel.getAdapter().notifyDataSetChanged();