简体   繁体   English

ListView未显示在片段内的选项卡中

[英]ListView doesnot show up in tab inside fragment

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. arraylist包含所有对象,但是根本不显示列表视图。 It just shows up a blanks screen. 它只是显示一个空白屏幕。

My xml's:- 我的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>

and the itemview xml:- 和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. 片段和选项卡显示正确。

Maybe try the following: 也许尝试以下方法:

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

in the relative layout and the listview as well. 在相对布局和列表视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM