kara
kara

Reputation: 123

i try to rectify this but i cannot

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

the error message is,

Error:(42, 19) error: constructor employ_list in class employ_list cannot be applied to given types; required: String,String found: Main7Activity,int,ArrayList reason: actual and formal argument lists differ in length Error:Execution failed for task ':app:compileDebugJavaWithJavac'. Compilation failed; see the compiler error output for details.

Upvotes: 2

Views: 150

Answers (2)

Jyoti JK
Jyoti JK

Reputation: 2171

The problem is , You are using wrong class

adapter = new employ_list (this, android.R.layout.simple_list_item_1, data);
  1. adapter is listAdapter object.

  2. You are initializing it with employ_list.

  3. employ_list constructor doesn't have the parameters you passed

replace it as

adapter = new listAdapter (this, data);

Upvotes: 4

Abdullah G
Abdullah G

Reputation: 157

You don't have a constructor employ_list class so you don't append list.

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

Upvotes: 0

Related Questions