好的,我现在非常沮丧,我有一个我设置完全相同的花药文件,但由于某种原因,这只是不想工作。我有一个PreferenceScreen xml文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:summary="@string/alias_summary"
android:key="alias"
android:title="@string/alias_title"/>
<EditTextPreference
android:summary="@string/server_url_summary"
android:key="server_url"
android:title="@string/server_url_title"
android:defaultValue="http://www.myserverhere.com"/>
<ListPreference
android:summary="@string/main_bg_list_pref_summary"
android:title="@string/main_bg_list_pref_title"
android:key="main_view_bg_list_pref"
android:entries="@array/color_names"
android:entryValues="@array/color_values"
android:defaultValue="white"/>
<EditTextPreference />
</PreferenceScreen>
当我尝试在我的活动中调用服务器URL(抱歉我必须删除实际链接,但它完全相同)时,它只是不会显示但是如果我强制解析该服务器URL ,它就像一个魅力。我不知道为什么它不会看到这个pref ..任何帮助将不胜感激!!
这是我的活动代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ReviewsByCategory extends ListActivity implements OnSharedPreferenceChangeListener
{
SharedPreferences prefs;
ArrayList<HashMap<String,String>> reviews = new ArrayList<HashMap<String,String>>();
View linearLayoutCustomList;
private final static String TAG = "ReviewsByCategory";
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.remote_list);
linearLayoutCustomList = findViewById(R.id.customList);
displayReviews();
}
private void displayReviews()
{
String[] fields = new String[]{"date", "description", "addinfo", "review", "rating", "alias"};
int[] ids = new int[]{R.id.date, R.id.description, R.id.addInfo, R.id.review, R.id.rating, R.id.alias};
SimpleAdapter adapter = new SimpleAdapter(this, reviews, R.layout.remote_row, fields, ids);
Log.d(TAG, "just before populate list");
populateList();
this.setListAdapter(adapter);
}
private void populateList()
{
BufferedReader in = null;
try
{
String category = getIntent().getExtras().getString("CATEGORY");
category = "?CATEGORY=" + category;
//THIS WILL NOT WORK
//String server = prefs.getString("server_url", "http://www.myserverhere.com" + category);
//THIS WILL WORK
String server = "http://www.myserverhere.com" + category;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(server));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while((line = in.readLine()) != null)
{
HashMap<String, String> temp = new HashMap<String, String>();
temp.put("date", line);
if((line = in.readLine()) != null)
{
temp.put("description", line);
}
if((line = in.readLine()) != null)
{
temp.put("addinfo", line);
}
if((line = in.readLine()) != null)
{
temp.put("review", line);
}
if((line = in.readLine()) != null)
{
temp.put("rating", line);
}
if((line = in.readLine()) != null)
{
temp.put("alias", line);
}
reviews.add(temp);
}
}
catch(Exception e)
{
Toast.makeText(this, "Error: " + e, Toast.LENGTH_LONG).show();
}
}
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1)
{
linearLayoutCustomList = findViewById(R.id.customList);
String bg = prefs.getString("custom_list_bg_color", "#000000");
linearLayoutCustomList.setBackgroundColor(Color.parseColor(bg));
}
}
答案 0 :(得分:1)
假设它使server_url正常,它没有获得该类别。如果找不到server_url,您只需添加类别。
换句话说,而不是
String server =
prefs.getString("server_url", "http://www.myserverhere.com" + category);
你应该写
String server =
prefs.getString("server_url", "http://www.myserverhere.com") + category;
答案 1 :(得分:0)
我添加这一行后工作正常:
prefs = PreferenceManager.getDefaultSharedPreferences(this);