简体   繁体   中英

Android how can i get value of ArrayList Hashmap in my baseadapter

I have an Activity called Myprofile and a baseAdapter called Myprofile_CustomView on my activity I get Json data which then I convert into a ArrayList with a hashmap and my question is how can I retrieve the values of the hashmap in the baseadapter ? This is my activity Myprofile

public class Myprofile extends Activity {
    String URI_URL;
    Integer page;
    ProgressBar pb;
      ListView mm;
    Myprofile_CustomView BA;
    ArrayList<HashMap<String,String>> userslist;

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

        URI_URL = getResources().getString(R.string.PathUrl) + "/api/myprofile";
     page=0;

        // Listview for adapter
       mm= (ListView)findViewById(R.id.myprofiles);

        new Myprofile_Async().execute();
    }


   public class Myprofile_Async extends AsyncTask<String,String,String> {
        HttpURLConnection conn;
        URL url;
        String result="";
      DataOutputStream wr;
       int id;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pb=(ProgressBar)findViewById(R.id.progressBar);
            pb.setVisibility(View.VISIBLE);
            id= getIntent().getExtras().getInt("id");
            // page Int is used to keep count of scroll events
          if(page==0)
          {page=1;}
            else {page=page+1;}
            Toast.makeText(Myprofile.this,""+page,Toast.LENGTH_SHORT).show();
        }

        @Override
        protected String doInBackground(String... params) {
       // Gets data from api

            BufferedReader reader=null;
            String cert="id="+id+"&page="+page;
            try{
                url = new URL(URI_URL);
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                conn.connect();

                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);

                wr = new DataOutputStream(conn.getOutputStream());
                wr.writeBytes(cert);
                wr.flush();
                wr.close();



                reader =  new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder sBuilder = new StringBuilder();

                String line = "";
                while ((line = reader.readLine()) != null) {
                    sBuilder.append(line + "\n");
                }


                result = sBuilder.toString();
                reader.close();
                conn.disconnect();
                return result;



            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            System.err.println("cassies" + result);

            return result;

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                HashMap<String,String> map= new HashMap<>();

                JSONObject jsonn= new JSONObject(result);
                JSONArray jArray = jsonn.getJSONArray("myprofile");
                JSONObject jobject=null;
                JSONArray sss= new JSONArray();
                for(int i=0; i < jArray.length(); i++) {
                    jobject= jArray.getJSONObject(i);

                    map.put("fullname",jobject.getString("fullname"));
                    sss.put(jobject);

                }

                jsonn.put("myprofile", sss);
  // Add values to arrayList
                userslist.add(map);


           // Send information to BaseAdapter
            BA= new Myprofile_CustomView(userslist,Myprofile.this);
                mm.setAdapter(BA);

            } catch (Exception e) {
                System.err.println("mpee: " + e.toString());

            }
            pb.setVisibility(View.INVISIBLE);
        }
    }

}

this part above I have no issues with my problem is in the BaseAdapter with the ArrayList userList I don't know how to get HashMap keys from it. I am naming the keys because I have other fields that I will eventually do

public class Myprofile_CustomView extends BaseAdapter {

    JSONObject names;
    Context ctx;
    LayoutInflater myiflater;
 ArrayList<HashMap<String,String>> usersList;
// Have data come in and do a toast to see changes
    public Myprofile_CustomView(ArrayList<HashMap<String,String>> arr, Context c) {
        notifyDataSetChanged();
        ctx = c;
       usersList= arr;
        myiflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        try {
            JSONArray jaLocalstreams = names.getJSONArray("myprofile");
            return jaLocalstreams.length();
        } catch (Exception e) {
            Toast.makeText(ctx, "Error: Please try again", Toast.LENGTH_LONG).show();
            return names.length();
        }


    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row=convertView;
        MyViewHolder holder=null;

            try {
                if(row==null) {
                    LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               row = li.inflate(R.layout.zmyprofile,parent,false);
                    holder=new MyViewHolder(row);
                    row.setTag(holder);

            }
                else
                {
                    holder=(MyViewHolder)row.getTag();

                }


          // How can I get HashMap value for fullname here so I can set it to to Text
           String fullname=  usersList

               holder.fullname.setText(fullname);

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


    }

    class MyViewHolder{
        TextView fullname;

        MyViewHolder(View v)
        {
            fullname= (TextView)v.findViewById(R.id.fullname);


        }

    }


}

getCount should return the size of your dataset. In your case usersList

public int getCount() {
   return usersList == null ? 0 : userLists.size();
}

int getView you want to retrieve the item at position:

HashMap<String, String> item = usersList.get(i);
 String fullname = item.get("fullname");

the value of position changes with the scrolling,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM