Java 如何在地图中获取值<>;?
我试图在Android中使用Map获得以下JsonJava 如何在地图中获取值<>;?,java,android,baseadapter,Java,Android,Baseadapter,我试图在Android中使用Map获得以下Json "effect_list": [{ "1":[ { "effects_id":"1", "effects_name":"Band 1" }, { "effects_id":"2", "effects_name":"Band 2" }, {
"effect_list": [{
"1":[
{
"effects_id":"1",
"effects_name":"Band 1"
},
{
"effects_id":"2",
"effects_name":"Band 2"
},
{
"effects_id":"3",
"effects_name":"Band 3"
}
],
"2": [
{
"effects_id":"4",
"effects_name":"Background Blur"
},
{
"effects_id":"5",
"effects_name":"Blemish Removal"
}
],
"3": [
{
"effects_id":"6",
"effects_name":"Background Blur"
},
{
"effects_id":"7",
"effects_name":"Blemish Removal"
}
]
}]
}
我正在尝试使用BaseAdapter在Listview中显示Map的值。我可以通过getItem(position.get(“1”).get(0)使用它的键成功地获得一个特定的值。如何获取地图中的所有值
public class MyContactAdapter2 extends BaseAdapter {
List<Map<String, List<EffectList>>> contactList;
Context context;
private LayoutInflater mInflater;
int size = 0;
ViewHolder vh;
// Constructors
public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
contactList = objects;
}
public MyContactAdapter2() {
System.out.println("hai");
}
@Override
public int getCount() {
int count = contactList.size();
System.out.println("Count size" + count);
return count;
}
@Override
public Map<String, List<EffectList>> getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println(10);
final MyContactAdapter2.ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = MyContactAdapter2.ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (MyContactAdapter2.ViewHolder) convertView.getTag();
}
EffectList item = getItem(position).get("1").get(0);
vh.textViewName.setText(item.getEffectsId());
vh.textViewEmail.setText(item.getEffectsName());
return vh.rootView;
}
private static class ViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
private ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static MyContactAdapter2.ViewHolder create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new MyContactAdapter2.ViewHolder(rootView, imageView, textViewName, textViewEmail);
}
}
}
公共类MyContactAdapter2扩展BaseAdapter{
列出联系人名单;
语境;
私人停车场;
int size=0;
视窗座;
//建设者
公共MyContactAdapter2(上下文、列表对象){
this.context=上下文;
this.mInflater=LayoutInflater.from(上下文);
联系人列表=对象;
}
公共MyContactAdapter2(){
系统输出打印号(“hai”);
}
@凌驾
public int getCount(){
int count=contactList.size();
系统输出打印项次(“计数大小”+计数);
返回计数;
}
@凌驾
公共地图getItem(内部位置){
返回联系人列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
系统输出打印Ln(10);
最终MyContactAdapter2.ViewHolder vh;
if(convertView==null){
视图视图=mInflater.flate(R.layout.get\u layout\u row\u视图,父视图,false);
vh=MyContactAdapter2.ViewHolder.create((RelativeLayout)视图);
视图.设置标签(vh);
}否则{
vh=(MyContactAdapter2.ViewHolder)convertView.getTag();
}
EffectList项=getItem(位置).get(“1”).get(0);
vh.textViewName.setText(item.getEffectsId());
vh.textViewEmail.setText(item.getEffectsName());
返回vh.rootView;
}
私有静态类视图持有者{
公共最终相对论Yout rootView;
公共最终图像视图;
公共最终文本视图文本视图名称;
公开最终文本查看文本查看电子邮件;
私有视图持有者(RelativeLayout rootView、ImageView ImageView、TextView textViewName、TextView textViewEmail){
this.rootView=rootView;
this.imageView=imageView;
this.textViewName=textViewName;
this.textViewEmail=textViewEmail;
}
公共静态MyContactAdapter2.ViewHolder创建(RelativeLayout rootView){
ImageView ImageView=(ImageView)rootView.findViewById(R.id.ImageView);
TextView textViewName=(TextView)rootView.findViewById(R.id.textViewName);
TextView textViewEmail=(TextView)rootView.findViewById(R.id.textViewEmail);
返回新的MyContactAdapter2.ViewHolder(rootView、imageView、textViewName、textViewEmail);
}
}
}
使用上述代码,我能够获得效果id:“1”,“效果名称”:“波段1”
如何获取“1”、“2”和“3”的效果id和效果名称您可以从getItem方法获取第一个位置的效果列表,并获取“1”、“2”和“3”的效果id和效果名称的数据 比如:
Map<String, List<EffectList>> itemListByNumber = myContactAdapter2.getItem(0);
List<EffectList> effectListFor1 = itemListByNumber.get("1");
Map itemListByNumber=myContactAdapter2.getItem(0);
List effectListFor1=itemListByNumber.get(“1”);
从effectListFor1可以获得所需的值。使用以下代码
JsonArray jsonArray = get Jsonarray with key "effect_list"
for (int i=0; i < jsonArray.size(); i++ ){
JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet())
{
String key = entry.getKey(); // will return the 1 for i ==0
if (entry.getValue().isJsonObject()){
JsonArray values = entry.getValue().getAsJsonArray();
for (int j=0; j < values.size(); j++ ){
String effects_id = values.get(j).getAsJsonObject().get("effects_id").getAsString(); // will return the 1 for j ==0
String effects_name = values.get(j).getAsJsonObject().get("effects_name").getAsString(); // will return the Band 1 for j ==0
}
}
}
}
JsonArray JsonArray=使用键“effect\u list”获取JsonArray
for(int i=0;i
我正在使用Gson库!
只需在app-levelbuild.gradle中编写compile'com.google.code.gson:gson:2.6.2'
,就可以更具体了
Map<String, List<EffectList>> itemListByNumber = myContactAdapter2.getItem(0);
for(Map.Entry<Integer, List<EffectList>> s : itemListByNumber.entrySet())
System.out.print(s.getKey());
for (int i=0; i < s.getValue().size(); i ++{
// get the data one by one
}
Map itemListByNumber=myContactAdapter2.getItem(0);
对于(Map.Entry s:itemListByNumber.entrySet())
System.out.print(s.getKey());
对于(int i=0;i