ListView不会显示在片段内的选项卡中(ListView doesnot show up in tab inside fragment)

编程入门 行业动态 更新时间:2024-10-24 10:23:25
ListView不会显示在片段内的选项卡中(ListView doesnot show up in tab inside fragment)

我有以下代码来获取列表视图的数据和一个发布它的适配器: -

public class ByState extends Fragment { public ArrayList<Person> personData = new ArrayList<Person>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.by_state, container, false); Context cont=getActivity(); new GetAllStates(getContext(),rootView).execute(); return rootView; } class GetAllStates extends AsyncTask<String,String,JsonReader> { private ByState asyncContext=null; private View rootView=null; private Context context=null; public GetAllStates(Context state,View view) { this.context=state; this.rootView = view; } @Override protected JsonReader doInBackground(String... key) { JsonReader reader=null; try { String url = "http://congress-search-148802.appspot.com/getData.php?value=legislator"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); reader = new JsonReader(new InputStreamReader(con.getInputStream())); } catch (Exception e) { Log.w("Error", e.getMessage()); } return reader; } @Override protected void onPostExecute(JsonReader reader) { ListView yourListView = (ListView) rootView.findViewById(R.id.state_listView); Person object; try { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("results")){ reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); object=new Person(); while (reader.hasNext()) { String id = reader.nextName(); if(id.equals("bioguide_id")){ String bioguide_id=reader.nextString(); object.bioguide=bioguide_id; } else if(id.equals("last_name")){ String lastname=reader.nextString(); object.lastname=lastname; } else if(id.equals("first_name")){ String firstname=reader.nextString(); object.firstname=firstname; } else if(id.equals("party")){ String party=reader.nextString(); object.party=party; } else if(id.equals("state_name")){ String statename=reader.nextString(); object.state=statename; } else if(id.equals("district")){ if (reader.peek() == JsonToken.NULL){ reader.skipValue(); object.district="NA"; } else{ String district=reader.nextString(); object.district=district; } } else reader.skipValue(); object.image="https://theunitedstates.io/images/congress/original/"+object.bioguide+".jpg"; //object.display(); } personData.add(object); reader.endObject(); }reader.endArray(); } else { reader.skipValue(); } }reader.endObject(); } catch (IOException e) { Log.w("Error",e.getMessage()); } ListAdapter customAdapter = new ListAdapter(context, R.layout.bystate_itemview,personData); yourListView .setAdapter(customAdapter); } } public class ListAdapter extends ArrayAdapter<Person> { public ListAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); } public ListAdapter(Context context, int resource, ArrayList<Person> items) { super(context, resource, items); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi; vi = LayoutInflater.from(getContext()); v = vi.inflate(R.layout.bystate_itemview, null); } Person p = getItem(position); if (p != null) { TextView tt1 = (TextView) v.findViewById(R.id.last_name); TextView tt2 = (TextView) v.findViewById(R.id.first_name); if (tt1 != null) { tt1.setText(p.getLastname()); } if (tt2 != null) { tt2.setText(p.getFirstname()); } } return v; } } } class Person{ String bioguide; String image; String lastname; String firstname; String district; String state; String party; public String getBioguide() { return bioguide; } public void setBioguide(String bioguide) { this.bioguide = bioguide; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getParty() { return party; } public void setParty(String party) { this.party = party; } public String getState() { return state; } public void setState(String state) { this.state = state; } }

arraylist包含所有对象,但列表视图根本不显示。 它只显示一个空白屏幕。

我的xml: -

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/state_listView" android:layout_alignParentTop="true" /> </RelativeLayout>

和itemview xml: -

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <TextView android:layout_width="163dp" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/last_name" android:layout_weight="0.06" /> <TextView android:layout_width="102dp" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:id="@+id/first_name" android:layout_weight="0.06" /> </LinearLayout>

这是我的第一个自定义适配器,我不知道我哪里出错了。 任何帮助都非常感谢。 片段和标签正确显示。

I have the following code to get data for list view and a adapter to post it:-

public class ByState extends Fragment { public ArrayList<Person> personData = new ArrayList<Person>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.by_state, container, false); Context cont=getActivity(); new GetAllStates(getContext(),rootView).execute(); return rootView; } class GetAllStates extends AsyncTask<String,String,JsonReader> { private ByState asyncContext=null; private View rootView=null; private Context context=null; public GetAllStates(Context state,View view) { this.context=state; this.rootView = view; } @Override protected JsonReader doInBackground(String... key) { JsonReader reader=null; try { String url = "http://congress-search-148802.appspot.com/getData.php?value=legislator"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); reader = new JsonReader(new InputStreamReader(con.getInputStream())); } catch (Exception e) { Log.w("Error", e.getMessage()); } return reader; } @Override protected void onPostExecute(JsonReader reader) { ListView yourListView = (ListView) rootView.findViewById(R.id.state_listView); Person object; try { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("results")){ reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); object=new Person(); while (reader.hasNext()) { String id = reader.nextName(); if(id.equals("bioguide_id")){ String bioguide_id=reader.nextString(); object.bioguide=bioguide_id; } else if(id.equals("last_name")){ String lastname=reader.nextString(); object.lastname=lastname; } else if(id.equals("first_name")){ String firstname=reader.nextString(); object.firstname=firstname; } else if(id.equals("party")){ String party=reader.nextString(); object.party=party; } else if(id.equals("state_name")){ String statename=reader.nextString(); object.state=statename; } else if(id.equals("district")){ if (reader.peek() == JsonToken.NULL){ reader.skipValue(); object.district="NA"; } else{ String district=reader.nextString(); object.district=district; } } else reader.skipValue(); object.image="https://theunitedstates.io/images/congress/original/"+object.bioguide+".jpg"; //object.display(); } personData.add(object); reader.endObject(); }reader.endArray(); } else { reader.skipValue(); } }reader.endObject(); } catch (IOException e) { Log.w("Error",e.getMessage()); } ListAdapter customAdapter = new ListAdapter(context, R.layout.bystate_itemview,personData); yourListView .setAdapter(customAdapter); } } public class ListAdapter extends ArrayAdapter<Person> { public ListAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); } public ListAdapter(Context context, int resource, ArrayList<Person> items) { super(context, resource, items); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi; vi = LayoutInflater.from(getContext()); v = vi.inflate(R.layout.bystate_itemview, null); } Person p = getItem(position); if (p != null) { TextView tt1 = (TextView) v.findViewById(R.id.last_name); TextView tt2 = (TextView) v.findViewById(R.id.first_name); if (tt1 != null) { tt1.setText(p.getLastname()); } if (tt2 != null) { tt2.setText(p.getFirstname()); } } return v; } } } class Person{ String bioguide; String image; String lastname; String firstname; String district; String state; String party; public String getBioguide() { return bioguide; } public void setBioguide(String bioguide) { this.bioguide = bioguide; } public String getDistrict() { return district; } public void setDistrict(String district) { this.district = district; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getParty() { return party; } public void setParty(String party) { this.party = party; } public String getState() { return state; } public void setState(String state) { this.state = state; } }

The arraylist contains all the objects but the list view is not displayed at all. It just shows up a blanks screen.

My xml's:-

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/state_listView" android:layout_alignParentTop="true" /> </RelativeLayout>

and the itemview xml:-

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="1"> <TextView android:layout_width="163dp" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/last_name" android:layout_weight="0.06" /> <TextView android:layout_width="102dp" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:id="@+id/first_name" android:layout_weight="0.06" /> </LinearLayout>

This is my first custom adapter and I dont know where I am going wrong. Any help is highly appreciated. The fragment and tabs are showing up properly.

最满意答案

也许尝试以下方法:

android:layout_width="match_parent" android:layout_height="match_parent"

在相对布局和列表视图中也是如此。

Maybe try the following:

android:layout_width="match_parent" android:layout_height="match_parent"

in the relative layout and the listview as well.

更多推荐

本文发布于:2023-08-06 15:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451564.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项卡   段内   ListView   doesnot   fragment

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!