2015-11-16 24 views
1

我傳遞了導航滑塊中的JSON,其中它獲取的差距2項之間的空隙差距。我無法理解爲什麼會發生這種情況。我曾嘗試過幾種方法,但我不成功。在Android中的循環創建項目之間的差距

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); 
    } 

這是我的XML文件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; 
} 

在這裏,我怎麼得到視圖

enter image description here

+0

你在使用什麼適配器?如果是自定義的,請發佈。實際上,它看起來像是將列表項佈局中的一個TextViews設置爲「INVISIBLE」,具體取決於它是類別還是子類別。如果是這樣的話,你應該使用'GONE'來代替。但如果這不是問題,請張貼適配器。 –

+0

我已更新帖子,請檢查 –

+0

我沒有使用任何可見或不可見的 –

回答

0

您使用的是相同的Data類兩個類別和子類別的圖像。當JSON數據是一個類別時,您在Data對象上設置類別String,但不在子類別String中設置類別。當數據是一個子類別時反之亦然。您未設置的String保留爲空,並且在TextView上設置空文本將其留空。因此,當列表項目代表某個類別時,頂部TextView會顯示類別名稱,但底部TextView爲空。下一個項目將是一個子類別,出於相同的原因,頂部TextView將爲空白。兩個相鄰的空白TextView在類別與其第一個子類別之間留有很大差距。以下子類別項目中的空白類別TextView s在這些項目之間留下較小的差距。

如果您想盡可能多地使用當前設置,一種解決方案是根據數據類型切換TextView的可見性屬性,我們可以通過檢查空對象成員來確定這些屬性。

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; 
} 
相關問題