【发布时间】:2026-01-13 09:35:01
【问题描述】:
我在导航滑块中传递了 json,它在项目之间获得了 2 个空格间隙。我不明白为什么会这样。我尝试了几种方法,但都没有成功。
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;
}
这是我如何获得视图的图像
【问题讨论】:
-
您使用的是什么适配器?如果是定制的,请发布。实际上,看起来您将列表项布局中的 TextView 之一设置为
INVISIBLE,具体取决于它是类别还是子类别。如果是这种情况,您应该改用GONE。但如果这不是问题,请发布适配器。 -
我已经更新了帖子,请检查一下
-
我没有使用任何可见的或不可见的
-
您在哪里使用 Navgation_adapter ?请上传它的xml。
-
navigation.xml 更新了它
标签: java android arrays json for-loop