自定义列表适配器想要在加载时刷新 json
Custom List Adapter want to refresh when loads 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++;
}
更改数据后尝试 adapter.invalidate()
或 adapter.notifyDataSetChanged()
。
你的情况:
list_hotel.getAdapter().invalidate();
或 list_hotel.getAdapter().notifyDataSetChanged();
我正在使用自定义适配器查看 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++;
}
更改数据后尝试 adapter.invalidate()
或 adapter.notifyDataSetChanged()
。
你的情况:
list_hotel.getAdapter().invalidate();
或 list_hotel.getAdapter().notifyDataSetChanged();