[英]Android: Setter in Fragment & get String in RecyclerViewAdapter
我從我的MySQL
數據庫中獲取了一個String
,該String
工作正常。 在獲取數據后,我試圖在Setter中設置數據,並將獲取的數據填充到我的RecyclerAdapter中,以將其輸出為列表,但它不起作用。 唯一有效的方法是從我的數據庫中獲取數據並設置設置器,之后它將不起作用,不知道為什么。
public ArrayList<NewsData> parseJSON(String result) {
ArrayList<NewsData> newsData = new ArrayList<NewsData>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
NewsData nd = new NewsData();
nd.setTitle(json_data.getString("title"));
Toast.makeText(getActivity(), nd.getTitle(), Toast.LENGTH_SHORT).show();
newsData.add(nd);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return newsData;
}
在這里,我正在獲取數據庫的數據,並通過將數據作為Toast進行測試來測試其是否工作正常。
public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.ViewHolder> {
private Context context;
class ViewHolder extends RecyclerView.ViewHolder {
public TextView itemTitle;
public TextView holeText;
public ViewHolder(final View itemView) {
super(itemView);
itemTitle = (TextView) itemView.findViewById(R.id.title_cView);
holeText = (TextView) itemView.findViewById(R.id.text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context = itemView.getContext();
}
});
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.layout_cardviewhome, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 2;
}
}
這是我的適配器,我已經嘗試過一些方法,但是它們無法正常工作,也許你們中的一個可以在這里幫助我。 所以我想將MySQL
的數據添加到RecyclerView
。 (我在“ parseJSON
”中獲得的數據)
當我刪除所有錯誤並無法正常工作時,我的適配器幾乎快用完了
我希望你能聽懂我的英語不是很好。 但總而言之,感謝您嘗試提供的幫助:-)
我猜您應該將newsData作為參數傳遞給適配器構造函數。 在onBindViewHolder中,您可以通過從項目中獲取來設置TextViews信息。 例:
public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.ViewHolder> {
private Context context;
private ArrayList<NewsData> newsData;
public HomeRecyclerAdapter (ArrayList<NewsData> newsData, Context context) {
this.newsData = newsData;
this.context = context;
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView itemTitle;
public TextView holeText;
public ViewHolder(final View itemView) {
super(itemView);
itemTitle = (TextView) itemView.findViewById(R.id.title_cView);
holeText = (TextView) itemView.findViewById(R.id.text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context = itemView.getContext();
}
});
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.layout_cardviewhome, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
NewsData thisNewsData = newsData.get(position);
holder.itemTitle.setText(thisNewsData.getItemTitle());
holder.holeText.setText(thisNewsData.getHoleText());
}
@Override
public int getItemCount() {
return newsDataArray.size();
}
}
如果出現問題,請進行相應調整,並告訴我是否有幫助。
將初始列表傳遞給適配器:
private ArrayList<NewsData> newsData;
public HomeRecyclerAdapter (ArrayList<NewsData> newsData) {
this.newsData = newsData;
}
在doInBackground()
中的AsyncTask
中從數據庫中獲取新數據。 您可以在那里解析數據並創建對象。 在onPostExceute()
您可以修改適配器的數據。 例如,將其添加到適配器並在onPostExceute()
調用它:
public void add(NewsData data) {
this.newsData.add(data);
notifyDataSetChanged();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.