gpt4 book ai didi

java - 在从 Volley 中加载数据时向应用程序添加进度对话框

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

我正在尝试向我的应用程序添加一个进度对话框,同时它会从 Volley 中加载数据。我尝试了几种方法,但我目前正在遵循本教程 http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/并希望它看起来像那里使用的示例。到目前为止,请有人看一下我的代码!我没有收到错误,但它没有像示例那样显示。正如您所看到的,我在 onCreate 中创建了该方法,并在响应监听器中调用了 hide 方法。

谢谢!

MainActivity.java

public class MainActivity extends AppCompatActivity {

private List<NewsRecord> newsListData = new ArrayList<NewsRecord>();

private GridView newsListView;

private NewsListAdapter adapter;

LinearLayout layout;

private ProgressDialog pDialog;

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

GridView newsListView = (GridView) findViewById(R.id.newsFeedList);
adapter = new NewsListAdapter(this, R.layout.adapter_news_list, newsListData, this);

layout = (LinearLayout) findViewById(R.id.progressbar_view);
newsListView.setAdapter(adapter);

pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading Articles...");
pDialog.show();

newsListView.setOnItemClickListener(itemClicked);

nextStart = 0;
updateListData(nextStart, 20);

}

public int nextStart = 0;

public void updateListData(int StartPoint, int count){
String url = "http://www.efstratiou.info/projects/newsfeed/getList.php?start=" + StartPoint + "&count=" + count;

EDANewsApp app = EDANewsApp.getInstance();

JsonArrayRequest jsonRequest = new JsonArrayRequest(url, listener, errorListener);
app.requestQueue.add(jsonRequest);

nextStart +=count;
}

@Override
public boolean onCreateOptionsMenu (Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}

@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}

private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}

public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.action_about:
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.action_search:
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}

AdapterView.OnItemClickListener itemClicked = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent intent = new Intent(MainActivity.this, NewsItemActivity.class);

intent.putExtra("newsItemId", newsListData.get(position).recordId);

startActivity(intent);
}
};

private SearchView.OnQueryTextListener searchQueryListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Intent searchIntent = new Intent(MainActivity.this, SearchResultsActivity.class);
searchIntent.putExtra("query", query);
return true;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
};

//Listeners
Response.Listener<JSONArray> listener = new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//we successfully received the JSONArray
//Here we will extract the data and use it in our app

//Clear the dataset before loading new data
// newsListData.clear();
//Go through all the JSON objects
for (int i = 0; i < response.length(); i++) {

try {
//Get one JSON object
JSONObject jsonObj = response.getJSONObject(i);

//Put JSON data in a Java object
NewsRecord record = new NewsRecord();
record.recordId = jsonObj.getInt("record_id");
record.title = jsonObj.getString("title");
record.date = jsonObj.getString("date");
record.shortInfo = jsonObj.getString("short_info");
record.imageUrl = jsonObj.getString("image_url");

newsListData.add(record);

} catch (JSONException e) {
e.printStackTrace();
}

}
adapter.notifyDataSetChanged();
hidePDialog();
}
};

Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//There was an error in the communication
//We can notify the user about it
hidePDialog();

}
};

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/newsListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/progressbar_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >

<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Loading data..." />
</LinearLayout>

<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#C0C0C0" />
</LinearLayout>

<GridView
android:id="@+id/newsFeedList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"/>

</FrameLayout>

最佳答案

尝试下面的代码

ProgressDialog pd = ProgressDialog.show(context,null,"Please wait");
JsonArrayRequest jsonRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
if(pd!=null && pd.isShowing())
pd.dismiss();
// Code

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(pd!=null && pd.isShowing())
pd.dismiss();
// Code
});

app.requestQueue.add(jsonRequest);

关于java - 在从 Volley 中加载数据时向应用程序添加进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34356840/

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