自定义ListView没有填充Android中的项目

时间:2016-07-23 19:52:38

标签: android json listview android-json

我正在使用JSON进行解析。在按钮上单击JSON解析开始,将填充listview。数据来自服务器,但列表视图没有填充。代码甚至没有显示任何错误。请检查

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;
}
}

3 个答案:

答案 0 :(得分:2)

您从ArrayAdapter扩展了适配器,但是您没有向适配器添加任何项目。此外,您将构造函数3放入不同的数组。我认为,正确的方法是从BaseAdapter扩展您的适配器。但是,你可以修改你的构造函数:

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

您的存在适配器将起作用。

答案 1 :(得分:0)

创建自定义适配器时,还需要覆盖在类中扩展的Adapter类的varchar方法:

getCount()

答案 2 :(得分:0)

实际上,您应该在响应侦听器下创建队列。 我希望它能奏效。 如果它不起作用,它可能源于你的php方面。 喜欢 公共类MainActivity扩展AppCompatActivity { 按钮开始; 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,}; 总计; @覆盖 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(){         @覆盖         public void onClick(查看v){             JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,&#34; http://deliverit.co.in/test.php?tablename = shakestable&#34;,                     新的Response.Listener&lt; JSONObject&gt;(){                         @覆盖                         public void onResponse(JSONObject response){                             尝试{                                 JSONArray jsonArray = response.getJSONArray(&#34; products&#34;);                                 for(int i = 0; i&lt; jsonArray.length(); i ++){                                     JSONObject产品= jsonArray.getJSONObject(i);                                     deliverName [i] = products.getString(&#34; name&#34;);                                     deliverPrice [i] = products.getString(&#34; price&#34;);                                 }                                 适配器适配器=新适配器(getBaseContext(),deliverName,deliverPrice,deliverImage);                                 listView.setAdapter(适配器);                                 Log.e(&#34; TESTTTT&#34;,deliverName [2]);                             } catch(JSONException e){                                 e.printStackTrace();                             }                         }                     },                     新的Response.ErrorListener(){                         @覆盖                         public void onErrorResponse(VolleyError error){                             Toast.makeText(MainActivity.this,&#34; ERROR \ n&#34; + error.getMessage(),Toast.LENGTH_SHORT).show();                         }                     }             );              RequestQueue requestQueue = Volley.newRequestQueue(this);             requestQueue.add(jsonObjectRequest);         }     }); } }