Android String对共享首选项的引用无法迭代?

时间:2014-11-03 18:41:18

标签: java android reference sharedpreferences

所以我有一个名为locationsList的ArrayList。我还有一个SharedPreferences,它包含一个城市名称,该名称是在我之前的活动中设置的,我希望将其添加到我的ArrayList中。但我想用“>”添加它在它的最后。例如,

SharedPreferences prefs = getActivity().getSharedPreferences("prefs", 0);
locationsList.add(0, prefs.getString("city", "no city") + ">");

然而它没有改变!!! “>”没有添加。当我设置textView的文本时,我甚至尝试添加它。

TextView tv...;
tv.setText(locationsList.get(0) + ">");

我不明白为什么它不能改变。显然,数组列表包含对首选项的引用,并且不能更改。但我甚至尝试将首选项分配给字符串变量,然后迭代它,它不会让步。任何人都可以帮助我

活动中的异步

class getLocationsUrl extends AsyncTask<String, String, String> {

    ArrayList<String> tempList = new ArrayList<String>();

    @Override
    protected String doInBackground(String... arg0) {

        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("city", prefs.getString("city", "durban")));

        JSONParser parser = new JSONParser();
        JSONObject json = parser.makeHttpRequest(IMAGE_URL + "fetchlocations.php", "POST",   
params);

        try {

            list = json.getJSONArray("posts");

            for(int i = 0; i < list.length(); i++) {
                JSONObject c = list.getJSONObject(i);
                String location = c.getString("location");

                tempList.add(location);
                Log.d("async trend", trendList.get(0));
            }

            Log.d("Got list", imageUrl.get(0) + " " + trendList.get(0));

        } catch(JSONException je) {
            Log.d("json exception", "json exception");
            je.printStackTrace();   
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        locationsList = tempList;
        locationsFragment.updateAdapter();
        feedFragment.updateHeadingAdapter();
    }
}

在onCreateView中设置FeedFragment

    headingPager = (ViewPager) view.findViewById(R.id.headingPager);

    headingList = (ArrayList<String>) NightWatch.locationsList.clone();
    added = prefs.getString("city", "makaka");
    headingList.add(0, added + ">");

    headingAdapter = new CustomHeadingPagerAdapter(getChildFragmentManager());
    headingPager.setAdapter(headingAdapter);

FeedFragments CustomHeadingAdapter

    public class CustomHeadingPagerAdapter extends FragmentPagerAdapter {

    public CustomHeadingPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {

        return HeadingFragment.newInstance(arg0, headingList);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return headingList.size();
    }

}

从异步

调用的FeedFragment的updateHeadingAdapter()
    public void updateHeadingAdapter() {
    headingList = (ArrayList<String>) NightWatch.locationsList.clone();
    headingList.add(0, (prefs.getString("city", "null") + ">"));
    headingAdapter = new CustomHeadingPagerAdapter(getChildFragmentManager());
    headingPager.setAdapter(headingAdapter);
}

最后是我在适配器中返回的HeadingFragment。

   package info.nightowl.nightowl;

import java.util.ArrayList;

import com.example.nightowl.R;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class HeadingFragment extends Fragment{

int position;
Typeface font;
ArrayList<String> list;
SharedPreferences prefs;

static HeadingFragment newInstance(int position, ArrayList<String> list) {

    final HeadingFragment f = new HeadingFragment();
    Bundle args = new Bundle();
    args.putInt("position", position);
    args.putStringArrayList("list", list);

    f.setArguments(args);
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {

    position = getArguments().getInt("position");
    list = getArguments().getStringArrayList("list");
    prefs = getActivity().getSharedPreferences("prefs", 0);
    font = Typeface.createFromAsset(getActivity().getAssets(),
            "NotCourierSans.ttf");

    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.heading_viewpage_layout, null);

    TextView tv = (TextView) v.findViewById(R.id.headingText);

    if(position == 0) tv.setText(">" + list.get(position) + ">");
    else tv.setText(list.get(position) + ">");

    tv.setTypeface(font);
    tv.setTextColor(Color.YELLOW);
    tv.setTextSize(30);


    return v;

}
}

1 个答案:

答案 0 :(得分:0)

我明白了。永远记住,当迭代到字符串文本vi时,你将它设置为xml中的单行,所以......

android:singleLine="true"

真的是一种愚蠢的错误