2017-04-02 15 views
1

這樣的IM下面這個教程http://www.wingnity.com/blog/android-json-parsing-and-image-loading-tutorial/如何解析JSON阿比顯示數據爲Listview的ListView

我想在我的片段,以適應代碼,在onCreateView方法,但列表視圖不顯示(空白片段)。我不知道爲什麼。

我進口的所有所需的庫

Actor.java

public class Actors { 

private String name; 
private String description; 
private String dob; 
private String country; 
private String height; 
private String spouse; 
private String children; 
private String image; 

public Actors() { 
    // TODO Auto-generated constructor stub 
} 

public Actors(String name, String description, String dob, String country, 
       String height, String spouse, String children, String image) { 
    super(); 
    this.name = name; 
    this.description = description; 
    this.dob = dob; 
    this.country = country; 
    this.height = height; 
    this.spouse = spouse; 
    this.children = children; 
    this.image = image; 
} 


public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getDob() { 
    return dob; 
} 

public void setDob(String dob) { 
    this.dob = dob; 
} 

public String getCountry() { 
    return country; 
} 

public void setCountry(String country) { 
    this.country = country; 
} 

public String getHeight() { 
    return height; 
} 

public void setHeight(String height) { 
    this.height = height; 
} 

public String getSpouse() { 
    return spouse; 
} 

public void setSpouse(String spouse) { 
    this.spouse = spouse; 
} 

public String getChildren() { 
    return children; 
} 

public void setChildren(String children) { 
    this.children = children; 
} 

public String getImage() { 
    return image; 
} 

public void setImage(String image) { 
    this.image = image; 
} 

} 

ActorAdapter.java

import java.io.InputStream; 
import java.util.ArrayList; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

import info.androidhive.navigationdrawer.R; 

public class ActorAdapter extends ArrayAdapter<Actors> { 
ArrayList<Actors> actorList; 
LayoutInflater vi; 
int Resource; 
ViewHolder holder; 

public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) { 
    super(context, resource, objects); 
    vi = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    Resource = resource; 
    actorList = objects; 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // convert view = design 
    View v = convertView; 
    if (v == null) { 
     holder = new ViewHolder(); 
     v = vi.inflate(Resource, null); 
     holder.imageview = (ImageView) v.findViewById(R.id.ivImage); 
     holder.tvName = (TextView) v.findViewById(R.id.tvName); 
     holder.tvDescription = (TextView) v.findViewById(R.id.tvDescriptionn); 
     holder.tvDOB = (TextView) v.findViewById(R.id.tvDateOfBirth); 
     holder.tvCountry = (TextView) v.findViewById(R.id.tvCountry); 
     holder.tvHeight = (TextView) v.findViewById(R.id.tvHeight); 
     holder.tvSpouse = (TextView) v.findViewById(R.id.tvSpouse); 
     holder.tvChildren = (TextView) v.findViewById(R.id.tvChildren); 
     v.setTag(holder); 
    } else { 
     holder = (ViewHolder) v.getTag(); 
    } 
    holder.imageview.setImageResource(R.drawable.imgadd); 
    new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage()); 
    holder.tvName.setText(actorList.get(position).getName()); 
    holder.tvDescription.setText(actorList.get(position).getDescription()); 
    holder.tvDOB.setText("B'day: " + actorList.get(position).getDob()); 
    holder.tvCountry.setText(actorList.get(position).getCountry()); 
    holder.tvHeight.setText("Height: " + actorList.get(position).getHeight()); 
    holder.tvSpouse.setText("Spouse: " + actorList.get(position).getSpouse()); 
    holder.tvChildren.setText("Children: " + actorList.get(position).getChildren()); 
    return v; 

} 

static class ViewHolder { 
    public ImageView imageview; 
    public TextView tvName; 
    public TextView tvDescription; 
    public TextView tvDOB; 
    public TextView tvCountry; 
    public TextView tvHeight; 
    public TextView tvSpouse; 
    public TextView tvChildren; 

} 

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 

} 
} 

Articles.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:padding="4dp" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/ivImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="8dp" 
     android:src="@drawable/imgadd" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/tvName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Tom Cruise" 
      android:textColor="#166CED" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <TextView 
      android:id="@+id/tvDateOfBirth" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#D64530" 
      android:text="Date of Birth: July 3, 1962" /> 

     <TextView 
      android:id="@+id/tvHeight" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Height: 1.80 m" 
      android:textColor="#D64530" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

     <TextView 
      android:id="@+id/tvCountry" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#D64530" 
      android:text="United States" /> 

    </LinearLayout> 

</LinearLayout> 

<TextView 
    android:id="@+id/tvDescriptionn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#009A57" 
    android:text="Description" /> 

<TextView 
    android:id="@+id/tvSpouse" 
    android:layout_width="wrap_content" android:textColor="#166CED" 
    android:layout_height="wrap_content" 
    android:text="Spouse: Katie Holmes" /> 

<TextView 
    android:id="@+id/tvChildren" 
    android:layout_width="wrap_content" android:textColor="#166CED" 
    android:layout_height="wrap_content" 
    android:text="Children: Suri Cruise, Isabella Jane Cruise, Connor Cruise" /> 

