我正在开发一个Android应用程序,该应用程序从站点下载信息并将其加载到可与之交互的阵列列表中。
我的问题是,由于某种原因我无法发现,用于构建ArrayList的一些变量无法正确初始化或为空。
以下是我的活动和课程:
Tenda.java(MainActivity)
public class Tenda extends Activity implements OnItemClickListener, LocationListener {
ProgressDialog lDialog;
String json_string;
ArticleAdapter adapter_article;
int user_id;
private LocationManager locationManager;
public String provider;
private String lattitude , longitude;
/*************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tenda);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
init_conf();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tenda, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_update:
updateList();
return true;
case R.id.menu_search:
alert_search();
return true;
case R.id.menu_cancel:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
TextView stock = (TextView) view.findViewById(R.id.list_product_stock);
int num_stock = Integer.parseInt(stock.getText().toString()) ;
if(num_stock!=0){
ArrayList<Article> productes=adapter_article.getItems_producte();
Article current_product= productes.get(position);
alert_comprar(num_stock,current_product);
}
else
{
Toast.makeText(this, "OUT OF STOCK", Toast.LENGTH_LONG).show();
}
}
private void init_conf(){
Bundle extras = getIntent().getExtras();
user_id = extras.getInt("user_id");
Toast.makeText(this, "user id"+Integer.toString(user_id), Toast.LENGTH_LONG).show();
ListView lsv_super = (ListView)findViewById(R.id.list_productes);
adapter_article = new ArticleAdapter(Tenda.this, null);
Log.i ("adapter-super", "" + adapter_article);
lsv_super.setAdapter(adapter_article);
lsv_super.setOnItemClickListener(this);
updateList();
}
private void updateList(){
loading();
String url = "http://www.v2msoft.com/clientes/lasalle/curs-android/products.php?user_id="+user_id;
boolean is_con=is_connected();
if(is_con){
AsyncTask(url);
Toast.makeText(this, "You're connected to Internet", Toast.LENGTH_LONG).show();
}
else{
json_string=storeRead();
refreshListByJson(json_string);
Toast.makeText(this, "You're not connected to Internet", Toast.LENGTH_LONG).show();
}
}
private void search(String text){
loading();
String url="http://www.v2msoft.com/clientes/lasalle/curs-android/search.php?user_id="+user_id+"&q="+text;
boolean is_con=is_connected();
if(is_con){
Toast.makeText(this, url, Toast.LENGTH_LONG).show();
searchAsyncTask(url);
}
else{
Toast.makeText(this, "NO CONNECTION", Toast.LENGTH_LONG).show();
}
}
private void comprar(Article article, int value){
int product_id= article.getId();
String url="http://www.v2msoft.com/clientes/lasalle/curs-android/buy.php?user_id="+user_id+
"&product_id="+product_id+
"&items="+value+
"&lat="+1+
"&long="+1;
Toast.makeText(this, url, Toast.LENGTH_LONG).show();
}
private void AsyncTask(String url){
LongAsyncTask task = new LongAsyncTask();
task.execute(url);
}
private void searchAsyncTask(String url){
SearchAsyncTask task = new SearchAsyncTask();
task.execute(url);
}
public void alert_search(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Searcher");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String m_Text = input.getText().toString();
Toast.makeText(getApplicationContext(), m_Text, Toast.LENGTH_LONG).show();
search(m_Text);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
public void alert_comprar(int max_value,final Article article){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Comprar");
final NumberPicker number= new NumberPicker(this);
number.setMaxValue(max_value);
number.setMinValue(0);
builder.setView(number);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int value= number.getValue();
Toast.makeText(getApplicationContext(), value+"", Toast.LENGTH_LONG).show();
comprar(article,value);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
/*************************ASYNC TASK************************/
public class LongAsyncTask extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... url) {
String url_result= conect(url[0]);
Log.i("DO IN BACKGROUND", url_result);
return url_result;
}
protected void onPostExecute(String url_result) {
Log.i("On Post EXECUTE", url_result);
json_string= url_result;
storeWrite(json_string);
refreshListByJson(json_string);//line 257
}
}
public class SearchAsyncTask extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... url) {
String url_result= conect(url[0]);
Log.i("Search result", url_result);
return url_result;
}
protected void onPostExecute(String url_result) {
Log.i("On Post EXECUTE SEARCH", url_result);
json_string= url_result;
Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();
refreshListByJson(json_string);
}
}
public class BuyAsyncTask extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... url) {
String url_result= conect(url[0]);
Log.i("buy result", url_result);
return url_result;
}
protected void onPostExecute(String url_result) {
Log.i("On Post EXECUTE BUY", url_result);
json_string= url_result;
Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();
}
}
/*************************************************/
private void refreshListByJson(String json){//line 300
lDialog.dismiss();
Store store=Store.newStore(json);
Log.i ("store", "" + store);
Log.i("store-nom", "" + store.getStore());//line 305
Log.i("store-producte", "" + store.getProductes().toString()); //line 306
//if (store.getProductes() != null && store.getProductes().size()>0)
//{
adapter_article.setItems_producte(store.getProductes());
//}
ListView lsv_producte = (ListView)findViewById(R.id.list_productes);
adapter_article = new ArticleAdapter(Tenda.this, store.getProductes());
lsv_producte.setAdapter(adapter_article);
Log.i ("adapter-article", "" + lsv_producte);
lsv_producte.setOnItemClickListener(this);
}
private void storeWrite(String data){
String FILENAME = "json_store";
FileOutputStream fos;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Write STORAGE", data);
}
private String storeRead(){
String FILENAME = "json_store";
FileInputStream fis;
StringBuilder fileContent = new StringBuilder();
Log.i ("file-content", "" + fileContent);
try {
fis= openFileInput (FILENAME);
BufferedReader br= new BufferedReader(new InputStreamReader(fis));
String line="";
while((line = br.readLine())!= null)
{
fileContent.append(line);
}
br.close();
fis.close();
Log.i("READ STORAGE", fileContent.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileContent.toString();
}
public String conect(String url_string){
HttpURLConnection con = null;
BufferedReader reader= null;
try{
URL url = new URL(url_string);
con = (HttpURLConnection)url.openConnection();
reader= new BufferedReader(new InputStreamReader(con.getInputStream()));
String line ="";
StringBuffer responseBuffer = new StringBuffer();
while((line=reader.readLine())!=null)
{
responseBuffer.append(line);
}
return responseBuffer.toString();
}
catch(Exception ex){
Log.e(getClass().getName(), ex.getMessage(),ex);
return null;
}
}
public boolean is_connected(){
ConnectivityManager conMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo i = conMgr.getActiveNetworkInfo();
if (i == null)
return false;
if (!i.isConnected())
return false;
if (!i.isAvailable())
return false;
return true;
}
private void loading(){
lDialog = new ProgressDialog(this);
lDialog.setMessage("Loading...");
lDialog.setCancelable(false);
lDialog.show();
}
/*** Location ***/
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
int lat = (int) (arg0.getLatitude());
int lng = (int) (arg0.getLongitude());
lattitude = "Lattitude: "+ lat ;
longitude = "Longitude: "+ lng;
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
////TODO Auto-generated method stub
}
}
Class Article.java
公共类文章{
private int id;
private String fabricante;
private String nombre;
private float precio;
private int stock;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFabricante() {
return fabricante;
}
public void setFabricante(String fabricante) {
this.fabricante = fabricante;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public float getPrecio() {
return precio;
}
public void setPrecio(float precio) {
this.precio = precio;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
类ArticleAdapter.java
public class ArticleAdapter extends BaseAdapter {
public ArrayList<Article> getItems_producte() {
return items_producte;
}
private Activity activity;
private ArrayList<Article> items_producte;
public ArticleAdapter(Activity activity, ArrayList<Article> items_producte){
this.activity = activity;
this.items_producte= items_producte;
}
@Override
public int getCount() {
if(items_producte==null){
return 0;
}
else{
return items_producte.size();
} }
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items_producte.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View contentView, ViewGroup Parent) {
// TODO Auto-generated method stub
View view = contentView;
if(view==null){
LayoutInflater inflate =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflate.inflate(R.layout.info_product, null,false);
}
Article item_producte = items_producte.get(position);
TextView id = (TextView)view.findViewById(R.id.list_product_id);
TextView fabricant = (TextView)view.findViewById(R.id.list_product_fabricant);
TextView nom = (TextView)view.findViewById(R.id.list_product_nom);
TextView preu = (TextView)view.findViewById(R.id.list_product_preu);
TextView stock = (TextView)view.findViewById(R.id.list_product_stock);
id.setText(Integer.toString(item_producte.getId()));
fabricant.setText(item_producte.getFabricante());
nom.setText(item_producte.getNom());
preu.setText(Float.toString(item_producte.getPreu()));
stock.setText(Integer.toString(item_producte.getStock()));
fabricant.setTextColor(activity.getResources().getColor(R.color.fabricant));
nom.setTextColor(activity.getResources().getColor(R.color.fabricant));
preu.setTextColor(activity.getResources().getColor(R.color.fabricant));
stock.setTextColor(activity.getResources().getColor(R.color.fabricant));
if(position%2==0){
view.setBackgroundColor(activity.getResources().getColor(R.color.files_parelles));
}
else{
view.setBackgroundColor(activity.getResources().getColor(R.color.files_imparelles));
}
/*STOCK COLOR*/
if(item_producte.getStock()<=0){
view.setBackgroundColor(activity.getResources().getColor(R.color.stock_null_bg));
fabricant.setTextColor(activity.getResources().getColor(R.color.stock_null_tx));
nom.setTextColor(activity.getResources().getColor(R.color.stock_null_tx));
preu.setTextColor(activity.getResources().getColor(R.color.stock_null_tx));
stock.setTextColor(activity.getResources().getColor(R.color.stock_null_tx));
}
return view;
}
public void setItems_producte(ArrayList<Article> items_producte) {
this.items_producte = items_producte;
this.notifyDataSetChanged();
}
}
类LongAsyncTask.java
public class LongAsyncTask extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... arg0) {
return null;
}
protected void onPostExecute(String result) {
}
}
Class Store.java
import java.util.ArrayList;
导入com.google.gson.Gson;
公共类商店{
private String supermercado;
private ArrayList<Article> productes;
public String getSupermercado() {
return supermercado;
}
public void setSupermercado(String supermercado) {
this.supermercado = supermercado;
}
public ArrayList<Article> getProductes() {
return productes;
}
public void setProductes(ArrayList<Article> productes) {
this.productes = productes;
}
static Store newStore(String json_string){
Gson gson= new Gson();
Store store = gson.fromJson(json_string,Store.class);
return store;
}
}
Logcat输出
04-29 08:54:14.254: I/DO IN BACKGROUND(1124): {"succeed":true,"user_id":362}
04-29 08:54:14.265: I/MESSAGE(1124): {"succeed":true,"user_id":362}
04-29 08:54:14.324: I/put Extra user id(1124): 362
04-29 08:54:19.495: I/adapter-super(1124): com.example.shop.ArticleAdapter@416f9d00
04-29 08:54:24.904: I/DO IN BACKGROUND(1124): { "supermercado": "La Salle - Curs Android II", "productos" : [
{"id" : 1, "fabricante" : "Nestlé", "nombre" : "NESQUIK", "precio" : 12.85, "stock" : 10 }, { "id" : 2, "fabricante" : "Coca-cola", "nombre" : "Coca-cola", "precio" : 3.21, "stock" : 150 }, { "id" : 3, "fabricante" : "Borges", "nombre" : "Aceite girasol 1L", "precio" : 1.88, "stock" : 0 }, { "id" : 4, "fabricante" : "Serpis", "nombre" : "Aceitunas rellenas de anchoa 160g", "precio" : 1.39, "stock" : 1523 }, { "id" : 5, "fabricante" : "Carretilla", "nombre" : "Pimientos piquillo ajillo 290g", "precio" : 2.25, "stock" : 14 }, { "id" : 6, "fabricante" : "Orlando", "nombre" : "Tomate frito con aceite de oliva virgen brik 390g", "precio" : 0.95, "stock" : 0 }, { "id" : 7, "fabricante" : "Kellogg's", "nombre" : "Cereales Special K chocolate 300g", "precio" : 2.39, "stock" : 152 }, { "id" : 8, "fabricante" : "Lu", "nombre" : "Galletas rellenas mini Principe 160g", "precio" : 1.59, "stock" : 29 }, { "id" : 9, "fabricante" : "Pascual", "nombre" : "Leche sin lactosa desnatada brik 1l", "precio" : 1.49, "stock" : 1596 }, { "id" : 10, "fabricante" : "Danone", "nombre" : "Danet vainilla 4x125g", "precio" : 1.90, "stock" : 856 }, { "id" : 11, "fabricante" : "Hornimans", "nombre" : "Infusueños 20 sobres", "precio" : 2.35, "stock" : 598 } ]}
04-29 08:54:26.394: I/On Post EXECUTE(1124): { "supermercado": "La Salle - Curs Android II", "productos" : [ { "id" : 1, "fabricante" : "Nestlé", "nombre" : "NESQUIK", "precio" : 12.85, "stock" : 10 }, { "id" : 2, "fabricante" : "Coca-cola", "nombre" : "Coca-cola", "precio" : 3.21, "stock" : 150 }, { "id" : 3, "fabricante" : "Borges", "nombre" : "Aceite girasol 1L", "precio" : 1.88, "stock" : 0 }, { "id" : 4, "fabricante" : "Serpis", "nombre" : "Aceitunas rellenas de anchoa 160g", "precio" : 1.39, "stock" : 1523 }, { "id" : 5, "fabricante" : "Carretilla", "nombre" : "Pimientos piquillo ajillo 290g", "precio" : 2.25, "stock" : 14 }, { "id" : 6, "fabricante" : "Orlando", "nombre" : "Tomate frito con aceite de oliva virgen brik 390g", "precio" : 0.95, "stock" : 0 }, { "id" : 7, "fabricante" : "Kellogg's", "nombre" : "Cereales Special K chocolate 300g", "precio" : 2.39, "stock" : 152 }, { "id" : 8, "fabricante" : "Lu", "nombre" : "Galletas rellenas mini Principe 160g", "precio" : 1.59, "stock" : 29 }, { "id" : 9, "fabricante" : "Pascual", "nombre" : "Leche sin lactosa desnatada brik 1l", "precio" : 1.49, "stock" : 1596 }, { "id" : 10, "fabricante" : "Danone", "nombre" : "Danet vainilla 4x125g", "precio" : 1.90, "stock" : 856 }, { "id" : 11, "fabricante" : "Hornimans", "nombre" : "Infusueños 20 sobres", "precio" : 2.35, "stock" : 598 } ]}
04-29 08:54:26.534: I/Write STORAGE(1124): { "supermercado": "La Salle - Curs Android II", "productos" : [ { "id" : 1, "fabricante" : "Nestlé", "nombre" : "NESQUIK", "precio" : 12.85, "stock" : 10 }, { "id" : 2, "fabricante" : "Coca-cola", "nombre" : "Coca-cola", "precio" : 3.21, "stock" : 150 }, { "id" : 3, "fabricante" : "Borges", "nombre" : "Aceite girasol 1L", "precio" : 1.88, "stock" : 0 }, { "id" : 4, "fabricante" : "Serpis", "nombre" : "Aceitunas rellenas de anchoa 160g", "precio" : 1.39, "stock" : 1523 }, { "id" : 5, "fabricante" : "Carretilla", "nombre" : "Pimientos piquillo ajillo 290g", "precio" : 2.25, "stock" : 14 }, { "id" : 6, "fabricante" : "Orlando", "nombre" : "Tomate frito con aceite de oliva virgen brik 390g", "precio" : 0.95, "stock" : 0 }, { "id" : 7, "fabricante" : "Kellogg's", "nombre" : "Cereales Special K chocolate 300g", "precio" : 2.39, "stock" : 152 }, { "id" : 8, "fabricante" : "Lu", "nombre" : "Galletas rellenas mini Principe 160g", "precio" : 1.59, "stock" : 29 }, { "id" : 9, "fabricante" : "Pascual", "nombre" : "Leche sin lactosa desnatada brik 1l", "precio" : 1.49, "stock" : 1596 }, { "id" : 10, "fabricante" : "Danone", "nombre" : "Danet vainilla 4x125g", "precio" : 1.90, "stock" : 856 }, { "id" : 11, "fabricante" : "Hornimans", "nombre" : "Infusueños 20 sobres", "precio" : 2.35, "stock" : 598 } ]}
04-29 08:54:28.634: I/store(1124): com.example.shop.Store@41860630
04-29 08:54:28.634: I/store-nom(1124): null
04-29 08:54:28.667: I/get-productes(1124): null
04-29 08:54:28.667: I/get-productes(1124): null
04-29 08:54:28.694: I/adapter-article(1124): android.widget.ListView{416e41e8 V.ED.VC. ......ID 0,0-480,0 #7f090004 app:id/list_productes}
问题的核心位于&#34; refreshListbyJson&#34; Tenda.java中的函数:在那里使用的大多数变量都是空白或空。
其中一个类中是否存在导致这些变量未正确初始化的问题? Logcat显示应用程序可以下载Internet信息并将其存储在存储中。
非常感谢你的帮助。
答案 0 :(得分:1)
json键与对象字段不匹配。例如:json返回productos但是在Store.java中,arraylist名称是productes ..如果你将这个名称改为productos(&#39; o&#39;而不是&#39; e&#39;)它将起作用。同样,文章类中也存在名称不匹配,例如&#34; nom&#34;在类字段和json对象中有&#34; nombre&#34;。希望这会有所帮助。
<强> Store.java 强>
import java.util.ArrayList;
import com.google.gson.Gson;
public class Store {
private String supermercado;
private ArrayList<Article> productes;
public String getSupermercado() {
return supermercado;
}
public void setSupermercado(String supermercado) {
this.supermercado = supermercado;
}
public ArrayList<Article> getProductes() {
return productes;
}
public void setProductes(ArrayList<Article> productes) {
this.productes = productes;
}
static Store newStore(String json_string){
Gson gson= new Gson();
Store store = gson.fromJson(json_string,Store.class);
return store;
}
}
<强> Article.java 强>
public class Article {
private int id;
private String fabricant;
private String nombre;
private float precio;
private int stock;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFabricant() {
return fabricant;
}
public void setFabricant(String fabricant) {
this.fabricant = fabricant;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public float getPrecio() {
return precio;
}
public void setPrecio(float precio) {
this.precio = precio;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}