android ListView is empty Logcat shows nothing - android

i am trying to display json data in listview but listview is empty logcat shows no error and app also don't get crashed i am unable to find the error here is the method that adds json data to the adapter
public void setTextToTextView(JSONArray jsonArray) {
contactAdapter = new ContactAdapter(this,R.layout.row_layout);
listView.setAdapter(contactAdapter);
try {
int count = 0;
String stop;
while(count < jsonArray.length())
{
JSONObject JO = jsonArray.getJSONObject(count);
stop = JO.getString("stop");
Contacts contacts = new Contacts(stop);
contactAdapter.add(contacts);
count++;
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
here is the Adapter class
public class ContactAdapter extends ArrayAdapter{
List list = new ArrayList();
public ContactAdapter(Context context, int resources){
super(context,resources);
}
public void add(Contacts object)
{
super.add(object);
list.add(object);
}
#Override
public int getCount() {return list.size();}
#Override
public Object getItem(int position) {return list.get(position);}
#Override
public View getView(int position,View convertView,ViewGroup parent)
{
View row;
row = convertView;
ContactHolder contactHolder;
if(row == null)
{
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
contactHolder = new ContactHolder();
contactHolder.tx_stop = (TextView)row.findViewById(R.id.tx_stop);
row.setTag(contactHolder);
}
else
{
contactHolder = (ContactHolder)row.getTag();
}
Contacts contacts = (Contacts)this.getItem(position);
contactHolder.tx_stop.setText(contacts.getStop());
return row;
}
static class ContactHolder
{
TextView tx_stop;
}
}

Call
contactAdapter.notifyDataSetChanged();
after your while loop.

You can create ArrayList<Contacts> contactsArray = new ArrayList<Contacts>(); then push all objects in it after that to set your adapter with passed array. Your method should look like:
public void setTextToTextView(JSONArray jsonArray) {
ArrayList<Contacts> contactsArray = new ArrayList<Contacts>();
try {
int count = 0;
String stop;
while(count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
stop = JO.getString("stop");
Contacts contacts = new Contacts(stop);
contactsArray.add(contacts);
count++;
}
contactAdapter = new ContactAdapter(this,R.layout.row_layout, contactsArray);
listView.setAdapter(contactAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
In your adapter do the following:
ArrayList<Contacts> list = new ArrayList<Contacts>();
public ContactAdapter(Context context, int resources, ArrayList<Contacts> list){
super(context,resources);
this.list = list;
}
That's all. Hope to help!

Related

List view not refreshing when item is deleted [duplicate]

This question already has answers here:
ListView not "refreshing" after item is deleted from it
(6 answers)
Closed 5 years ago.
I Have a list view where i have loaded it with json array data. I have minus button to delete row from list. Here the problem is I am able to delete the item but list view is not getting refreshed. The list view gets refreshed once the application is closed or when I go back and navigate to that activity again.
Below is the custom adapter class code:
public class AddPassengerAdapater extends BaseAdapter implements ListAdapter {
private final JSONArray jsonArray;
ArrayList<String> data;
Context context;
JSONObject json_data;
LayoutInflater inflater;
public AddPassengerAdapater(Context context, JSONArray jsonArray,ArrayList<String> data) {
this.context = context;
this.jsonArray = jsonArray;
this.data = data;
String details = SessionManager.getPreferences(context,"splitfare_consumer");
data = new ArrayList<String>();
JSONArray jsonarray = null;
try {
jsonarray = new JSONArray(details);
for (int i=0; i < jsonarray.length() ; i++){
json_data = jsonarray.getJSONObject(i);
data.add(String.valueOf(json_data));
}
} catch (JSONException e) {
e.printStackTrace();
}
inflater = LayoutInflater.from(this.context);
}
#Override public int getCount() {
if(null==jsonArray)
return 0;
else
return jsonArray.length();
}
#Override public JSONObject getItem(int position) {
if(null==jsonArray) return null;
else
return jsonArray.optJSONObject(position);
}
#Override public long getItemId(int position) {
JSONObject jsonObject = getItem(position);
return jsonObject.optLong("id");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.addpassenger_row_layout, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
}
else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
json_data = getItem(position);
try {
String n = json_data.getString("name");
mViewHolder.name.setText(n);
String nu = json_data.getString("number");
mViewHolder.number.setText(nu);
} catch (JSONException e) {
e.printStackTrace();
}
mViewHolder.deletelist.setOnClickListener(new View.OnClickListener() {
JSONArray delete_jsonarray;
JSONObject jsonObject;
#Override
public void onClick(View v) {
String phonenumber = mViewHolder.number.getText().toString();
String jsonsetpreference_data = SessionManager.getPreferences(context,"splitfare_consumer");
Log.d("jsonsetpreference_data:::::::","" +jsonsetpreference_data);
try {
delete_jsonarray= new JSONArray(jsonsetpreference_data);
for (int i =0; i<= delete_jsonarray.length()-1 ; i++){
jsonObject = delete_jsonarray.getJSONObject(i);
String array_phonenumber = jsonObject.getString("number");
if (phonenumber.equals(array_phonenumber)){
delete_jsonarray.remove(i);
}
}
SessionManager.setPreferences(context,"splitfare_consumer",delete_jsonarray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return convertView;
}
private class MyViewHolder {
TextView name, number;
ImageView deletelist;
public MyViewHolder(View item) {
name = (TextView) item.findViewById(R.id.consumername);
number = (TextView) item.findViewById(R.id.consumer_number);
deletelist = (ImageView) item.findViewById(R.id.dltconsumer);
}
}
}
Listview activity code:
public class AddPassengerList extends AppCompatActivity {
#BindView(R.id.listview)
ListView listview;
#BindView(R.id.btn_addmore)
CustomFontButton btn_addmore;
#BindView(R.id.donebtn)
CustomFontButton donebtn;
public static final String TAG = "AddPassengerList";
Activity activity=this;
Context context = this;
JSONArray js;
AddPassengerAdapater adapter;
LayoutInflater inflater;
String name ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_passenger_list);
ButterKnife.bind(this);
/*Status bar color*/
Window window = activity.getWindow();
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
window.setStatusBarColor(ContextCompat.getColor(context,R.color.colorAccent));
String data = SessionManager.getPreferences(context,"splitfare_consumer");
Log.d(TAG,""+data);
ArrayList<String> details= new ArrayList<String>();
try {
js = new JSONArray(data);
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new AddPassengerAdapater(context,js,details);
listview.setAdapter(adapter);
btn_addmore.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
btn_addmore.setBackgroundResource(R.drawable.addmorepassengerpressed);
break;
case MotionEvent.ACTION_UP:
btn_addmore.setBackgroundResource(R.drawable.addmorepassenger);
Intent i = new Intent(AddPassengerList.this,AddPassenger.class);
startActivity(i);
break;
}
return false;
}
});
}
}
Recall your adapter again after delete item...
mViewHolder.deletelist.setOnClickListener(new View.OnClickListener() {
JSONArray delete_jsonarray;
JSONObject jsonObject;
#Override
public void onClick(View v) {
String phonenumber = mViewHolder.number.getText().toString();
String jsonsetpreference_data = SessionManager.getPreferences(context,"splitfare_consumer");
Log.d("jsonsetpreference_data:::::::","" +jsonsetpreference_data);
try {
delete_jsonarray= new JSONArray(jsonsetpreference_data);
for (int i =0; i<= delete_jsonarray.length()-1 ; i++){
jsonObject = delete_jsonarray.getJSONObject(i);
String array_phonenumber = jsonObject.getString("number");
if (phonenumber.equals(array_phonenumber)){
delete_jsonarray.remove(i);
}
}
SessionManager.setPreferences(context,"splitfare_consumer",delete_jsonarray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
notifyDataSetChanged();
}
});

Received data is not inflate to gridview

I have data model class and i download photos by json. And then i want to add theese photos in gridview. But i could not.
This download operation in onCreate(),
ProductHandler.setProducts(products);
final GridView gridView = (GridView) findViewById(R.id.gridView);
AsyncTaskListener asyncTaskListener = new AsyncTaskListener() {
#Override
public void onCompleted(String result) {
ArrayList<Product> products = ProductHandler.getProducts();
String json = "";
if(!Util.isNullOrEmpty(result)){
json = Html.fromHtml(result).toString();
}
if(!Util.isNullOrEmpty(json) && Util.isJSONValid(json)){
Log.e("json", json);
try {
JSONArray jsonArray = new JSONArray(json);
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
Product product = new Product();
product.setPhoto(jsonObject.getString("photo"));
products.add(product);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.e("after", String.valueOf(products.size()));
ProductAdapter adapter = new ProductAdapter(context, products);
gridView.setAdapter(adapter);
}
};
RequestParams requestParams = new RequestParams();
requestParams.add("api_key", Preferences.getSavedapiKey(context));
TaskParams taskParams = new TaskParams(Constant.API_PATH + "presentation.php", ClientMethod.POST, requestParams);
AsyncHttpClient asyncHttpClient = new AsyncHttpClient(asyncTaskListener);
asyncHttpClient.execute(taskParams);
ProductHandler is simple;
public class ProductHandler {
private static ArrayList<Product> products;
public static synchronized ArrayList<Product> getProducts(){
return products;
}
public static synchronized void setProducts(ArrayList<Product> products){
ProductHandler.products = products;
}
}
ProductAdapter;
public class ProductAdapter extends BaseAdapter {
private Context context;
private ArrayList<Product> products;
public ProductAdapter(Context context, ArrayList<Product> products){
this.context = context;
this.products = products;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView == null){
imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else{
imageView = (ImageView) convertView;
}
Util.Image.stringToImageView(imageView, products.get(position).getPhoto());
return imageView;
}
}
And products are define global in activity;
ArrayList<Product> products = new ArrayList<>();
So when it running, gridview is empty. I cannot understand the problem. Thank you.
Please pass your count in adapter(ProductAdapter)
#Override
public int getCount() {
return products.size();// you are passing 0 here
}

Android ArrayList Adapter doesn't show the ListView

I have an ArrayList to represent in a ListView by an Adapter:
private ArrayList <Contact> listContacts = new ArrayList <Contact> ();
This is the (simple) Contact Class:
public class Contact {
String pic;
String name;
String surname1;
String surname2;
String phonenumber;
}
And this is the Adapter Class:
public class ContactsAdapter extends ArrayAdapter<Contact> {
private static class ViewHolder {
ImageView pic;
TextView name;
TextView surname1;
TextView surname2;
TextView phonenumber;
}
Context context;
public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
super(context, textViewResourceId, items);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(R.layout.layout_contact, parent, false);
holder = new ViewHolder();
holder.pic= (ImageView) convertView.findViewById(R.id.pic);
holder.name= (TextView) convertView.findViewById(name);
holder.surname1= (TextView) convertView.findViewById(R.id.surname1);
holder.surname2= (TextView) convertView.findViewById(R.id.surname2);
holder.phonenumber= (TextView) convertView.findViewById(R.id.phonenumber);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Contact item = getItem(position);
if (item!= null) {
int idImage = context.getResources().getIdentifier(item.pic, "drawable", context.getPackageName());
holder.pic.setImageResource(idImage);
holder.name.setText(item.name);
holder.surname1.setText(item.surname1);
holder.surname2.setText(item.surname2);
holder.phonenumber.setText(item.phonenumber);
}
return convertView;
}
}
The MainActivity is like this:
public class MainActivity extends ListActivity {
private ArrayList <Contact> listContacts = new ArrayList <Contact> ();
ContactsAdapter contactsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean result = loadListFromFiles();
if (result) {
loadContacts();
}
}
private void loadContacts() {
contactsAdapter = new ContactsAdapter(getApplicationContext(), R.layout.activity_main, listContacts);
setListAdapter(contactsAdapter);
}
The loadListFromFiles code:
private boolean loadListFiles(){
boolean result = false;
Context context = getApplicationContext();
File path = context.getFilesDir();
File[] files = path.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
result = loadFile(files[i].getName());
}
return resultado;
}
private boolean loadFile(String fileName) {
String[] arrayContact = new String[4];
try
{
BufferedReader fin =
new BufferedReader(
new InputStreamReader(
openFileInput(fileName)));
int i = 0;
String line= "";
while ((line= fin.readLine()) != null) {
arrayContact[i] = linea;
i++;
}
fin.close();
Contact contact = new Contact();
contact.pic = arrayContact[3];
contact.name= arrayContact[0];
contact.surname1 = arrayContact[1];
contact.surname2 = arrayContact[2];
contact.phonenumber= arrayContact[3];
listContacts.add(contact);
return true;
}
catch (Exception ex)
{
Log.e("Files", "Error to read file by memory");
return false;
}
}
The ArrayList is right, but the layout don't show anything.
Override getCount() and getItem() methods to return count and contact item.
#Override
public int getCount() {
if(items == null)
return 0;
return items.size();
}
#Override
public Contact getItem(int i) {
return items.get(i);
}
Also, create one variable items in Adapter class and assign it to parameter passed to adapter constructor.
ArrayList<Contact> items;
public ContactsAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) {
super(context, textViewResourceId, items);
this.context = context;
this.items = items;
}

How to set Custom listview According to the itemname alphabetically?

This is the method where i am getting the data from the server and show in the listview
I want to show the list alphabetically accordingly to the i am setting the name with the help of model class. pojoRestaurant.setRestroName(jsonObject.getString("RestaurantName"));
I want to show the list accordingly RestaurantName.can anyone tell me how can i do this ??
public void requestRestaurantSearchByFilter_list() {
mProgressDialog.show();
StringRequest restrolistrequestfilter = new StringRequest(Request.Method.POST, GlobalData.SEARCHBYFILTERURL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
mProgressDialog.dismiss();
mPaymentMethodList.clear();
mDataList.clear();
mAllList.clear();
mAdapter.notifyDataSetChanged();
Log.e("responcefilterlist....", response);
JSONObject jObject = new JSONObject(response);
if (jObject.getString("status").equals("1")) {
JSONArray jsonArray = jObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
PojoRestaurant pojoRestaurant = new PojoRestaurant();
pojoRestaurant.setRestroName(jsonObject.getString("RestaurantName"));
pojoRestaurant.setDeliveryTime(jsonObject.optString("DeliveryTime"));
pojoRestaurant.setPaymentOption(jsonObject.getString("PaymentOptions"));
pojoRestaurant.setMinimumOrder(jsonObject.getString("MinimumOrder"));
if (jsonObject.getString("ImageUrl").equals("") || jsonObject.getString("ImageUrl") == "null") {
} else {
pojoRestaurant.setRestroImage(jsonObject.getString("ImageUrl"));
}
id = jsonObject.getString("ID");
mPaymentMethodList.add(jsonObject.getString("PaymentOptions"));
getpaymentId.add(id);
getListViewId.add(id);
if (jsonObject.getString("OpenStatus").equals("true")) {
pojoRestaurant.setOpenClose("Open");
} else {
pojoRestaurant.setOpenClose("Close");
}
mAllList.add(pojoRestaurant);
mDataList.add(pojoRestaurant);
//mDeliveryList.add(jsonObject.getString("DeliveryTime"));
// mCuisineTypeList.add(jsonObject.getString("RestaurantName"));
}
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("error", "" + volleyError.getMessage());
if (volleyError.getMessage() == null)
requestRestaurantSearchByFilter_list();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
String foodid = getArguments().getString("FOODID");
String areaid = getArguments().getString("AREAID");
Log.e("foodid", "" + foodid);
Log.e("areaid", "" + areaid);
params.put("DeliveryAreaID", areaid);
params.put("ProvideOffers", "0");
params.put("NewRestaurant", "0");
params.put("PaymentMethod", "0");
params.put("OpenRestaurant", "0");
params.put("FoodID", foodid);
return params;
}
};
RequestQueue restrolistqueuefilter = Volley.newRequestQueue(getContext());
restrolistrequestfilter.setShouldCache(false);
restrolistqueuefilter.add(restrolistrequestfilter);
}
This is my Adpater Class
public class RestroListBaseAdapter extends BaseAdapter {
private ArrayList<PojoRestaurant> mList;
private Context mContext;
private ViewHolder viewHolder;
private ArrayList<PojoRestaurant> suggestion = new ArrayList<>();
public RestroListBaseAdapter(Context mContext, ArrayList<PojoRestaurant> mList) {
this.mContext = mContext;
this.mList = mList;
}
#Override
public int getCount() {
if (mList != null) {
return mList.size();
}
return 0;
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int lastPosition = -1;
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.restaurant_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.RestroName = (TextView) convertView.findViewById(R.id.tv_restro_name);
viewHolder.OpenClose = (TextView) convertView.findViewById(R.id.tvOpenClose);
viewHolder.MinimumOrder = (TextView) convertView.findViewById(R.id.tv_minimun_order);
viewHolder.DeliveryTime = (TextView) convertView.findViewById(R.id.tv_delivery_time);
viewHolder.PaymentOption = (TextView) convertView.findViewById(R.id.tv_Payment);
viewHolder.RestroImage = (ImageView) convertView.findViewById(R.id.img_restro_list);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
convertView.startAnimation(animation);
lastPosition = position;
convertView.setTag(viewHolder);
PojoRestaurant pojoRestaurant = (PojoRestaurant) getItem(position);
viewHolder.RestroName.setText(pojoRestaurant.getRestroName());
viewHolder.DeliveryTime.setText(pojoRestaurant.getDeliveryTime());
viewHolder.MinimumOrder.setText(pojoRestaurant.getMinimumOrder());
viewHolder.OpenClose.setText(pojoRestaurant.getOpenClose());
viewHolder.PaymentOption.setText(pojoRestaurant.getPaymentOption());
Picasso.with(mContext).load(pojoRestaurant.getRestroImage()).into(viewHolder.RestroImage);
return convertView;
}
private class ViewHolder {
private TextView RestroName, OpenClose, MinimumOrder, DeliveryTime, PaymentOption;
private ImageView RestroImage;
}
}
You just need to sort that list returned from server by alphabetically order and then add this list directly to your listview.
For example you have a model class:
public class Restaurant {
String restaurantName;
int restaurantStar;
}
You should implements Comparator interface for this class:
public class Restaurant implements Comparator<Restaurant> {
String restaurantName;
int restaurantStar;
#Override
public int compare(Schedule s1, Schedule s2) {
// because class String has already implemented Comparator. just get that
return s1.restaurantName.compare(s2.restaurantName);
}
}
Finally you can sort this list of restaurants:
// get somewhere on network
List<Restaurant> restaurants;
Collection.sort(restaurants);
Finally you can assign directly this list to listview. For example:
adapter.setData(restaurants);
// refresh listview so listview will display new sorted data
adapter.notifyDataSetChanged();
Here is an another example that using Comparator:
comparator tutorial
First, you need to sort the list(depending upon your logic i.e. alphabetically) which you have already used to set to the adapter and then notify the list adapter.
You can use your own comparator for sorting the list.
Sample code for Comparator:
Comparator comparator = new Comparator() {
public int compare(PojoRestaurant restro1, PojoRestaurant restro2) {
String restroName1 = restro1.getRestroName.toUpperCase();
String restroName2 = restro2.getRestroName.toUpperCase();
//ascending order
return restroName1.compareTo(restroName2);
//descending order
//return restroName2.compareTo(restroName1);
}
};
Then, you can use following code to sort the list
Collections.sort(yourList, collection)

Android Listview Adds The Same Item

I'm developing an Android Application and I'm using Android ListView. I'm getting datas from the web service and fill them into the Arraylist. It's size is 37. Then i try to fill the listview with Arraylist but it always get the same element(last one). Below you can see the code part:
private void ctv_listele(String res){
ArrayList<CTV> ctvList = new ArrayList<CTV>();
try {
JSONObject jsonObject = new JSONObject(res);
JSONArray jsonArray = jsonObject.getJSONArray("tarife");
int max = jsonArray.length();
for(int i = 0; i < max; i++) {
JSONObject tmp = jsonArray.getJSONObject(i);
ctv.setYear(tmp.getString("Year"));
ctv.setYearlyCost(tmp.getString("YearlyCost"));
ctv.setMonthlyCost(tmp.getString("MonthlyCost"));
ctv.setGroup(tmp.getString("Group"));
ctv.setDegree(tmp.getString("Degree"));
Log.e("Added",tmp.getString("YearlyCost"));
ctvList.add(ctv);
}
Log.e("End",String.valueOf(ctvList.size()));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView productList = (ListView) findViewById(R.id.listView_ctv);
MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.ctv_row, ctvList);
productList.setAdapter(adapter);
}
public class MyCustomAdapter extends ArrayAdapter<CTV>{
private Activity context;
private ArrayList<CTV> liste;
private LayoutInflater layoutInflater;
private AdapterSatir adaSatir;
public MyCustomAdapter(Activity context, int ctvRow, ArrayList<CTV> objects) {
super(context, R.layout.ctv_row, objects);
this.context=context;
this.liste=objects;
Log.e("liste",String.valueOf(liste.size()));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view_satir=convertView;
if(view_satir==null) {
adaSatir=new AdapterSatir();
layoutInflater=context.getLayoutInflater();
view_satir=layoutInflater.inflate(R.layout.cevretemizliktarifeleri_row, null,true);
adaSatir.textView1=(TextView) view_satir.findViewById(R.id.textView_ctvlist1);
adaSatir.textView2=(TextView) view_satir.findViewById(R.id.textView_ctvlist2);
adaSatir.textView3=(TextView) view_satir.findViewById(R.id.textView_ctvlist3);
adaSatir.textView4=(TextView) view_satir.findViewById(R.id.textView_ctvlist4);
view_satir.setTag(adaSatir);
} else {
adaSatir = (AdapterSatir) view_satir.getTag();
}
adaSatir.textView1.setText(liste.get(position).getDegree());
adaSatir.textView2.setText(liste.get(position).getGroup());
adaSatir.textView3.setText(liste.get(position).getMonthlyCost());
adaSatir.textView4.setText(liste.get(position).getYearlyCost());
return view_satir;
}
private class AdapterSatir
{
public TextView textView1;
public TextView textView2;
public TextView textView3;
public TextView textView4;
}
}
for(int i = 0; i < max; i++) {
JSONObject tmp = jsonArray.getJSONObject(i);
ctv.setYear(tmp.getString("Year"));
ctv.setYearlyCost(tmp.getString("YearlyCost"));
ctv.setMonthlyCost(tmp.getString("MonthlyCost"));
ctv.setGroup(tmp.getString("Group"));
ctv.setDegree(tmp.getString("Degree"));
Log.e("Added",tmp.getString("YearlyCost"));
ctvList.add(ctv);
}
you forget to init your cvt object at every iteration. So you add always the same reference to the list.
for(int i = 0; i < max; i++) {
JSONObject tmp = jsonArray.getJSONObject(i);
CTV cvt = new CVT();
ctv.setYear(tmp.getString("Year"));
ctv.setYearlyCost(tmp.getString("YearlyCost"));
ctv.setMonthlyCost(tmp.getString("MonthlyCost"));
ctv.setGroup(tmp.getString("Group"));
ctv.setDegree(tmp.getString("Degree"));
Log.e("Added",tmp.getString("YearlyCost"));
ctvList.add(ctv);
}
First Add new data to ctvList
then call adapter.notifyDataSetChanged();
Then new data will reflect in the List view please check this

Categories

Resources