我有兩個問題作爲標題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();
}
}
}
只是想幫忙:你可能想看看Retrofit庫。它將使得從互聯網獲取json對象並將它們轉化爲對象非常容易。在你的情況下,你只需告訴Retrofit你期望來自Web服務的一個Transaction對象,它會爲你完成所有的轉換。 – lemuel
謝謝我得到了解決方案〜 –