我必须知道JSON数组解析并在ListView中加载,其功能类似于在滚动事件上加载更多数据。
以下是我的班级代码。
public class MainActivity extends AppCompatActivity {
String URL = "API-URL";
int page_number = 1;
ListView lv;
ArrayList<ArrayList<HashMap<String,String>>> data = new ArrayList<ArrayList<HashMap<String,String>>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.listview);
new PerformTask().execute();
}
ProgressDialog mProgressDialog;
private class PerformTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
String json="";
try {
JSONParser jsonParser = new JSONParser();
json = jsonParser.getJSONdata(URL,page_number++);
Log.e("jsonResponse", json + "");
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (!result.equals("")) {
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray dataArray = jsonObj.optJSONArray("key");
for(int i = 0; i<dataArray.length(); i++){
ArrayList<HashMap<String,String>> dataArrayList = new ArrayList<HashMap<String,String>>();
JSONArray dataArray_inner = dataArray.getJSONArray(i);
for(int j = 0; j<dataArray_inner.length(); j++){
JSONObject object22 = dataArray_inner.optJSONObject(j);
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("your_key", object22.getString("key_data"));
hashMap.put("your_key", object22.getString("post_content"));
dataArrayList.add(hashMap);
}
data.add(dataArrayList);
}
if(data.size() > 0){
Log.e("Data ","Data are available");
AdapterMain adapter = new AdapterMain(MainActivity.this, data);
lv.setAdapter(adapter);
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
int threshold = 1;
int count = lv.getAdapter().getCount();
if (i == SCROLL_STATE_IDLE) {
if (lv.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMorePerformTask AsyncTask;
new LoadMorePerformTask().execute();
}
}
}
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
mProgressDialog.dismiss();
}
}
private class LoadMorePerformTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Loading more...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
String json="";
try {
JSONParser jsonParser = new JSONParser();
json = jsonParser.getJSONdata(URL,page_number++);
Log.e("jsonResponse", json + "");
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (!result.equals("")) {
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray dataArray = jsonObj.optJSONArray("key");
for(int i = 0; i<dataArray.length(); i++){
ArrayList<HashMap<String,String>> dataArrayList = new ArrayList<HashMap<String,String>>();
JSONArray dataArray_inner = dataArray.getJSONArray(i);
for(int j = 0; j<dataArray_inner.length(); j++){
JSONObject object22 = dataArray_inner.optJSONObject(j);
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("your_key", object22.getString("key_data"));
hashMap.put("your_key", object22.getString("key_data"));
dataArrayList.add(hashMap);
}
data.add(dataArrayList);
}
if(data.size() > 0){
Log.e("Data ","Data are available");
int lv_position = lv.getLastVisiblePosition();
AdapterMain adapter = new AdapterMain(MainActivity.this, data);
lv.setAdapter(adapter);
lv.setSelectionFromTop(lv_position, 0);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
mProgressDialog.dismiss();
}
}
}
此处提供适配器类代码。
public class AdapterMain extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private ArrayList<ArrayList<HashMap<String,String>>> arraylist;
protected int count;
public AdapterMain(Context context, ArrayList<ArrayList<HashMap<String,String>>> numberlist) {
mContext = context;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<ArrayList<HashMap<String,String>>>();
this.arraylist.addAll(numberlist);
}
public class ViewHolder {
TextView title1, description1, title2, description2;
RelativeLayout rel_second;
}
@Override
public int getCount() {
return arraylist.size();
}
@Override
public ArrayList<HashMap<String,String>> getItem(int position) {
return arraylist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.row_view, null);
holder.rel_second = (RelativeLayout) view.findViewById(R.id.rel_second);
holder.title1 = (TextView) view.findViewById(R.id.title1);
holder.description1 = (TextView) view.findViewById(R.id.description1);
holder.title2 = (TextView) view.findViewById(R.id.title2);
holder.description2 = (TextView) view.findViewById(R.id.description2);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextView
holder.title1.setText(arraylist.get(position).get(0).get("key1"));
holder.description1.setText(arraylist.get(position).get(0).get("key2"));
holder.rel_second.setVisibility(View.VISIBLE);
holder.title2.setText(arraylist.get(position).get(1).get("key1"));
holder.description2.setText(arraylist.get(position).get(1).get("key2"));
// Listen for ListView Item Click
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Click Perform of Item
}
});
return view;
}
}
JSONParser类在这里。
public class JSONParser {
static InputStream is = null;
public JSONParser() {
}
public String getJSONdata(String URL, int pageNumber){
String response_str = "";
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
post.addHeader(new BasicHeader("parameter_key", ""+pageNumber));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
response_str=sb.toString();
} catch (Exception e){
e.printStackTrace();
}
return response_str;
}
}
这是Gradle文件代码。
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "testdemoapp.sandy.com.myapplication"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:26.0.1'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'org.apache.httpcomponents:httpcore:4.4.1'
}
在这里我遇到了在滚动时加载更多数据的问题。有时候它会调用AsyncTask两次并加载比所需数据更多的数据。
如果有任何想法,请告诉我。 提前谢谢。
答案 0 :(得分:2)
你应该使用布尔值。
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
int threshold = 1;
int count = lv.getAdapter().getCount();
if (i == SCROLL_STATE_IDLE) {
if (lv.getLastVisiblePosition() >= count
- threshold) {
// Execute LoadMorePerformTask AsyncTask;
new LoadMorePerformTask().execute();
}
}
}
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
}
});
问题在于您的代码。您是创建请求,每个列表都到达底部。您需要保留一个布尔isLoaded = false
,然后在列表到达底部时触发请求,检查isLoaded
是true
然后触发请求,在异步任务中,您可以设置布尔值true收到结果并在触发请求后将其设置为false。
if (i == SCROLL_STATE_IDLE) {
if (lv.getLastVisiblePosition() >= count- threshold) {
// Execute LoadMorePerformTask AsyncTask;
if(isLoaded){
new LoadMorePerformTask().execute();
isLoaded=false
}
}
和aysc
protected void onPostExecute(String result) {
super.onPostExecute(result);
isLoaded = true; //and your rest of the code}
希望这会有所帮助。