0

嗨,我是 android 的新手,我尝试使用 rgb 颜色代码从数据库中显示颜色,我使用 hashmap 来映射数据和字段数据将出现,但我在 db 中有 rgb 颜色代码它会正确出现我的问题是它会仅显示 db 中的最后一个颜色值。如何在文本字段中获取所有值和颜色修复。

public class AndroidJSONParsingActivity extends Activity {
static String items;
static String items1;
static String dates;
static String finalDate;
int a,r,g,b;
ListView lv2;
TextView text1;
String COLOR;
private static final String TAG_JSONDataResult = "JSONDataResult";  
private static final String TAG_GRPNAME = "GRPNAME";
private static final String TAG_QNT = "QTY";
private static final String TAG_BUDGET = "BUDGET";
private static final String TAG_STOCK = "STOCK";
private static final String TAG_DIFF = "DIFF";
private static final String TAG_DIFF_P = "DIFF_P";

private static final String TAG_COLOR = "COLOR";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lv2=(ListView)findViewById(R.id.list);
    Bundle b=this.getIntent().getExtras();
    items=b.getString("item");
    items1=b.getString("item1");
    dates=b.getString("date");

    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd");
    Date myDate = null;
    try {
        myDate = dateFormat.parse(dates);

    } catch (ParseException e) {
        e.printStackTrace();
    }

    SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
     finalDate = timeFormat.format(myDate);

    torun();

}

public void torun() {
    String myUrl;
    myUrl = String.format("http://10.0.2.2:51382/RestServiceImpl.svc/jsons/?Location=%s&GROUP=%s&asondate=%s",items.replaceAll(" ","%20"),items1.replaceAll(" ","%20"),finalDate);

    final ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();
     // getting JSON string from URL

    try {

        JSONObject json1 = jParser.getJSONFromUrl(myUrl);
        // Getting Array of Contacts
        JSONArray list = json1.getJSONArray(TAG_JSONDataResult);


        // looping through All Contacts
        for(int i = 0; i < list.length(); i++){
            JSONObject c = list.getJSONObject(i);

            String GRPNAME = c.getString(TAG_GRPNAME);
            String QTY = c.getString(TAG_QNT);
            String BUDGET = c.getString(TAG_BUDGET);
            String STOCK = c.getString(TAG_STOCK);
            String DIFF = c.getString(TAG_DIFF);
            String DIFF_P = c.getString(TAG_DIFF_P);

             COLOR = c.getString(TAG_COLOR);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            map.put(TAG_GRPNAME, GRPNAME);

            map.put(TAG_QNT, QTY);
            map.put(TAG_BUDGET, BUDGET);
            map.put(TAG_STOCK, STOCK);
            map.put(TAG_DIFF, DIFF);
            map.put(TAG_DIFF_P, DIFF_P);

            map.put(TAG_COLOR,COLOR);

            dataList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    /**
     * Updating parsed JSON data into ListView
     * */
     ListAdapter adapter = new SimpleAdapter(this, dataList,
            R.layout.list_item,
            new String[] {  TAG_GRPNAME, TAG_QNT, TAG_BUDGET, TAG_STOCK, TAG_DIFF, TAG_DIFF_P, },
            new int[] {
                     R.id.l2, R.id.l3, R.id.l4, R.id.l5, R.id.l6, R.id.l7}){
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
             View v = convertView;
             if (v == null) {
                 LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 v = vi.inflate(R.layout.list_item, null);
             }
              text1 = (TextView) v.findViewById(R.id.l7);


             HashMap<String, String> map=dataList.get(position);
             map.get(TAG_COLOR);
             String[] ARGB = COLOR.split(" ");
             String V1=ARGB[0];
             String V2=ARGB[1];
             String V3=ARGB[2];
             String V4=ARGB[3];
             a=Integer.parseInt(V1);
             r=Integer.parseInt(V2);
             g=Integer.parseInt(V3);
             b=Integer.parseInt(V4);
             text1.setBackgroundColor(Color.rgb(r,g,b));

             return super.getView(position, v, parent);
         }
     };

            lv2.setAdapter(adapter);

    // Launching new screen on Selecting Single ListItem

}
4

2 回答 2

0

尝试在 for 循环之外声明 Map 并每次都对其进行初始化。这就是为什么您不断获得相同价值的原因。

        HashMap<String, String> map = new HashMap<String, String>();
        for(int i = 0; i < list.length(); i++){
        //
        //
        ...
        //

        map = new HashMap<String, String>();

        map.put(TAG_GRPNAME, GRPNAME);

        map.put(TAG_QNT, QTY);
        map.put(TAG_BUDGET, BUDGET);
        map.put(TAG_STOCK, STOCK);
        map.put(TAG_DIFF, DIFF);
        map.put(TAG_DIFF_P, DIFF_P);

        map.put(TAG_COLOR,COLOR);
        //map.put(TAG_PHONE_MOBILE, mobile);

        // adding HashList to ArrayList
        dataList.add(map);
于 2013-07-22T10:19:25.750 回答
0

您可以在使用datatList 设置适配器时在contactList 中添加hashmaps?

于 2013-07-22T10:20:24.053 回答