0

我是 android 新手,我在数据库中有一个表,我想使用 json 解析器更改 url 并检索程序,然后输出就会出现。

我的问题是如何根据值 ex 更改背景颜色(quantity(qny)0 到 10 表示背景颜色为红色,11 到 30 表示背景颜色为绿色,31 到 50 表示背景颜色为蓝色)。

代码:

public class AndroidJSONParsingActivity extends ListActivity {


private static String url = "http://10.0.2.2:51382/RestServiceImpl.svc/json/123";

/* JSON Node names */
private static final String TAG_CONTACTS = "JSONDataResult";
private static final String TAG_ID = "Id";
private static final String TAG_NAME = "Name";

private static final String TAG_MRP = "Mrp";
private static final String TAG_QNT = "qty";

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

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        JSONArray contacts = json.getJSONArray("JSONDataResult");


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

            // Storing each json item in variable
            String Id = c.getString(TAG_ID);
            String Name = c.getString(TAG_NAME);
            //String email = c.getString("Age");
            String Mrp = c.getString(TAG_MRP);
            String qnt = c.getString(TAG_QNT);
        //  String address = c.getString(TAG_ADDRESS);
        //String gender = c.getString(TAG_GENDER);

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

            // adding each child node to HashMap key => value
            map.put(TAG_ID, Id);
            map.put(TAG_NAME, Name);
            map.put(TAG_MRP, Mrp);
            map.put(TAG_QNT, qnt);
            //map.put(TAG_PHONE_MOBILE, mobile);

            // adding HashList to ArrayList
        contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(this, contactList,
            R.layout.list_item,
            new String[] { TAG_ID, TAG_NAME, TAG_MRP,  TAG_QNT }, new int[] {
                    R.id.id, R.id.name, R.id.mrp, R.id.qnt });

    setListAdapter(adapter);
  }
}
4

1 回答 1

0

只需尝试本教程,因为它使用数组适配器动态更改图像,并根据您的情况检查提及布局更改它的背景。

http://www.mkyong.com/android/android-listview-example/

于 2013-06-20T05:53:04.273 回答