我试图纠正这个但我不能

时间:2018-04-02 08:30:56

标签: java android

employ_list.java

public class employ_list {
    String name;
    String username;

    public employ_list(String name, String username) {
        this.name = name;
        this.username = username;
    }

    public String getname() {
        return name;
    }

    public String getusername() {
        return username;
    }
}

listAdapter.java

public class listAdapter extends BaseAdapter {
    private Context mContext;
    ViewHolder holder;
    ArrayList<employ_list> arrEmps;

    public listAdapter(Context c, ArrayList<employ_list> map) {
        mContext = c;
        arrEmps = map;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arrEmps.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View grid = convertView;

        if (convertView == null) {
            holder = new ViewHolder();
            grid = inflater.inflate(R.layout.customlist, null);
            grid.setMinimumHeight(150);
            holder.name = (TextView) grid.findViewById(R.id.name);
            holder.username = (TextView) grid.findViewById(R.id.username);
            grid.setTag(holder);
        } else {
            holder = (ViewHolder) grid.getTag();
        }

        holder.name.setText(arrEmps.get(position).getname());
        holder.username.setText(arrEmps.get(position).getusername());

        return grid;
    }

    class ViewHolder {
        TextView name;
        TextView username;
    }

}    

Main7Activity.java

public class Main7Activity extends AppCompatActivity {
    ListView lv;
    ArrayList<employ_list> data;
    listAdapter adapter;
    String address = "http://192.168.0.106/test/select1.php";
    InputStream is = null;
    String line = null;
    String result = null;
    //  String[] data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);

        lv = (ListView) findViewById(R.id.listview1);

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
        getData();
        adapter = new employ_list (this, android.R.layout.simple_list_item_1, data);
        lv.setAdapter(adapter);
    }

    private void getData()
    {
        try{
            URL url = new URL(address);
            HttpURLConnection con=(HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            is = new BufferedInputStream(con.getInputStream());
        } catch(Exception e){
            e.printStackTrace();
        }

        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            while((line=br.readLine()) != null)
            {
                sb.append(line+"\n");
            }
            is.close();
            result = sb.toString();

        }catch(Exception e){
            e.printStackTrace();
        }

        try{
            data = new ArrayList<>();

            JSONObject jo = new JSONObject(result);
            JSONArray ja = jo.getJSONArray("flag");

           // JSONArray ja = new JSONArray(result);
            //JSONObject jo = null;
            employ_list item = null;
            //data = new String[ja.length()];
            for(int i=0;i<ja.length();i++)
            {
                jo = ja.getJSONObject(i);
                item = new employ_list (jo.getString("name"), jo.getString("username"));
                data.add(item);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

错误信息是,

  

错误:(42,19)错误:类employees_list中的构造函数employ_list不能应用于给定的类型;       required:String,String       发现:Main7Activity,int,ArrayList       原因:实际和正式的参数列表长度不同       错误:任务&#39;:app:compileDebugJavaWithJavac&#39;执行失败。   编译失败;有关详细信息,请参阅编译器错误输出。

2 个答案:

答案 0 :(得分:4)

问题是,您使用了错误的类

adapter = new employ_list (this, android.R.layout.simple_list_item_1, data);
  1. adapter是listAdapter对象。

  2. 您正在使用employ_list初始化它。

  3. employ_list构造函数没有您传递的参数

  4. 将其替换为

    adapter = new listAdapter (this, data);
    

答案 1 :(得分:0)

您没有构造函数employ_list类,因此您不会附加列表。

    adapter = new employ_list (this,android.R.layout.simple_list_item_1, data);