Java 无法迭代对共享首选项的Android字符串引用?
所以我有一个名为locationsList的ArrayList。我还拥有共享引用,其中包含在上一个活动中设置的城市名称,我希望将其添加到我的ArrayList中。但我想在它的末尾添加一个>。比如说Java 无法迭代对共享首选项的Android字符串引用?,java,android,reference,sharedpreferences,Java,Android,Reference,Sharedpreferences,所以我有一个名为locationsList的ArrayList。我还拥有共享引用,其中包含在上一个活动中设置的城市名称,我希望将其添加到我的ArrayList中。但我想在它的末尾添加一个>。比如说 SharedPreferences prefs = getActivity().getSharedPreferences("prefs", 0); locationsList.add(0, prefs.getString("city", "no city") + ">"); 但是它不会改变!!
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();
}
}
从异步调用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;
}
}
我明白了。始终记住,当迭代到字符串文本vi时,您将其设置为xml中的一行,以便
android:singleLine="true"
这真是一个愚蠢的错误你能发布更多的代码吗?/你的代码没有错。@RolfSmit但肯定有错。为什么它没有改变!!!。当我说tv.setText是在开始的时候???我怀疑你的新代码是如何不编译成一个新的APK文件而运行旧文件的?会是这样吗?在我的环境中运行这段代码效果很好;Log.dTest,test+>;首先,单词是附加的,而不是迭代的。作为一种解决方案,这种改变并不十分可信——相反,它看起来像是与最终让您测试实际源代码的东西的随机巧合。
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;
}
}
android:singleLine="true"