[英]Android-recycleView can't show out and i don't know hot to show the value to my TextView
我有兩個標題問題
首先是我的recycleView
無法顯示,我返回了MyAdapter類中的void onCreateViewHolder
和MainActivity類中的setAdapter的視圖,但是它不起作用
第二個是我的List 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();
}
}
}
在showRoute()
方法中showRoute()
arraylist之后,調用youradapter.notifyDataChanged()
,這是您的問題。
嘗試將對適配器的引用存儲在以下字段中: public RecyclerView recyclerView; private mAdapter adapter;
public RecyclerView recyclerView; private mAdapter adapter;
並在for循環后調用notifyDatasetChanged(): adapter.notifyDatasetChanged();
接通后該行showRoute(s)
在onPostExecute
功能:
recyclerView.setAdapter(new MyAdapter(this,arrayList));
或者,您必須使適配器成為全局適配器並將其設置為arraylist。 然后,您可以調用notifyDatasetChanged
刷新列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.