Android动态更改Listview中textview的内容

时间:2015-10-29 22:48:32

标签: android listview textview

我想在创建后更改listview文本项的颜色。 如果状态aanwezig = 0 在创建简单适配器期间可以知道此状态。 但我不知道如何将textview更改为settextcolor(红色)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <!-- Bedrijf Label -->

    <TextView
        android:id="@+id/bedrijf"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"
        android:textColor="#43bd00"
        android:textSize="16sp"
        android:textStyle="bold" />

    <!-- Aanwezig label -->
    <TextView
        android:id="@+id/aanwezig"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:textColor="#acacac" />

</LinearLayout>

代码

package com.neel.jsonparsing;

        import java.io.BufferedInputStream;
        import java.io.BufferedReader;
        import java.io.InputStream;
        import java.io.InputStreamReader;
        import java.net.HttpURLConnection;
        import java.net.URL;
        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.Iterator;
        import java.util.Map;
        import java.util.Set;

        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;

        import android.app.ListActivity;
        import android.app.ProgressDialog;
        import android.content.Context;
        import android.content.Intent;
        import android.content.res.ColorStateList;
        import android.graphics.Color;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.AdapterView.OnItemClickListener;
        import android.widget.ArrayAdapter;
        import android.widget.ListAdapter;
        import android.widget.ListView;
        import android.widget.SimpleAdapter;
        import android.widget.TextView;
        import android.widget.Toast;

public class MainActivity extends ListActivity {

    private ProgressDialog pDialog;

    // JSON Node names
    private static final String TAG_RECORD   = "record";

    private static final String TAG_BEDRIJF  = "bedrijf";
    private static final String TAG_AANWEZIG = "aanwezig";

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

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

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

        ListView lv = getListView();

        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting values from selected ListItem
                String bedrijf = ((TextView) view.findViewById(R.id.bedrijf))
                        .getText().toString();
                String aanwezig = ((TextView) view.findViewById(R.id.aanwezig))
                        .getText().toString();

               int seclectedItem = position;

                {
                    ((TextView) view.findViewById(R.id.bedrijf)).setTextColor(Color.GREEN);

                }
//                else //
                {
                    ((TextView) view.findViewById(R.id.bedrijf)).setTextColor(Color.RED);
                }
            }
        });

        // Calling async task to get json
        new GetContacts().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // contacts JSONArray
            JSONArray records;
            JSONObject record;

            StringBuilder result = new StringBuilder();
            InputStream inputStream = null;

            try {
                URL url = new URL("http://android.php");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(10000);
                connection.setConnectTimeout(15000);
                connection.setRequestMethod("GET");
                connection.setUseCaches(false);
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setDoInput(true);

                InputStream in = new BufferedInputStream(connection.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                records = new JSONObject(result.toString()).getJSONArray("records");
                // get the 'posts' section from the JSON string
                for (int i = 0; i < records.length(); i++) {
                    record = records.getJSONObject(i).getJSONObject("record");

                    String bedrijf = record.getString("bedrijf");
                    String aanwezig = record.getString("aanwezig");

                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_BEDRIJF, bedrijf);
                    contact.put(TAG_AANWEZIG, aanwezig);

                    // adding contact to contact list
                    contactList.add(contact);
                }

                connection.disconnect();
                reader.close();

            } catch (JSONException e) {
                    e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, contactList,
                    R.layout.list_item, new String[] { TAG_BEDRIJF},
                    new int[] { R.id.bedrijf});

            setListAdapter(adapter);

0 个答案:

没有答案