</LinearLayout> 

Fragment_accueil.xml(我的片段佈局)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:padding="4dp" 
android:orientation="vertical" 
tools:context="info.androidhive.navigationdrawer.fragment.AccueilFragment"> 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:listitem="@layout/articles"> 

</ListView> 

</LinearLayout> 

fragment_accueil.java(我的片段活動)

//import..... 
public class AccueilFragment extends Fragment { 
private static final String ARG_PARAM1 = "param1"; 
private static final String ARG_PARAM2 = "param2"; 

private String mParam1; 
private String mParam2; 
int score = 0; 
TextView t1, t2, t3,t4; 
ImageView img; 

private OnFragmentInteractionListener mListener; 

public AccueilFragment() { 
} 

public static AccueilFragment newInstance(String param1, String param2) { 
    AccueilFragment fragment = new AccueilFragment(); 
    Bundle args = new Bundle(); 
    args.putString(ARG_PARAM1, param1); 
    args.putString(ARG_PARAM2, param2); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     mParam1 = getArguments().getString(ARG_PARAM1); 
     mParam2 = getArguments().getString(ARG_PARAM2); 
    } 

} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_accueil, container, false); 


    t1 = (TextView) view.findViewById(R.id.tvName); 
    t2 = (TextView) view.findViewById(R.id.tvDateOfBirth); 
    t3 = (TextView) view.findViewById(R.id.tvHeight); 
    t3 = (TextView) view.findViewById(R.id.tvCountry); 
    img = (ImageView) view.findViewById(R.id.ivImage); 
    ArrayList actorsList = new ArrayList<Actors>(); 

    ListView listview = (ListView) view.findViewById(R.id.list); 
    ListAdapter adapter = new ActorAdapter(getActivity().getApplicationContext(), R.layout.articles, actorsList); 

    listview.setAdapter(adapter); 

    new getData().execute(); 


    return view; 

} 



public void onButtonPressed(Uri uri) { 
    if (mListener != null) { 
     mListener.onFragmentInteraction(uri); 
    } 
} 

class getData extends AsyncTask<String, String, String> { 

    HttpURLConnection urlConnection; 


    @Override 
    protected String doInBackground(String... args) { 

     StringBuilder result = new StringBuilder(); 

     try { 
      URL url = new URL("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors"); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

      String line; 
      while ((line = reader.readLine()) != null) { 
       result.append(line); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      urlConnection.disconnect(); 
     } 


     return result.toString(); 
    } 



    @Override 
    protected void onPostExecute(String result) { 
     try { 
      JSONObject jsono = new JSONObject(result); 
      JSONArray jarray = jsono.getJSONArray("actors"); 

      for (int i = 0; i < jarray.length(); i++) { 
       JSONObject object = jarray.getJSONObject(i); 

       Actors actor = new Actors(); 

       actor.setName(object.getString("name")); 
       actor.setDescription(object.getString("description")); 
       actor.setDob(object.getString("dob")); 
       actor.setCountry(object.getString("country")); 
       actor.setHeight(object.getString("height")); 
       actor.setSpouse(object.getString("spouse")); 
       actor.setChildren(object.getString("children")); 
       actor.setImage(object.getString("image")); 

      } 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
    mListener = null; 
} 
public interface OnFragmentInteractionListener { 

    void onFragmentInteraction(Uri uri); 
} 



} 

有什麼問題,如何解決?感謝

回答

1

有幾件事情錯誤:

1.You不添加Actors actor = new Actors();actorsList

2.Calling這樣的:

ArrayList actorsList = new ArrayList<Actors>(); 

ListView listview = (ListView) view.findViewById(R.id.list); 
ListAdapter adapter = new ActorAdapter(getActivity().getApplicationContext(), R.layout.articles, actorsList); 

將在空白數據reuslt在listview

  • 我建議你在你的onPostExecute()的數據添加到ArrayList,然後初始化數據適配器。
  • ,或者您也可以撥打

    adapter.notifyDataSetChanged() in onPostExecute() 
    
    +0

    我在'actor.setImage ...'後面的'onPostExecute()'中使用了這個代碼'actorList.add(actor);'但是im有這個錯誤'無法爲actorList解析符號'。我有同樣的錯誤,如果我把adapter.notifyDataSetChanged –

    +1

    使'ArrayList actorsList'作爲全球性,並初始化它onCreateView()...'actorsList = new ArrayList ();' – rafsanahmad007

    +0

    好吧,那樣做! –

    1

    你加入,你想讓它顯示例如在片段中的ListView:

    public class FragmentMain extends Fragment 
    { 
        private ListView listView; //Define the list view up here. 
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
             Bundle savedInstanceState) { 
        //Set the listview equal to the fragment name 
        listView = inflater.inflate(R.layout.fragment_name, container, false); 
    
        } 
    } 
    

    這應該工作,讓我知道如果otherwize :)

    +0

    THX你的答案。我找到了解決方案。 +1投票給你 –