Java 滑动刷新
我是Java和Android开发的新手 坚持在android中实现“滑动刷新”。我正在尝试访问另一个类中的方法。我将强调android studio在blockquote中给出的错误。非常感谢你的帮助 MainActivity.javaJava 滑动刷新,java,android,pull-to-refresh,android-swipe,Java,Android,Pull To Refresh,Android Swipe,我是Java和Android开发的新手 坚持在android中实现“滑动刷新”。我正在尝试访问另一个类中的方法。我将强调android studio在blockquote中给出的错误。非常感谢你的帮助 MainActivity.java import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.widget.SwipeRef
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity{
private String TAG = MainActivity.class.getSimpleName();
private SwipeRefreshLayout swipeRefreshLayout;
private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
private static String url = "http://hooman.com/hoo.php";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
lv = (ListView) findViewById(R.id.list);
/**
*异步任务类通过HTTP调用获取json
*/
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
swipeRefreshLayout.setRefreshing(true);
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONArray联系人=新的JSONArray(jsonStr);
//通过所有触点循环
对于(int i=0;ivalue
触点。输入(“kw”,kw);
触点输入(“kva”,kva);
联系方式。put(“pf”,pf);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
//停止刷卡刷新
swipeRefreshLayout.setRefreshing(false);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
吐司长度(长)
.show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
吐司长度(长)
.show();
//停止刷卡刷新
swipeRefreshLayout.setRefreshing(false);
}
});
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
MainActivity.this,联系人列表,
R.layout.list_项,新字符串[]{“kw”,“kva”,
“pf”},新的int[]{R.id.kw,
R.id.kva,R.id.pf});
低压设置适配器(适配器);
}
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/kw"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/kva"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/pf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/kw"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/kva"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/pf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
提前感谢。首先,不要从doInBackground()方法调用UI视图 您可以从onPreExcute()方法或onPostExecute方法调用它们 但由于AsyncTask位于另一个文件中,所以也不能直接调用它们
因为您想在刷新SwipeRefreshLayout时执行AsyncTask代码 有两种方法: 第一:使AsyncTask类成为调用活动的内部类。(仅当您仅打算在此活动中使用此异步任务时才有用 第二:实现对异步任务的回调
3:使用截取/改造(库)。(因为您只调用API并获取JSON数据,所以它们使用“主线程以外的其他线程”默认情况下,它还内置了JSON支持。首先,您有循环依赖关系。AsyncTask不能引用活动中的
swipeRefreshLayout
视图,活动需要任务的实例,然后才能调用task.doInBackground()
(您应该无论如何都不要这样做)。只需要使用execute()
,很少使用execute().get()
您可以做的是重写活动中的postExecute函数
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
// Showing progress dialog
final ProgressDialog pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
contactList.clear();
new GetContacts() {
@Override
protected void onPostExecute(Void result) {
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList, R.layout.list_item,
new String[]{"kw", "kva", "pf"},
new int[]{R.id.kw,R.id.kva, R.id.pf});
lv.setAdapter(adapter);
}
}.execute();
}
}
);
您实际想要什么?是否要调用GetContacts AsyncTask类的doInBackground()函数?为什么要创建一个
私有类
?