Android 正在尝试创建listview。当列表向下滚动时加载,通过截取从PHP获取信息
仪表板活动,显示listviewAndroid 正在尝试创建listview。当列表向下滚动时加载,通过截取从PHP获取信息,android,listview,scroll,refresh,pull-to-refresh,Android,Listview,Scroll,Refresh,Pull To Refresh,仪表板活动,显示listview package renava.com.user.renava.renavamechanic; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import andr
package renava.com.user.renava.renavamechanic;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class DashboardActivity extends AppCompatActivity {
ListView workList;
public ArrayList<String> customerA, progressA, addressA, dateTimeA, contactA, jobIDA, latA, lngA;
SharedPreferences.Editor editor;
boolean cool = false;
SimpleAdapter sampleListAdapter;
HashMap<String, String> sampleObjectMap;
ArrayList<HashMap<String, String>> sampleArrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
jobIDA = new ArrayList<>();
customerA = new ArrayList<>();
contactA = new ArrayList<>();
addressA = new ArrayList<>();
progressA = new ArrayList<>();
dateTimeA = new ArrayList<>();
latA= new ArrayList<>();
lngA = new ArrayList<>();
editor = getSharedPreferences("RenavaSP", MODE_PRIVATE).edit();
workList = (ListView) findViewById(R.id.workList);
int id;
for (id = 1; id < 300; id++) {
String ID = String.valueOf(id);
jobIDA.add(ID);
String url = Config.DATA_URL+ID;
Toast.makeText(getApplicationContext(), ID, Toast.LENGTH_LONG).show();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(DashboardActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
cool = true;
workList.invalidateViews();
}
sampleListAdapter = new SimpleAdapter(DashboardActivity.this, sampleArrayList, R.layout.array_orders, new String[]{
"customerName", "progress", "address", "dateTime", "jobID"}, new int[]{R.id.nameText, R.id.progressText
, R.id.locationText, R.id.jobIdText, R.id.timeText});
workList.setAdapter(sampleListAdapter);
workList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int ith, long l) {
editor.putString("customerName", customerA.get(ith));
editor.putString("progress", progressA.get(ith));
editor.putString("address", addressA.get(ith));
editor.putString("dateTime", dateTimeA.get(ith));
editor.putString("contact", contactA.get(ith));
editor.putString("lat", latA.get(ith));
editor.putString("lng", lngA.get(ith));
editor.apply();
Intent intent = new Intent(DashboardActivity.this, ShowOrderActivity.class);
DashboardActivity.this.startActivity(intent);
}
});
}
private void showJSON(String response){
String customerName;
String progress;
String address;
String dateTime;
String contact;
String lat;
String lng;
try {
JSONObject orderData = new JSONObject(response);
sampleObjectMap = new HashMap<>();
customerName = orderData.getString(Config.KEY_NAME);
sampleObjectMap.put("customerName", customerName);
customerA.add(customerName);
progress = orderData.getString(Config.KEY_PROGRESS);
sampleObjectMap.put("progress", progress);
progressA.add(progress);
address = orderData.getString(Config.KEY_ADDRESS);
sampleObjectMap.put("address", address);
addressA.add(address);
dateTime = orderData.getString(Config.KEY_DATE);
sampleObjectMap.put("dateTime", dateTime);
dateTimeA.add(dateTime);
contact = orderData.getString(Config.KEY_CONTACT);
sampleObjectMap.put("contact", contact);
contactA.add(contact);
lat = orderData.getString(Config.KEY_LAT);
sampleObjectMap.put("lat", lat);
latA.add(lat);
lng = orderData.getString(Config.KEY_LNG);
sampleObjectMap.put("lng", lng);
lngA.add(lng);
sampleArrayList.add(sampleObjectMap);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
包renava.com.user.renava.renava;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.Toast;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类DashboardActivity扩展了AppCompatActivity{
ListView工作列表;
公共阵列列表customerA、progressA、addressA、dateTimeA、contactA、jobIDA、latA、lngA;
SharedReferences.Editor;
布尔值=假;
SimpleAdapter样本列表适配器;
HashMap-sampleObjectMap;
ArrayList sampleArrayList=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_仪表板);
jobIDA=newarraylist();
customerA=newarraylist();
contactA=新的ArrayList();
addressA=新的ArrayList();
progressA=新的ArrayList();
dateTimeA=新的ArrayList();
latA=新的ArrayList();
lngA=新的ArrayList();
editor=getSharedReferences(“renavap”,MODE_PRIVATE).edit();
workList=(ListView)findViewById(R.id.workList);
int-id;
对于(id=1;id<300;id++){
String ID=String.valueOf(ID);
jobIDA.add(ID);
字符串url=Config.DATA\u url+ID;
Toast.makeText(getApplicationContext(),ID,Toast.LENGTH_LONG.show();
StringRequest StringRequest=新的StringRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
showJSON(响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(DashboardActivity.this,error.toString(),Toast.LENGTH_LONG.show();
}
});
RequestQueue RequestQueue=Volley.newRequestQueue(this);
添加(stringRequest);
酷=真;
workList.invalidateViews();
}
sampleListAdapter=new SimpleAdapter(Dashboard Activity.this,sampleArrayList,R.layout.array\u orders,新字符串[]{
“customerName”、“progress”、“address”、“dateTime”、“jobID”},新int[]{R.id.nameText,R.id.progressText
,R.id.locationText,R.id.jobIdText,R.id.timeText});
workList.setAdapter(sampleListAdapter);
workList.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView AdapterView,View-View,int-ith,long-l){
putString(“customerName”,customerA.get(ith));
putString(“progress”,progressA.get(ith));
editor.putString(“address”,addressA.get(ith));
putString(“dateTime”,dateTimeA.get(ith));
editor.putString(“contact”,contactA.get(ith));
editor.putString(“lat”,latA.get(ith));
编辑器.putString(“lng”,lngA.get(ith));
editor.apply();
意向意向=新意向(DashboardActivity.this、ShowOrderActivity.class);
DashboardActivity.this.startActivity(意图);
}
});
}
私有void showJSON(字符串响应){
字符串客户名称;
串进程;
字符串地址;
字符串日期时间;
弦接触;
弦拉特;
管柱液化天然气;
试一试{
JSONObject orderData=新JSONObject(响应);
sampleObjectMap=newHashMap();
customerName=orderData.getString(Config.KEY\u名称);
sampleObjectMap.put(“customerName”,customerName);
customerA.add(customerName);
progress=orderData.getString(Config.KEY\u progress);
sampleObjectMap.put(“progress”,progress);
进度a.添加(进度);
address=orderData.getString(Config.KEY\u地址);
sampleObjectMap.put(“地址”,address);
地址a.添加(地址);
dateTime=orderData.getString(Config.KEY\u DATE);
sampleObjectMap.put(“dateTime”,dateTime);
dateTimeA.add(dateTime);
contact=orderData.getString(Config.KEY\u contact);
sampleObjectMap.put(“contact”,contact);
联系人A.添加(联系人);
lat=orderData.getString(Config.KEY\u lat);
sampleObjectMap.put(“lat”,lat);
latA.添加(lat);
lng=orderData.getString(Config.KEY\u lng);
sampleObjectMap.put(“lng”,lng);
液化天然气(lng);
添加(sampleObjectMap);
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
仪表板的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="renava.com.user.renava.renavamechanic.DashboardActivity">
<ListView
android:id="@+id/workList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
数组\u命令文件,该文件是数组布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/nameText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="customerName"
android:textSize="@dimen/dgts__confirmation_code_text_size"
android:textStyle="bold"/>
<TextView
android:id="@+id/progressText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="progress"
android:textStyle="italic"
android:textAlignment="center"
android:textColor="@color/dgts__purple"
android:textSize="@dimen/dgts__confirmation_code_text_size" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_below="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal">
<TextView
android:id="@+id/locationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="address"
android:textSize="@dimen/dgts__confirmation_code_text_size" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_below="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/jobIdText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Job ID"
android:textSize="@dimen/dgts__confirmation_code_text_size" />
<TextView
android:id="@+id/timeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date and Time"
android:textStyle="bold"
android:textAlignment="viewEnd"
android:textSize="@dimen/dgts__confirmation_code_text_size" />
</LinearLayout>
</RelativeLayout>
我的问题是listview没有加载,只显示了一项。
我认为,如果在列表中包含200项或更多项的情况下运行一次,则加载此活动也将花费大量时间。
因此,如果有任何方式显示列表,即使列表不完整,但从服务器下载并自动添加到listView时会添加新内容。就像facebook或任何新闻应用程序一样。
提前感谢当您将
sampleArrayList
传递到适配器时,如果看到一个项目,您需要创建自定义适配器以实现您的目的。当它使用for循环从internet获取数据时,是否还会在listView中添加项目?逐一地?