2016-07-23 107 views
0

我正在使用JSON進行解析。在按鈕上單擊JSON解析開始並且listview將被填充。數據來自服務器,但listview沒有填充。代碼甚至沒有顯示任何錯誤。請檢查自定義ListView未在Android中填充項目

public class MainActivity extends AppCompatActivity { 

Button start; 
ListView listView; 
TextView textView; 
RequestQueue requestQueue; 
String[] deliverName = new String[100]; 
String[] deliverPrice = new String[100]; 
int[] deliverImage = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
     R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
     R.mipmap.ic_launcher, R.mipmap.ic_launcher,}; 
int total; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    start = (Button) findViewById(R.id.button); 
    //textView = (TextView) findViewById(R.id.textView); 
    requestQueue = Volley.newRequestQueue(this); 
    listView = (ListView) findViewById(R.id.listView2); 

    start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://deliverit.co.in/test.php?tablename=shakestable", 
        new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse(JSONObject response) { 
          try { 
           JSONArray jsonArray = response.getJSONArray("products"); 
           for (int i = 0; i < jsonArray.length(); i++) { 
            JSONObject products = jsonArray.getJSONObject(i); 
            deliverName[i] = products.getString("name"); 
            deliverPrice[i] = products.getString("price"); 
           } 
           Adapter adapter = new Adapter(getBaseContext(), deliverName, deliverPrice, deliverImage); 
           listView.setAdapter(adapter); 
           Log.e("TESTTTT", deliverName[2]); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
          Toast.makeText(MainActivity.this, "ERROR\n" + error.getMessage(), Toast.LENGTH_SHORT).show(); 
         } 
        } 
      ); 
      requestQueue.add(jsonObjectRequest); 
     } 
    }); 
} 
} 

Adapter.java

public class Adapter extends ArrayAdapter<String> { 

int[] imgs={}; 
String[] name={}; 
String[] price={}; 
Context c; 
LayoutInflater inflater; 
public Adapter(Context context, String[] name, String[] price, int[] imgs) { 
    super(context, R.layout.movies_row_layout); 

    this.c = context; 
    this.name = name; 
    this.price = price; 
    this.imgs = imgs; 
} 

public class ViewHolder 
{ 
    TextView deliver_name; 
    TextView deliver_price; 
    ImageView deliver_image; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView==null){ 
     inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.movies_row_layout, null); 
    } 
    //View Holder Object 
    final ViewHolder holder = new ViewHolder(); 
    holder.deliver_name = (TextView) convertView.findViewById(R.id.DELIVERname); 
    holder.deliver_price = (TextView) convertView.findViewById(R.id.DELIVERprice); 
    holder.deliver_image = (ImageView) convertView.findViewById(R.id.movie_poster); 

    //ASSIGN DATA 
    holder.deliver_image.setImageResource(imgs[position]); 
    holder.deliver_price.setText(price[position]); 
    holder.deliver_name.setText(name[position]); 

    return convertView; 
} 
} 
+0

確定請求成功嗎?你試圖調試什麼? –

+0

而且,個人而言,我會使用Arraylists而不是大小排列 –

+0

@ cricket_007'Log.e(「TESTTTT」,deliverName [2]);'輸出一個值。所以這個請求是成功的 – akkk

回答

2

您從ArrayAdapter擴展適配器,但是你不添加任何產品的適配器。此外,你把構造函數3不同的數組。我認爲,正確的方法是從BaseAdapter擴展適配器。但是,您可以修改您的構造函數:

public Adapter(Context context, String[] name, String[] price, int[] imgs) { 
    super(context, R.layout.movies_row_layout,names); 
              ^^^^^^^^^ 
... 

而您的存在適配器將工作。

0

創建自定義適配器,然後還需要重寫適配器類getCount()方法,該方法是在課堂延伸:

@Override 
public int getCount() { 
    return name.length; // return data-source size from here 
} 
0

其實你應該在響應監聽器下面創建你的隊列。 我希望它能起作用。 如果它不起作用,它可能源於你的PHP方面。

public class MainActivity extends AppCompatActivity { 

Button start; 
ListView listView; 
TextView textView; 
RequestQueue requestQueue; 
String[] deliverName = new String[100]; 
String[] deliverPrice = new String[100]; 
int[] deliverImage = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
     R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, 
     R.mipmap.ic_launcher, R.mipmap.ic_launcher,}; 
int total; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    start = (Button) findViewById(R.id.button); 
    //textView = (TextView) findViewById(R.id.textView); 
    requestQueue = Volley.newRequestQueue(this); 
    listView = (ListView) findViewById(R.id.listView2); 

    start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://deliverit.co.in/test.php?tablename=shakestable", 
        new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse(JSONObject response) { 
          try { 
           JSONArray jsonArray = response.getJSONArray("products"); 
           for (int i = 0; i < jsonArray.length(); i++) { 
            JSONObject products = jsonArray.getJSONObject(i); 
            deliverName[i] = products.getString("name"); 
            deliverPrice[i] = products.getString("price"); 
           } 
           Adapter adapter = new Adapter(getBaseContext(), deliverName, deliverPrice, deliverImage); 
           listView.setAdapter(adapter); 
           Log.e("TESTTTT", deliverName[2]); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
          Toast.makeText(MainActivity.this, "ERROR\n" + error.getMessage(), Toast.LENGTH_SHORT).show(); 
         } 
        } 
      ); 
      RequestQueue requestQueue = Volley.newRequestQueue(this); 
      requestQueue.add(jsonObjectRequest); 
     } 
    }); 
} 
} 
相關問題