我从我连接到Mysql的php脚本获取Json记录。 PHP脚本一次发送500条记录。 然后将这些记录转换为数组,并生成包含所有500条记录的列表视图。 我希望listview一次显示20条记录,当用户到达listview的底部时,接下来应显示20条记录。
用于此活动的代码bieng是
public class ListActivity extends AppCompatActivity implements AsyncResponse, AdapterView.OnItemClickListener {
final String LOG = "ListActivity";
private ArrayList<Product> productList;
private ListView lvProduct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ImageLoader.getInstance().init(UILConfig.config(ListActivity.this));
PostResponseAsyncTask taskRead = new PostResponseAsyncTask(ListActivity.this, this);
taskRead.execute("http:///product.php");
}
@Override
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
BindDictionary<Product> dict = new BindDictionary<Product>();
dict.addStringField(R.id.tvName, new StringExtractor<Product>() {
@Override
public String getStringValue(Product product, int position) {
return product.name;
}
});
dict.addStringField(R.id.tvPrice, new StringExtractor<Product>() {
@Override
public String getStringValue(Product product, int position) {
return "" + product.v;
}
});
dict.addDynamicImageField(R.id.ivImage,
new StringExtractor<Product>() {
@Override
public String getStringValue(Product product, int position) {
return product.youtubeid;
}
}, new DynamicImageLoader() {
@Override
public void loadImage(String url, ImageView imageView) {
ImageLoader.getInstance().displayImage(url, imageView);
}
});
FunDapter<Product> adapter = new FunDapter<>(
ListActivity.this, productList, R.layout.layout_list, dict);
lvProduct = (ListView)findViewById(R.id.lvProduct);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Product selectedProduct = productList.get(position);
Intent in = new Intent(ListActivity.this, Detailactivity.class);
in.putExtra("product", (Serializable) selectedProduct);
startActivity(in);
}
}