0

我试图让我的列表视图在列表视图中呈现一些简单的标签,这是我的代码:

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 来自哪里,因为我得到一个错误。

有人可以帮忙吗?

谢谢,

麦克风

4

2 回答 2

1

mInflater应该是一个LayoutInflater。您应该可以替换mInflatergetActivity().getLayoutInflater().

于 2013-06-19T13:09:03.080 回答
0

在查看行下使用此代码;

LayoutInflater mInflater= ((Activity)context).getLayoutInflater();
于 2013-06-19T13:14:08.157 回答