如何在自定义arraylist中添加静态字段?

时间:2018-02-05 09:24:35

标签: java android json arraylist android-spinner

您好我在我的项目中使用json解析。我有以下响应,我将它设置为微调器。

import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Image
} from 'react-native';

const playIcon = require('./images/play.png');
const volumeIcon = require('./images/sound.png');
const hdIcon = require('./images/hd-sign.png');
const fullScreenIcon = require('./images/full-screen.png');
const remoteImage = { uri:'https://s3.amazonaws.com/crysfel/public/book/new-york.jpg' };

export default class App extends Component<{}> {
  render() {
    return (
      <Image source={remoteImage} style={styles.fullscreen}>
      <View style={styles.container}>
        <Image source={playIcon} style={styles.icon} />
        <Image source={volumeIcon} style={styles.icon} />
        <View style={styles.progress}>
          <View style={styles.progressBar} />
        </View>
        <Image source={hdIcon} style={styles.icon} />
        <Image source={fullScreenIcon} style={styles.icon} />
      </View>
    </Image>
    );
  }
}

const styles = StyleSheet.create({
  fullscreen: {
    flex: 1,
  },
  container: {
    position: 'absolute',
    backgroundColor: '#202020',
    borderRadius: 5,
    flexDirection: 'row',
    height: 50,
    padding: 5,
    paddingTop: 16,
    bottom: 30,
    right: 10,
    left: 10,
    borderWidth: 1,
    borderColor: '#303030',
  },
  icon: {
    tintColor: '#fff',
    height: 16,
    width: 16,
    marginLeft: 5,
    marginRight: 5,
  },
  progress: {
    backgroundColor: '#000',
    borderRadius: 7,
    flex: 1,
    height: 14,
    margin: 10,
    marginTop: 2,
  },
  progressBar: {
    backgroundColor: '#bf161c',
    borderRadius: 5,
    height: 10,
    margin: 2,
    width: 80,
  },
});

现在的问题是我想添加一个id并命名为static。 id:1,name:All

任何人都可以帮我解决这个问题吗?

{"success":1,"languages":[{"id":"2","name":"telugu"},{"id":"3","name":"English"},{"id":"4","name":"Tamil"}]}

3 个答案:

答案 0 :(得分:2)

从for循环中取出这一行

LanguageModel alldata = new LanguageModel();
 alldata.setLangid(("1"));
 alldata.setLangname(("All"));
 langList.add(0,alldata);

答案 1 :(得分:1)

这里的问题是你在for循环中添加id : 1, name : All会导致它被多次添加。这应该只添加一次。

for循环

中删除以下四行
LanguageModel alldata = new LanguageModel();
alldata.setLangid(("1"));
alldata.setLangname(("All"));
langList.add(0,alldata);

并放在for声明之上。

答案 2 :(得分:0)

您正在从循环和填充以及arraylist获取数据。只需删除此行即可。

LanguageModel alldata = new LanguageModel();
alldata.setLangid(("1"));
alldata.setLangname(("All"));
langList.add(0,alldata);