gpt4 book ai didi

android - Fragment、Volley 和 RecyclerView

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:39 24 4
gpt4 key购买 nike

我希望有人能帮我解决我的问题。我有一个有 3 个选项卡的 android 应用程序,我使用 fragment ,第一个选项卡是 recyclerView 列表,第二个选项卡是 map 。问题出在选项卡 1 中,我需要使用 volley 获取数据到选项卡 1 上的 recyclerView,如果运行正常但我无法在第一个应用程序启动时看到数据,但是当我更改选项卡并再次返回选项卡 1 时,它将刷新数据并在 recyclerView 上显示数据。

适配器.java

public class CustomListAdapterWarkop extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private List<Warkop> mWarkop;
private LayoutInflater inflater;


public CustomListAdapterWarkop(Context context, List<Warkop> mWarkop) {
this.context=context;
inflater= LayoutInflater.from(context);
this.mWarkop = mWarkop;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_warkop_row, parent, false);
ItemViewHolder holder = new ItemViewHolder(view);

return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ItemViewHolder viewHolder = (ItemViewHolder) holder;
Warkop current = mWarkop.get(position);

viewHolder.tvNamaWarkop.setText(current.getNamaWarkop());


ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.noimage)
.showImageOnFail(R.drawable.noimage)
.showImageOnLoading(R.drawable.noimage).build();
imageLoader.displayImage(current.getFotoWarkop(), viewHolder.ivFotoWarkop, options);

}

@Override
public int getItemCount() {
return mWarkop.size();
}

}

ItemHolder.java

package com.andylah.warkopedia;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
* Created by andylah on 11/3/2017.
*/

public class ItemViewHolder extends RecyclerView.ViewHolder {

public ImageView ivFotoWarkop;
public TextView tvNamaWarkop;

public ItemViewHolder(View itemView) {
super(itemView);

tvNamaWarkop = itemView.findViewById(R.id.nama_warkop);
ivFotoWarkop = itemView.findViewById(R.id.image_warkop);

}
}

标签1.java

public class tabSatu extends Fragment {

private static final String TAG = tabDua.class.getSimpleName();
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private boolean isFragmentLoaded = false;
View vTabSatu;
private RecyclerView recyclerView;
public static List<Warkop> warkopList = new ArrayList<Warkop>();
private CustomListAdapterWarkop warkopAdapter;

public tabSatu(){

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

new AsyncFetch().execute();

vTabSatu = inflater.inflate(R.layout.tabsatu_view, container, false);

recyclerView = vTabSatu.findViewById(R.id.warkop_container);

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());

recyclerView.setLayoutManager(layoutManager);

Log.d("LOG : ", "onCreatedView Run");

// Inflate the layout for this fragment
return vTabSatu;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

warkopAdapter = new CustomListAdapterWarkop(getActivity(), warkopList);

warkopAdapter.notifyDataSetChanged();

recyclerView.setAdapter(warkopAdapter);

Log.d("LOG : ", "onViewCreated Run");
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && !isFragmentLoaded ) {
// Load your data here or do network operations here
isFragmentLoaded = true;
//new AsyncFetch().execute();
}else{
isFragmentLoaded = false;


Log.d("LOG : ", "isFragmentLoaded = false");
}
}

private class AsyncFetch extends AsyncTask<String, String, String> {

ProgressDialog pDialog = new ProgressDialog(getActivity());

HttpURLConnection conn;
URL url = null;

@Override
protected void onPreExecute() {
super.onPreExecute();

pDialog.setMessage("Loading list warkop ...");
pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected String doInBackground(String... strings) {
try {

// Enter URL address where your json file resides
// Even you can make call to php file which returns json data
url = new URL(AppConfig.LOAD_WARKOP);

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {

// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");

// setDoOutput to true as we recieve data from json file
conn.setDoOutput(true);

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {

int response_code = conn.getResponseCode();

// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {

// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;

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

// Pass data to onPostExecute method
return (result.toString());

} else {

return ("unsuccessful");
}

} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}

@Override
protected void onPostExecute(String result) {
pDialog.dismiss();

try{
JSONObject object = new JSONObject(result);
String getObject = object.getString("warkop");
JSONArray jsonArray = new JSONArray(getObject);
boolean error = object.getBoolean("error");

if(!error){
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);

Warkop warkop = new Warkop();

warkop.setNamaWarkop(jsonObject.getString("nama_warkop"));
warkop.setAlamatWrkop(jsonObject.getString("alamat_warkop"));
warkop.setKotaWarkop(jsonObject.getString("kota_warkop"));
warkop.setLatWarkop(Double.parseDouble(jsonObject.getString("lat_warkop")));
warkop.setLangWarkop(Double.parseDouble(jsonObject.getString("long_warkop")));
warkop.setIsWifi(Integer.parseInt(jsonObject.getString("is_wifi")));
warkop.setIsToilet(Integer.parseInt(jsonObject.getString("is_toilet")));
warkop.setIsTv(Integer.parseInt(jsonObject.getString("is_tv")));
warkop.setIsColokan(Integer.parseInt(jsonObject.getString("is_colokan")));
warkop.setIsParkir(Integer.parseInt(jsonObject.getString("is_parkir")));
warkop.setFotoWarkop(jsonObject.getString("foto_warkop"));

warkopList.add(warkop);
}
}else{
String errorMsg = object.getString("error_msg");
Toast.makeText(getContext(),
errorMsg, Toast.LENGTH_LONG).show();
}

}catch (JSONException e) {

e.printStackTrace();
Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

}
}

}

最佳答案

问题:即使您调用 notifyDataSetChanged(),但 Adapter 中没有数据。

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

warkopAdapter = new CustomListAdapterWarkop(getActivity(), warkopList);
warkopAdapter.notifyDataSetChanged();
recyclerView.setAdapter(warkopAdapter);
}

因此需要在API调用后设置warkopList并通知给Adapter。它会帮助你。

标签状态:

    @Override
protected void onPostExecute(String result) {
pDialog.dismiss();

try {
JSONObject object = new JSONObject(result);
String getObject = object.getString("warkop");
JSONArray jsonArray = new JSONArray(getObject);
boolean error = object.getBoolean("error");

if (!error) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);

Warkop warkop = new Warkop();

...

warkopList.add(warkop);
adapter.setItems(warkopList);
}
}
...
}

CustomListAdapterWarkop: 添加setItem()方法到适配器

public class CustomListAdapterWarkop extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...
public void setItems(List<WarkopList> warkopList) {
mWarkop = warkopList;
notifyDataSetChanged();
}
...
}

关于android - Fragment、Volley 和 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com