Ayub baba Ayub baba - 5 months ago 31
JSON Question

For loop in android create gaps between the items

I am passing the json in the navigation slider where its getting gap 2 spaces gap between the items. I am not able understand why its happening like that. I had tried several method but I am not successful.

Navigation.Java

private void navmenu() {
String menuurl = "http://www.souqalkhaleejia.com/webapis/categories.php";
Log.i("menuurl", menuurl);
JsonObjectRequest menuobj = new JsonObjectRequest(Request.Method.POST, menuurl, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray menu1=response.getJSONArray("menu1");
for (int i=0;i<menu1.length();i++){
JSONObject first=menu1.getJSONObject(i);
Data navgadata = new Data();
navgadata.setCatergory(first.getString("category"));
datalts.add(navgadata);
JSONArray item=first.getJSONArray("items");
for (int j=0;j<item.length();j++){
Data secdata=new Data();
JSONObject second=item.getJSONObject(j);
secdata.setId(second.getString("id"));
secdata.setSubcatergory(second.getString("title"));
datalts.add(secdata);
}


}

} catch (JSONException e) {
e.printStackTrace();
}
listnadapter.notifyDataSetChanged();
}

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("res", "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(menuobj);
}


This is my XML file navigation.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:textColor="#fff"
android:id="@+id/categories" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:textColor="#fff"
android:id="@+id/subcategories" />




Adapter.java

public class Navgation_adapter extends BaseAdapter {
private Context context;
private List<Data> catdata;
private LayoutInflater inflater;
public Navgation_adapter(Context context, List<Data> catdata){
this.context=context;
this.catdata=catdata;
}
@Override
public int getCount() {
return catdata.size();
}

@Override
public Object getItem(int i) {
return catdata.get(i);
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

ViewHolder holder;
if (inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null){
holder=new ViewHolder();
view=inflater.inflate(R.layout.navigation_list,viewGroup,false);
holder.categories= (TextView) view.findViewById(R.id.categories);
holder.subcategories= (TextView) view.findViewById(R.id.subcategories);
view.setTag(holder);

}else {
holder= (ViewHolder) view.getTag();
}
Data ndata=catdata.get(i);
holder.categories.setText(ndata.getCatergory());
holder.subcategories.setText(ndata.getSubcatergory());

return view;
}
static class ViewHolder{
TextView categories,subcategories;
}


Here the image how I am getting view

enter image description here

Answer

You're using the same Data class for both categories and subcategories. When the JSON data is a category, you set the category String on the Data object, but not the subcategory String. It's vice versa when the data is a subcategory. The Strings you don't set remain null, and setting null text on a TextView leaves it blank. So, when a list item represents a category, the top TextView displays the category name, but the bottom TextView is blank. The next item will be a subcategory, and for the same reasons, the top TextView will be blank. The two adjacent blank TextViews leave a large gap between the category and its first subcategory. The blank category TextViews in the following subcategory items leave smaller gaps between those items.

If you want to stick with as much of your current setup as possible, one solution is to toggle the TextViews' visibility properties depending on the type of data, which we can determine by checking for a null Data object member.

public View getView(int i, View view, ViewGroup viewGroup) {
    ...     
    Data ndata = catdata.get(i);

    holder.subcategories.setText(ndata.getSubcatergory());
    holder.categories.setText(ndata.getCatergory());

    if(ndata.getCatergory() == null) {
        holder.categories.setVisibility(View.GONE);
        holder.subcategories.setVisibility(View.VISIBLE);       
    }
    else {
        holder.categories.setVisibility(View.VISIBLE);
        holder.subcategories.setVisibility(View.GONE);      
    }

    return view;
}