gpt4 book ai didi

java - 使用 AsyncTask 检查列表

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:21 25 4
gpt4 key购买 nike

当我从服务器获取列表时遇到问题,我想检查它是否有任何对象。如果有对象,出现在我的布局文本中:几秒钟后没有对象,它就会消失 并显示我的列表。

我想要如果有对象就会直接出现在我的布局中。 私有(private)类 RetrieveTask 扩展 AsyncTask> {

    @Override
protected List<branch> doInBackground(String... params) {

manager = new SessionManager(getActivity().getApplicationContext());
manager.checkLogin();
HashMap<String, String> user = manager.getUserID();
String uid = user.get(SessionManager.KEY_uid);
// Intent intent= getActivity().getIntent(); // gets the previously created intent
//String type18 = intent.getStringExtra("key28");
String strUrl =

HttpURLConnection connection = null;
BufferedReader reader = null;

try {
URL url = new URL(strUrl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}

String finalJson = buffer.toString();

JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("markers");

////////////////////////////////////////
List<branch> bList = new ArrayList<>();

Gson gson = new Gson();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);

/////////market object
market mr = new market(Integer.valueOf(finalObject.getString("id")), finalObject.getString("mName")
, finalObject.getString("pic"));

/////////location object
location lo = new location(Float.valueOf(finalObject.getString("lat")), Float.valueOf(finalObject.getString("lng"))
, finalObject.getString("area"), finalObject.getString("city"));


/////////(branch object) inside it (market object)
branch it = new branch(Integer.valueOf(finalObject.getString("bid")), Integer.valueOf(finalObject.getString("phone"))
, finalObject.getString("ser"), Integer.valueOf(finalObject.getString("dis")), Integer.valueOf(finalObject.getString("brNo"))
, Float.valueOf(finalObject.getString("rating")), mr, lo,true);

/*//////////add marker
LatLng latlng = new LatLng(Double.valueOf(finalObject.getString("lat")), Double.valueOf(finalObject.getString("lng")));
addMarker(latlng, Integer.valueOf(finalObject.getString("bid")));*/

// adding the branch object in the list
bList.add(it);
}
return bList;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

return null;

}

这里是 postExcute

    @Override
protected void onPostExecute(final List<branch> result) {
super.onPostExecute(result);

if (result != null) {

final itemAdapter adapter = new itemAdapter(getActivity().getApplicationContext(), R.layout.row_favmarkets, result);
lv.setAdapter(adapter);


}
}

}

这是我的布局 xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="4dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
tools:context="com.gmplatform.gmp.favouriteitem">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="250dp"
android:text="There is no Markets in your Favourite List.."
android:textSize="24sp"
android:visibility="gone" />
</LinearLayout>

<ListView
android:id="@+id/l20"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:horizontalSpacing="3dp"
android:numColumns="3"
android:verticalSpacing="0dp" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button6"
android:layout_marginLeft="400dp" />

最佳答案

建议的更改在执行请求时首先不会显示任何内容。完成后,如果列表为空,它将显示“您最喜欢的列表中没有市场..”,否则将显示该列表。

零件布局 XML:

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout28">

<TextView
android:id="@+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="250dp"
android:text="There is no Markets in your Favourite List.."
android:textSize="24sp"
android:visibility="gone" />
</LinearLayout>

初始化linearLayout28 :

LinearLayout linearLayout28 = findViewById(R.id.linearLayout28);

AsyncTask 的更改:

private class RetrieveTask extends AsyncTask<Void, Void, List<branch>> {

@Override
protected void onPreExecute() {
super.onPreExecute();
lv.setVisibility(View.GONE);
linearLayout28.setVisibility(View.GONE);
}

@Override
protected List<branch> doInBackground(String... params) {
//Keep as your code
...
}

@Override
protected void onPostExecute(final List<branch> result) {
super.onPostExecute(result);

if (result != null && result.size() != 0) {
final itemAdapter adapter = new itemAdapter(
getActivity().getApplicationContext(),
R.layout.row_favmarkets, result);
lv.setAdapter(adapter);
lv.setVisibility(View.VISIBLE);
linearLayout28.setVisibility(View.GONE);
} else {
lv.setVisibility(View.GONE);
linearLayout28.setVisibility(View.VISIBLE);
}
}
}

关于java - 使用 AsyncTask 检查列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43748009/

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