gpt4 book ai didi

java - 这段代码中的 init() 方法有什么用?

转载 作者:行者123 更新时间:2023-12-02 02:39:18 25 4
gpt4 key购买 nike

我正在尝试学习如何使用 JSON,并且我将从教程中查看此代码,并且有一个 init() 方法。根据我在网上发现的信息,init()通常用作小程序的入口点。如果是这样,那么为什么 init() 出现在 Android 应用程序代码中而不是网站代码中?有人可以解释一下它的原因吗?这是在 android 中使用 JSON 时常见的情况还是不常见的情况?

public class MainActivity extends AppCompatActivity {

private RecyclerView mRestaurantRecyclerView;
private RestaurantAdapter mAdapter;
private ArrayList<Restaurant> mRestaurantCollection;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

init();
new FetchDataTask().execute();
}

private void init() {
mRestaurantRecyclerView = (RecyclerView) findViewById(R.id.restaurant_recycler);
mRestaurantRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRestaurantRecyclerView.setHasFixedSize(true);
mRestaurantCollection = new ArrayList<>();
mAdapter = new RestaurantAdapter(mRestaurantCollection, this);
mRestaurantRecyclerView.setAdapter(mAdapter);
}

public class FetchDataTask extends AsyncTask<Void, Void, Void> {
private String mZomatoString;

@Override
protected Void doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
Uri builtUri = Uri.parse(getString(R.string.zomato_api));
URL url;
try {
url = new URL(builtUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("user-key", "acfd3e623c5f01289bd87aaaff1926c1");
urlConnection.connect();

InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
//Nothing to do
return null;
}

reader = new BufferedReader(new InputStreamReader(inputStream));

String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}

if (buffer.length() == 0) {
return null;
}

mZomatoString = buffer.toString();
JSONObject jsonObject = new JSONObject(mZomatoString);

Log.v("Response", jsonObject.toString());

JSONArray restaurantsArray = jsonObject.getJSONArray("restaurants");

//list = new ArrayList<>();
for (int i = 0; i < restaurantsArray.length(); i++) {

Log.v("BRAD_", i + "");
String name;
String address;
String currency;
String imageUrl;
long lon;
long lat;
long cost;
float rating;


JSONObject jRestaurant = (JSONObject) restaurantsArray.get(i);
jRestaurant = jRestaurant.getJSONObject("restaurant");
JSONObject jLocattion = jRestaurant.getJSONObject("location");
JSONObject jRating = jRestaurant.getJSONObject("user_rating");


name = jRestaurant.getString("name");
address = jLocattion.getString("address");
lat = jLocattion.getLong("latitude");
lon = jLocattion.getLong("longitude");
currency = jRestaurant.getString("currency");
cost = jRestaurant.getInt("average_cost_for_two");
imageUrl = jRestaurant.getString("thumb");
rating = (float) jRating.getDouble("aggregate_rating");


Restaurant restaurant = new Restaurant();
restaurant.setName(name);
restaurant.setAddress(address);
restaurant.setLatitiude(lat);
restaurant.setLongitude(lon);
restaurant.setCurrency(currency);
restaurant.setCost(String.valueOf(cost));
restaurant.setImageUrl(imageUrl);
restaurant.setRating(String.valueOf(rating));

mRestaurantCollection.add(restaurant);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("MainActivity", "Error closing stream", e);
}
}
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
mAdapter.notifyDataSetChanged();
}
}

最佳答案

基本上是 init 方法设置 recyclerview 及其配置

mRestaurantRecyclerView = (RecyclerView) findViewById(R.id.restaurant_recycler);

从xml定义回收器并将其放入变量mRestaurantRecyclerVIew

mRestaurantRecyclerView.setLayoutManager(new LinearLayoutManager(this));

设置recyclerview的显示方式,稍后应在项目中定义

mRestaurantRecyclerView.setHasFixedSize(true);

正如函数所说,rv将具有固定大小,您也可以引用此链接了解此特定函数Understanding RecyclerView setHasFixedSize

 mRestaurantCollection = new ArrayList<>(); 

这个 mRestaurantCollection 将保存将在 recyclerview 中显示的数据列表。

mAdapter = new RestaurantAdapter(mRestaurantCollection, this);
mRestaurantRecyclerView.setAdapter(mAdapter);

最后但不是列表,这两行代码将通过适配器将数据与recyclerview Hook 。第一行是启动适配器插入 2 个参数,即数据和上下文,第二行告诉 recyclerview,“嘿,我是你的适配器,显示这个。”。

PS:最后但不是列表意味着一个笑话。哈哈,如果不好笑的话请原谅。

关于java - 这段代码中的 init() 方法有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45747307/

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