如何在异步任务中将arraylist值设置为另一个方法

时间:2015-01-31 10:12:41

标签: android

我想将asynk任务方法中的成员列表值设置为onresume方法但是我得到null值我是如何得到这个值的?

这是我的代码。

    public class IndexBar extends Activity implements OnItemClickListener {

        HashMap<Character, Integer> alphabetToIndex;
        static String TAG = "IndexBar";
        int number_of_alphabets = -1;
        static IndexBarHandler handler;
        private EditText editsearch;
        private ArrayAdapter<String> adapter1;
        public  ArrayAdapter<String> mainadapter;
        public ArrayList<String> memberlist;
        public ArrayList<String> first;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.indexbarlayout);
            editsearch = (EditText) findViewById(R.id.textview_header);
            editsearch.addTextChangedListener(new TextWatcher() {


                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub
                    mainadapter.getFilter().filter(s.toString());
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub

                }
            });
            getScreenHeight(this);
            getScreenWidth(this);
            handler = new IndexBarHandler(this);

        }

        protected void onResume() {
            super.onResume();

            /* populating the base listview */
            CustomListView mainlistview = (CustomListView) findViewById(R.id.listView_main);
            String main_list_array[] = getResources().getStringArray(
                    R.array.base_array);
            if (main_list_array == null) {
                Log.d(TAG, "Array of the main listview is null");
                return;
            }
            mainadapter = new ArrayAdapter<String>(
                    getBaseContext(), R.layout.mainlistview_row/*
                     * android.R.layout.
                     * simple_list_item_1
                     */,
                     main_list_array);
            mainlistview.setAdapter(mainadapter);
            populateHashMap();

        }

        public int convertDipToPx(int dp, Context context) { // 10dp=15px
            Resources r = context.getResources();
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                    r.getDisplayMetrics());
            return (int) px;
        }

        public int convertPxtoDip(int pixel) { // 15px=23dp
            float scale = getResources().getDisplayMetrics().density;
            int dips = (int) ((pixel / scale) + 0.5f);
            return dips;
        }

        /**
         * Determines the width of the screen in pixels
         * 
         * @return width
         */
        public int getScreenWidth(Activity activity) {
            Display display = activity.getWindowManager().getDefaultDisplay();
            int width = display.getWidth();
            Log.d(TAG, "Screen Width in pixels=" + width);
            return width;
        }

        /**
         * Determines the height of the screen in pixels
         * 
         * @return height
         */
        public int getScreenHeight(Activity activity) {
            Display display = activity.getWindowManager().getDefaultDisplay();
            int height = display.getHeight();
            Log.d(TAG, "Screen Height in pixels=" + height);
            return height;
        }

        public float pixelsToSp(Context context, Float px) {
            float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
            Log.d(TAG, "GetTextSize in pixels=" + px + " In Sp="
                    + (px / scaledDensity));
            return px / scaledDensity;
        }

        public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {

            if (!(view instanceof TextView || view == null))
                return;
            TextView rowview = (TextView) view;

            CharSequence alpahbet = rowview.getText();

            if (alpahbet == null || alpahbet.equals(""))
                return;

            String selected_alpahbet = alpahbet.toString().trim();
            Integer position = alphabetToIndex.get(selected_alpahbet.charAt(0));
            Log.d(TAG, "Selected Alphabet is:" + selected_alpahbet
                    + "   position is:" + position);

            ListView listview = (ListView) findViewById(R.id.listView_main);
            listview.setSelection(position);
        }

        /**
         * This populates the HashMap which contains the mapping between the
         * alphabets and their relative position index.
         */
        private void populateHashMap() {
            alphabetToIndex = new HashMap<Character, Integer>();
            String base_list[] = getResources().getStringArray(R.array.base_array);
            int base_list_length = base_list.length;

            for (int i = 0; i < base_list_length; i++) {
                char firstCharacter = base_list[i].charAt(0);
                boolean presentOrNot = alphabetToIndex.containsKey(firstCharacter);
                if (!presentOrNot) {
                    alphabetToIndex.put(firstCharacter, i);
                    // Log.d(TAG,"Character="+firstCharacter+"  position="+i);
                }
            }
            number_of_alphabets = alphabetToIndex.size(); // Number of enteries in
            // the map is equal to
            // number of letters
            // that would
            // necessarily display
            // on the right.

            /*
             * Now I am making an entry of those alphabets which are not there in
             * the Map
             */
            String alphabets[] = getResources().getStringArray(
                    R.array.alphabtes_array);
            int index = -1;

            for (String alpha1 : alphabets) {
                char alpha = alpha1.charAt(0);
                index++;

                if (alphabetToIndex.containsKey(alpha))
                    continue;

                /*
                 * Start searching the next character position. Example, here alpha
                 * is E. Since there is no entry for E, we need to find the position
                 * of next Character, F.
                 */
                for (int i = index + 1; i < 26; i++) { // start from next character
                    // to last character
                    char searchAlphabet = alphabets[i].charAt(0);

                    /*
                     * If we find the position of F character, then on click event
                     * on E should take the user to F
                     */
                    if (alphabetToIndex.containsKey(searchAlphabet)) {
                        alphabetToIndex.put(alpha,
                                alphabetToIndex.get(searchAlphabet));
                        break;
                    } else if (i == 25) /*
                     * If there are no entries after E, then on
                     * click event on E should take the user to
                     * end of the list
                     */
                        alphabetToIndex.put(alpha, base_list_length - 1);
                    else
                        continue;

                }//
            }//
        }
        public void getAllMember() {

            new AsyncTask<Void, Void, String>() {
                ProgressDialog mProgressDialog;
                CustomListView mainlistview = (CustomListView) findViewById(R.id.listView_main);
                public String firstname;

                protected void onPostExecute(String result) {
                    mProgressDialog.dismiss();
                    memberlist = new ArrayList<String>();
                    first = new ArrayList<String>();

                    try {
                        JSONObject jsob = new JSONObject(result.toString());
                        if (jsob.getString("msg").equalsIgnoreCase("Success")) {
                            JSONArray datajson = jsob.getJSONArray("data");
                            for (int i = 0; i < datajson.length(); i++) {

                                JSONObject c = datajson.getJSONObject(i);
                                String id = c.getString("iMember_id");
                                firstname = c.getString("vUsername");
                                Log.d("firstname val", firstname);

                                memberlist.add(firstname);

                            }
                            mainadapter = new ArrayAdapter<String>(
                                    getBaseContext(),
                                    R.layout.mainlistview_row/*
                                                             * android.R.layout.
                                                             * simple_list_item_1
                                                             */, memberlist);
                            mainlistview.setAdapter(mainadapter);

                        } else {
                            System.out.println("not a valid user");
                        }

                    } catch (Exception e) {
                        Log.e("login problem", "" + e);

                    }

                }

                private void startActivity(Intent i) {
                    // TODO Auto-generated method stub

                }

                @Override
                protected String doInBackground(Void... arg0) {
                    // Creating service handler class instance
                    try {
                        HttpPost httppost1 = null;
                        HttpClient httpclient1 = new DefaultHttpClient();

                        httppost1 = new HttpPost(JsonKey.MAIN_URL);

                        // Add your data
                        List<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>(
                                1);
                        nameValuePairs1.add(new BasicNameValuePair("action",
                                "GetAllMembers"));
                        httppost1.setEntity(new UrlEncodedFormEntity(
                                nameValuePairs1));
                        // Execute HTTP Post Request
                        HttpResponse response1 = httpclient1.execute(httppost1);
                        BufferedReader in1 = new BufferedReader(
                                new InputStreamReader(response1.getEntity()
                                        .getContent()));
                        StringBuffer sb1 = new StringBuffer("");
                        String line1 = "";
                        while ((line1 = in1.readLine()) != null) {
                            sb1.append(line1);
                        }
                        in1.close();
                        Log.e(" Get All magazine original data", sb1.toString());
                        return sb1.toString();
                    } catch (Exception e) {
                        Log.e("Get All magazine response problem", "" + e);
                        return " ";
                    }
                }

                @Override
                protected void onPreExecute() {
                    // TODO Auto-generated method stub
                    super.onPreExecute();
                    mProgressDialog = new ProgressDialog(IndexBar.this);
                    mProgressDialog.setTitle("");
                    mProgressDialog.setCanceledOnTouchOutside(false);
                    mProgressDialog.setMessage("Please Wait...");
                    mProgressDialog.show();

                }
            }.execute();

        }


    }


