2014-11-21 206 views
0

所以我已經在Stackoverflow上嘗試了許多解決方案,但我永遠無法知道如何去做。我有點新手。所以我有一個ListView並想更改字體。 這裏是一個包含我的ListView更改ListView的字體顏色

package qax.khabaralyoum; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import android.annotation.TargetApi; 
import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.graphics.drawable.ColorDrawable; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Build; 
import android.os.Bundle; 
import android.text.Html; 
import android.util.Log; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.StatusLine; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.view.View; 
import android.view.Window; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh; 
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout; 
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Fragment; 
import android.app.ListFragment; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 

public class MainListActivity extends ListActivity { 

    public static final int NUMBER_OF_POSTS = 20; 
    public static final String TAG = MainListActivity.class.getSimpleName(); 
    protected JSONObject mBlogData; 
    protected ProgressBar mProgressBar; 
    private final String KEY_TITLE = "title"; 
    private final String KEY_AUTHOR = "author"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 
     getActionBar().hide(); 
     setContentView(R.layout.activity_main_list); 

     mProgressBar = (ProgressBar) findViewById(R.id.progressBar); 

     if (isNetworkAvailable()) { 
      mProgressBar.setVisibility(View.VISIBLE); 

      GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask(); 
      getBlogPostsTask.execute(); 
     } else { 
      Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show(); 
     } 

     //Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     try { 
      JSONArray jsonPosts = mBlogData.getJSONArray("posts"); 
      JSONObject jsonPost = jsonPosts.getJSONObject(position); 
      String blogUrl = jsonPost.getString("url"); 

      Intent intent 
        = new Intent(this, BlogWebViewActivity.class); 
      intent.setData(Uri.parse(blogUrl)); 
      startActivity(intent); 
     } catch (JSONException e) { 
      logException(e); 
     } 
    } 

    private void logException(Exception e) { 
     logException(e); 
    } 

    private boolean isNetworkAvailable() { 
     ConnectivityManager manager = (ConnectivityManager) 
       getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = manager.getActiveNetworkInfo(); 

     boolean isAvailable = false; 
     if (networkInfo != null && networkInfo.isConnected()) { 
      isAvailable = true; 
     } 

     return isAvailable; 
    } 

    private void handleBlogResponse() { 
     mProgressBar.setVisibility(View.INVISIBLE); 

     if (mBlogData == null) { 
      updateDisplayError(); 

     } else { 

      try { 
       JSONArray jsonPosts = mBlogData.getJSONArray("posts"); 
       ArrayList<HashMap<String, String>> blogPosts = 
         new ArrayList<HashMap<String, String>>(); 
       for (int i = 0; i < jsonPosts.length(); i++) { 
        JSONObject post = jsonPosts.getJSONObject(i); 
        String title = post.getString(KEY_TITLE); 
        title = Html.fromHtml(title).toString(); 

        HashMap<String, String> blogPost = new HashMap<String, String>(); 
        blogPost.put(KEY_TITLE, title); 

        blogPosts.add(blogPost); 

       } 

       String[] keys = {KEY_TITLE}; 
       int[] ids = {android.R.id.text1}; 
       SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, android.R.layout.simple_list_item_1, keys, ids); 
       setListAdapter(adapter); 

      } catch (JSONException e) { 
       logException(e); 
      } 
     } 

    } 

    private void updateDisplayError() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle(getString(R.string.error_title)); 
     builder.setMessage(getString(R.string.error_message)); 
     builder.setPositiveButton(android.R.string.ok, null); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 

     TextView emptyTextView = (TextView) getListView().getEmptyView(); 
     emptyTextView.setText(getString(R.string.no_items)); 

    } 

    private class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> { 

     @Override 
     protected JSONObject doInBackground(Object... params) { 
      int responseCode = -1; 
      JSONObject jsonResponse = null; 
      StringBuilder builder = new StringBuilder(); 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet httpget = new HttpGet("http://idoodling.com/api/get_recent_posts/"); 

      try { 
       HttpResponse response = client.execute(httpget); 
       StatusLine statusLine = response.getStatusLine(); 
       responseCode = statusLine.getStatusCode(); 

       if (responseCode == HttpURLConnection.HTTP_OK) { 
        HttpEntity entity = response.getEntity(); 
        InputStream content = entity.getContent(); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
        String line; 
        while ((line = reader.readLine()) != null) { 
         builder.append(line); 
        } 

        jsonResponse = new JSONObject(builder.toString()); 

       } else { 
        Log.i(TAG, String.format("Unsuccessful HTTP response code: %d", responseCode)); 
       } 
      } catch (MalformedURLException e) { 
       logException(e); 
      } catch (IOException e) { 
       logException(e); 
      } catch (Exception e) { 
       logException(e); 
      } 

      return jsonResponse; 
     } 

     @Override 
     protected void onPostExecute(JSONObject result) { 
      mBlogData = result; 
      handleBlogResponse(); 
     } 

    } 

    public void activity2(View view) { 
     Intent intent = new Intent(this, qax.khabaralyoum.BlogWebViewActivity.class); 
     startActivity(intent); 

    } 
} 

我從我需要某種類型的適配器來改變字體的解決方案,知道我的Java類。我想我的課堂上有一個Adapater,但是我是否需要編輯或添加一些內容,以便將它連接到XML以更改字體顏色?我會喜歡一步一步的指導,爲我做的代碼,我有。

回答

3

正如您在文章中提到的,您需要在適配器中設置字體顏色。爲此,您需要覆蓋默認的getView並進行更改,以便設置TextView文本的顏色。要做到這一點,改變

SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, android.R.layout.simple_list_item_1, keys, ids); 

SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, android.R.layout.simple_list_item_1, keys, ids){ 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 
     TextView tv = (TextView) view.findViewById(android.R.id.text1); 
     tv.setTextColor(Color.RED); 

     return view; 

    }; 
}; 

您需要更改Color.RED到您想要的顏色。如果您使用十六進制顏色代碼,則可以執行Color.parseColor("#FFFFFF"),用您的顏色代替#FFFFFF。如果您需要更多關於獲取Color對象的信息,請參閱關於Color的Android文檔頁面。 (該頁面可以在https://developer.android.com/reference/android/graphics/Color.html

+0

非常感謝。這工作完美! – qax 2014-11-21 19:47:23