ListView 未显示在片段内的选项卡中
ListView doesnot show up in tab inside fragment
我有以下代码来获取列表视图的数据和 post 它的适配器:-
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;
}
}
数组列表包含所有对象,但根本不显示列表视图。它只是显示一个空白屏幕。
我的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>
和项目视图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>
这是我的第一个自定义适配器,我不知道哪里出错了。非常感谢任何帮助。片段和标签正确显示。
也许可以尝试以下方法:
android:layout_width="match_parent"
android:layout_height="match_parent"
在相对布局和列表视图中也是如此。
我有以下代码来获取列表视图的数据和 post 它的适配器:-
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;
}
}
数组列表包含所有对象,但根本不显示列表视图。它只是显示一个空白屏幕。
我的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>
和项目视图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>
这是我的第一个自定义适配器,我不知道哪里出错了。非常感谢任何帮助。片段和标签正确显示。
也许可以尝试以下方法:
android:layout_width="match_parent"
android:layout_height="match_parent"
在相对布局和列表视图中也是如此。