the value of memberlist in getallmember is i want to set in 
 mainadapter = new ArrayAdapter<String>(
                    getBaseContext(), R.layout.mainlistview_row/*
                     * android.R.layout.
                     * simple_list_item_1
                     */,
                     main_list_array);

在上面的代码中代替main_array_list但是这样做我得到空指针异常我怎么得到这个值?

String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Android Alphabetic IndexBar Scroller</string>
    <string-array name="alphabtes_array">
        <item>A</item>
        <item>B</item>
        <item>C</item>
        <item>D</item>
        <item>E</item>
        <item>F</item>
        <item>G</item>
        <item>H</item>
        <item>I</item>
        <item>J</item>
        <item>K</item>
        <item>L</item>
        <item>M</item>
        <item>N</item>
        <item>O</item>
        <item>P</item>
        <item>Q</item>
        <item>R</item>
        <item>S</item>
        <item>T</item>
        <item>U</item>
        <item>V</item>
        <item>W</item>
        <item>X</item>
        <item>Y</item>
        <item>Z</item>
    </string-array>
    <string-array name="base_array">
        <item >Alphabets</item>
        <item >Apple</item>
        <item >Airplane</item>
        <item >Banana</item>
        <item >Boy</item>
        <item >Bicycle</item>
        <item >Car</item>
        <item >Carrot</item>
        <item >Dog</item>
        <item >Donkey</item>
        <item >Flight</item>
        <item >Flower</item>
        <item >Hover</item>
        <item >Howard</item>
        <item >Holland</item>
        <item >Hug</item>
        <item >House</item>
        <item >Hallow</item>
        <item >Icecream</item>
        <item >Jug</item>
        <item >Jassmine</item>
        <item >Jely Bean</item>
        <item >Khushboo</item>
        <item >Kamal</item>
        <item >Mango</item>
        <item >Margo</item>
        <item >Melon</item>
        <item >Mouse</item>
        <item >Narrow</item>
        <item >Noose</item>
        <item >Nilon</item>
        <item >Oops</item>
        <item >Owl</item>
        <item >Parrot</item>
        <item >Pencil</item>
        <item >Palates</item>
        <item >Pillow</item>
        <item >Queue</item>
        <item >Qwk</item>
        <item >Rat</item>
        <item >Run</item>
        <item >Ranbeer</item>
        <item >Raina</item>
        <item >Ribbon</item>
        <item >Swallow</item>
        <item >Sarawati</item>
        <item >Singing</item>
        <item >Shine</item>
        <item >Tea</item>
        <item >Tamarind</item>
        <item >Tiger</item>
        <item >Vivek</item>
        <item >Voilet</item>
        <item >Voilence</item>
        <item >Value</item>
        <item >Vent</item>
        <item >Wall</item>
        <item >Wand</item>
        <item >X</item>
        <item >Yogurt</item>
        <item >Yatch</item>
        <item >Z</item>


    </string-array>
    <string name="header">Android Alphabetic IndexBar Scroller</string>
    <string name="dot">.</string>

</resources>

0 个答案:

没有答案