2016-09-30 32 views
0

我有兩個問題作爲標題Android的recycleView不能顯示出來,我不知道燙價值展示給我的TextView

首先是我recycleView不能顯示出來,我就返回查看無效onCreateViewHolder在MyAdapter類和setAdapter在MainActivity類別,但它不工作

二是似乎我的名單的ArrayList是空 我從Json的獲得四個值成功,因爲我可以看到Log:FinallyJson信息 我嘗試將這四個值用於兩個代碼

Transaction transaction = new Transaction(name, address, tel, words);  
arrayList.add(transaction); 

它不起作用。這讓我整日感到困惑。我如何顯示出recycleView並讓這四個值setAdaper成功。

有人可以教我解決方案嗎?

這裏是我的三類:

public class *MyAdapter* extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{ 
    private Context context; 
    private List<Transaction> mDatas; 

    public MyAdapter(Context context, List<Transaction> datas) { 
     this.context = context; 
     this.mDatas = datas; 
    } 
    public class MyViewHolder extends RecyclerView.ViewHolder{ 
     private TextView name,address,tel,words; 
     public MyViewHolder(View itemView) { 
      super(itemView); 
      name=(TextView)itemView.findViewById(R.id.name_textView); 
      address=(TextView)itemView.findViewById(R.id.address_textView); 
      tel=(TextView)itemView.findViewById(R.id.tel_textView); 
      words=(TextView)itemView.findViewById(R.id.words_textView); 
     } 
    } 
    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view=View.inflate(parent.getContext(),R.layout.cardview,null); 
     MyViewHolder myViewHolder=new MyViewHolder(view); 
     return myViewHolder; 
    } 
    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
     holder.name.setText(mDatas.get(position).getName().toString()); 
     holder.address.setText(mDatas.get(position).getAddress().toString()); 
     holder.tel.setText(mDatas.get(position).getTel().toString()); 
     holder.words.setText(mDatas.get(position).getWords().toString()); 
    } 
    @Override 
    public int getItemCount() { 
     return mDatas.size(); 
    } 
} 

public class Transaction { 
    String name; 
    String address; 
    String tel; 
    String words; 

    public Transaction() { 
    } 

    public Transaction(String name, String address, String tel, String words) { 
     this.name = name; 
     this.address = address; 
     this.tel = tel; 
     this.words = words; 
    } 

    public String getName() { 
     return name; 
    } 

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

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getTel() { 
     return tel; 
    } 

    public void setTel(String tel) { 
     this.tel = tel; 
    } 

    public String getWords() { 
     return words; 
    } 

    public void setWords(String words) { 
     this.words = words; 
    } 
} 

public class MainActivity extends AppCompatActivity { 

    private final String TAG = "MainActivity"; 
    private final String url = "http://data.coa.gov.tw/Service/OpenData/EzgoTravelFoodStay.aspx"; 
    public RecyclerView recyclerView; 
    public List<Transaction> arrayList = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     new MyTask().execute(url); 
     recyclerView = (RecyclerView) findViewById(R.id.recycleView); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 

     recyclerView.setAdapter(new MyAdapter(this,arrayList)); 

     Log.d("Array:",arrayList.toString()); 
    } 
    class MyTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... strings) { 
      String url = strings[0]; 
      try { 
       String routeJson = getRouteJson(url); 
       return routeJson; 
      } catch (IOException ex) { 
       Log.d(TAG, ex.toString()); 
       return null; 
      } 
     } 
     @Override 
     protected void onPostExecute(String s) { 
      showRoute(s);  
     } 
    } 
    private String getRouteJson(String url) throws IOException { 
     HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
     int responseCode = connection.getResponseCode(); 
     StringBuilder jsonIn = new StringBuilder(); 
     if (responseCode == 200) { 
      BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String line; 
      while ((line = bf.readLine()) != null) { 
       jsonIn.append(line); 
      } 
     } else { 
      Log.d(TAG, "responseCode:" + responseCode); 
     } 
     connection.disconnect(); 
     Log.d(TAG, "jsonIn:" + jsonIn); 
     return jsonIn.toString(); 
    } 
    private void showRoute(String route) { 
     try { 
      JSONArray jsonArray = new JSONArray(route); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsonObject = jsonArray.getJSONObject(i); 
       Log.d("jsonObject", jsonObject.toString()); 
       String name = jsonObject.getString("Name").toString(); 
       String address = jsonObject.getString("Address").toString(); 
       String tel = jsonObject.getString("Tel").toString(); 
       String words = jsonObject.getString("HostWords").toString(); 
       Transaction transaction = new Transaction(name, address, tel, words); 
       arrayList.add(transaction); 
       Log.d("FinallyJson:", name + "," + address + "," + tel + "," + words); 
      } 
     } catch (JSONException ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 
+0

只是想幫忙:你可能想看看Retrofit庫。它將使得從互聯網獲取json對象並將它們轉化爲對象非常容易。在你的情況下,你只需告訴Retrofit你期望來自Web服務的一個Transaction對象,它會爲你完成所有的轉換。 – lemuel

+0

謝謝我得到了解決方案〜 –

回答

1

呼叫youradapter.notifyDataChanged()填充在showRoute() method.That的您的問題ArrayList中後。

+0

感謝您的解決方案,它非常簡單明瞭 –

0

嘗試存儲引用您的適配器在一個領域:後 public RecyclerView recyclerView; private mAdapter adapter; 並調用notifyDatasetChanged()for循環: adapter.notifyDatasetChanged();

+0

感謝您的幫助。有用 !非常感謝你。 –

0

調用此行showRoute(s)後,您onPostExecute功能:

recyclerView.setAdapter(new MyAdapter(this,arrayList)); 

或者,您必須將適配器設置爲全局並將arraylist設置爲它。然後您可以撥打notifyDatasetChanged刷新列表。

+0

感謝您提供的信息,我非常感謝您幫助解決問題。 –