2015-10-14 99 views
1

我呼籲我的資料和baseAdapter一個活動叫Myprofile_CustomView我活動的ArrayList的Hashmap的價值,我得到它,然後我轉換成ArrayList的一個HashMap和我的問題JSON數據我該如何檢索baseadapter中hashmap的值? 這是我的活動我的資料Android的我怎樣才能得到我的baseadapter

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

} 

這部分上面我沒有問題,我的問題是與ArrayList的用戶列表的BaseAdapter我不知道如何從它那裏得到的HashMap鍵。我命名的鑰匙,因爲我有其他領域,我最終會做

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


     } 

    } 


} 

回答

2

getCount應該回到你的數據集的大小。在你的情況usersList

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

INT getView要檢索的位置處的項目:

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

與滾動位置的值發生變化,