2013-06-19 54 views
0

我試圖讓我的列表視圖呈現在列表視圖中一些簡單的標籤,在這裏渲染HTML是我的代碼:麻煩的Android的ListView

package net.mikepearce.theagileguide.fragment; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.text.Html; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

import net.mikepearce.theagileguide.R; 
import net.mikepearce.theagileguide.ValueActivity; 

public class ListManifestoFragment extends ListFragment { 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Set the adapter based on the string-array 
     String[] principles = getResources().getStringArray(R.array.manifesto_values); 
     ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_values, principles) 
     { 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) 
      { 
       View row; 

       if (null == convertView) { 
        row = mInflater.inflate(R.layout.list_values, null); 
       } else { 
        row = convertView; 
       } 

       TextView tv = (TextView) row.findViewById(android.R.id.text1); 
       tv.setText(Html.fromHtml(getItem(position))); 

       return row; 
      } 

     }; 

     setListAdapter(adapter); 

     // Get the list view 
     final ListView listView = getListView(); 
     listView.setTextFilterEnabled(true); 

     // When clicked, pass to new Activity with the position as a parameter 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Activity a = getActivity(); 
       Intent intent = new Intent(view.getContext(), ValueActivity.class); 
       intent.putExtra("value_id", position); 
       startActivity(intent); 
       a.overridePendingTransition(0,0); 
      } 
     }); 

    } 

} 

其中我在某種程度上,從這裏:https://stackoverflow.com/a/9695159/155227

我對Android Dev很新,所以我不確定這是我應該做什麼,如果是這樣,mInflater來自哪裏,因爲我得到一個錯誤。

有人可以幫忙嗎?

感謝,

邁克

回答

1

mInflater應該是一個LayoutInflater。您應該可以用getActivity().getLayoutInflater()替換mInflater

0

在View行下使用此代碼;

LayoutInflater mInflater= ((Activity)context).getLayoutInflater(); 
+0

這是行不通的,因爲這個代碼是在一個片段,而不是一個活動,並沒有什麼在代碼中有,我可以看到一個名爲'context'。 – CommonsWare

+0

對於這個上下文你​​使用的是你的Activityname.this或者對於fragment((Activity)上下文).getLayoutInflater()使用getActivity()。getLayoutInflater(); –