自定义列表适配器类中的错误

时间:2015-09-26 10:40:54

标签: android

我试图让服务器响应并将其存储在不同的字符串数组中但是当我调用适配器类到现在它很好但在适配器类中会给我错误。

    class viewticket extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pdialog = new ProgressDialog(UserLogedIn.this);
            pdialog.setMessage("Loading....");
            pdialog.setIndeterminate(false);
            pdialog.setCancelable(false);
            pdialog.show();
        }

        @Override
        protected String doInBackground(String... params)  {
            List<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("userid", u_id));


            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(URLMyTicket, ServiceHandler.POST, param);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null)

            {
                try {

                    contacts = new JSONArray(jsonStr);
                    int a=contacts.length();
                    Log.v(TAG,""+a);
                    String[] id = new String[contacts.length()];
                    String[] prob = new String[contacts.length()];
                    String[] desc = new String[contacts.length()];
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String aa = c.getString(TAG_ID);
                        String bb = c.getString(TAG_PROB);
                        String cc = c.getString(TAG_DESC);

                        Log.v(TAG, "TAG_ID" + aa);
                        Log.v(TAG,"TAGPROB"+bb);
                        Log.v(TAG,"TAGDESC"+cc);
                        id[i] = aa;
                        prob[i]=bb;
                        desc[i]=cc;
                        Log.v(TAG, "aaaaa" + id[i]);
                        Log.v(TAG,"bbbbb"+prob[i]);
                        Log.v(TAG,"ccc"+desc[i]);
                        Ticket_adapter adapter=new Ticket_adapter(this,id,prob,desc);
                        lv.setAdapter(adapter);
                    }
                } catch (JSONException e) {
                  System.out.print("hiiiiiiiiiiii" );
                    e.printStackTrace();
                }
            }
            if(jsonStr==null)
            {
               // Toast.makeText(UserLogedIn.this, "hiiiiiiiiiiiiiiiiiiiii", Toast.LENGTH_SHORT).show();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String  result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            pdialog.hide();
            pdialog.dismiss();
        }

    }

现在适配器类中的问题是它在super()方法中给出了错误     Ticket_adapter.java

    public class Ticket_adapter extends ArrayAdapter<String> {
    UserLogedIn.viewticket context;
    String[] id;
    String[] prob;
    String[] desc;


    public Ticket_adapter(UserLogedIn.viewticket context, String[] id,String[] prob,String[] desc) {
        super(context, R.id.list_item,id);//error cannot resolve method super
        this.context=context;
        this.id=id;
        this.prob=prob;
        this.desc=desc;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.my_list, null, true);

        TextView idtxt=(TextView)convertView.findViewById(R.id.uid);
        TextView probtxt=(TextView)convertView.findViewById(R.id.prob);
        TextView desctxt=(TextView)convertView.findViewById(R.id.ticket);

        idtxt.setText(id[position]);
        probtxt.setText(prob[position]);
        desctxt.setText(desc[position]);


        return convertView;

    }
}

错误:

  

错误:(21,9)错误:找不到合适的构造函数   ArrayAdapter(UserLogedIn.viewticket,int,String [])构造函数   ArrayAdapter.ArrayAdapter(Context,int,int)不适用(参数   不匹配; UserLogedIn.viewticket无法转换为Context)   构造函数ArrayAdapter.ArrayAdapter(Context,int,String [])不是   适用(参数不匹配; UserLogedIn.viewticket不能   转换为Context)构造函数   ArrayAdapter.ArrayAdapter(Context,int,List)不适用   (参数不匹配; UserLogedIn.viewticket无法转换为   上下文中)

2 个答案:

答案 0 :(得分:0)

问题是你在for循环中调用Adpater

试试这个

 String[] id = new String[contacts.length()];
                String[] prob = new String[contacts.length()];
                String[] desc = new String[contacts.length()];
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String aa = c.getString(TAG_ID);
                    String bb = c.getString(TAG_PROB);
                    String cc = c.getString(TAG_DESC);

                    Log.v(TAG, "TAG_ID" + aa);
                    Log.v(TAG,"TAGPROB"+bb);
                    Log.v(TAG,"TAGDESC"+cc);
                    id[i] = aa;
                    prob[i]=bb;
                    desc[i]=cc;
                    Log.v(TAG, "aaaaa" + id[i]);
                    Log.v(TAG,"bbbbb"+prob[i]);
                    Log.v(TAG,"ccc"+desc[i]);

                }//for loop ends

               if(id.length>0)
               {
                Ticket_adapter adapter=new Ticket_adapter(this,id,prob,desc);
                    lv.setAdapter(adapter);
               }
               else
                {
                  //toast which displays no tickets found
                }

答案 1 :(得分:0)

1)您需要在for循环之外创建适配器。

2)如果要在自定义适配器构造函数中调用super(),则需要传递Context而不是名为context的对象。

由于错误导致UserLogedIn.viewticket不是Context

查看官方文档,了解ArrayAdapter构造函数应该如何( Public Constructors 部分)。