gpt4 book ai didi

java - Android 如何在我的 baseadapter 中获取 ArrayList Hashmap 的值

转载 作者:行者123 更新时间:2023-11-29 20:21:22 25 4
gpt4 key购买 nike

在我的 Activity 中,我有一个名为 Myprofile 的 Activity 和一个名为 Myprofile_CustomView 的 baseAdapter 我获取 Json 数据,然后我将其转换为带有 HashMap 的 ArrayList,我的问题是如何我可以在 baseadapter 中检索 hashmap 的值吗?这是我的 Activity 我的个人资料

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


}

}


}

最佳答案

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

position的值随着滚动而变化,

关于java - Android 如何在我的 baseadapter 中获取 ArrayList Hashmap 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33134303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com