0

我一直在尝试为我的新 rom 创建一个名为“ProtoType”的设置应用程序,我正在尝试将 OnClickListener 添加到我的列表视图中,但我找不到合适的方法,因此我转向这里寻求帮助和我想知道是否有人可以告诉我如何在下面发布我的活动,谢谢。

package fr.xgouchet.tuto.switchpreferences;

import java.util.ArrayList;
import java.util.List;

import android.preference.PreferenceActivity;
import android.widget.ListAdapter;

public class MyPrefsActivity extends PreferenceActivity {

    private List<Header> mHeaders;

    protected void onResume() {
        super.onResume();

        setTitle("Settings"); 

        if (getListAdapter() instanceof MyPrefsHeaderAdapter)
            ((MyPrefsHeaderAdapter) getListAdapter()).resume();
    }

    protected void onPause() {
        super.onPause();
        if (getListAdapter() instanceof MyPrefsHeaderAdapter)
            ((MyPrefsHeaderAdapter) getListAdapter()).pause();
    }

    public void onBuildHeaders(List<Header> target) {
        // Called when the settings screen is up for the first time
        // we load the headers from our xml description

        loadHeadersFromResource(R.xml.my_prefs_headers, target);

        mHeaders = target;
    }

    public void setListAdapter(ListAdapter adapter) {
        int i, count;

        if (mHeaders == null) {
            mHeaders = new ArrayList<Header>();
            // When the saved state provides the list of headers,
            // onBuildHeaders is not called
            // so we build it from the adapter given, then use our own adapter

            count = adapter.getCount();
            for (i = 0; i < count; ++i)
                mHeaders.add((Header) adapter.getItem(i));
        }

        super.setListAdapter(new MyPrefsHeaderAdapter(this, mHeaders));
    }
}
4

2 回答 2

3

在 PreferenceActivity 上,listView 隐藏在 getListView() 后面; 最简单的例子:

ListView listView = getListView();
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View view, int i, long l) {
            Toast.makeText(Activity.this, "myPos "+i, Toast.LENGTH_LONG).show();
        }
    });

代码看起来像

package fr.xgouchet.tuto.switchpreferences;

import java.util.ArrayList;
import java.util.List;

import android.preference.PreferenceActivity;
import android.widget.ListAdapter;

public class MyPrefsActivity extends PreferenceActivity {

    private List<Header> mHeaders;

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



        ListView listView = getListView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> av, View view, int i, long l) {
                Toast.makeText(Activity.this, "myPos "+i, Toast.LENGTH_LONG).show();
            }
        });
     }

    protected void onResume() {
        super.onResume();

        setTitle("Settings"); 

        if (getListAdapter() instanceof MyPrefsHeaderAdapter)
            ((MyPrefsHeaderAdapter) getListAdapter()).resume();
    }

    protected void onPause() {
        super.onPause();
        if (getListAdapter() instanceof MyPrefsHeaderAdapter)
            ((MyPrefsHeaderAdapter) getListAdapter()).pause();
    }

    public void onBuildHeaders(List<Header> target) {
        // Called when the settings screen is up for the first time
        // we load the headers from our xml description

        loadHeadersFromResource(R.xml.my_prefs_headers, target);

        mHeaders = target;
    }

    public void setListAdapter(ListAdapter adapter) {
        int i, count;


        if (mHeaders == null) {
            mHeaders = new ArrayList<Header>();
            // When the saved state provides the list of headers,
            // onBuildHeaders is not called
            // so we build it from the adapter given, then use our own adapter

            count = adapter.getCount();
            for (i = 0; i < count; ++i)
                mHeaders.add((Header) adapter.getItem(i));
        }

        super.setListAdapter(new MyPrefsHeaderAdapter(this, mHeaders));
    }

}
于 2013-05-21T06:58:58.160 回答
0

我认为它应该在您的适配器中实现。这是自定义适配器的示例。您可以为项目中的元素指定设置和侦听器。

/** Provides the custom adapter for views of words */
private class WordAdapter extends ArrayAdapter<DTOWord> {
    Context context;
    int layoutResourceId;
    ArrayList<DTOWord> wordsArray;

    /** Set up words data */
    public WordAdapter(Context context, int layoutResourceId, 
            ArrayList<DTOWord> words) 
    {
        super(context, layoutResourceId, words);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.wordsArray = words;            
    }

    @Override
    /** Returns a view with a word */
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View row = convertView;

        if (row == null)
        {
            LayoutInflater inflater = ((Activity) context).
                    getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
        }

        return this.getView(row, this.wordsArray.get(position));
    }

    /** Set up the view of the word with specified data */
    private View getView(View wordView, final DTOWord wordData)
    {
        View container = (View) wordView.findViewById(R.id.
                layout_wordData);
        TextView title = (TextView) wordView.findViewById(R.id.
                textView_word);
        Button btnEditWord = (Button) wordView.findViewById(R.id.
                btn_wordEdit);

        this.setEditListener(btnEditWord, wordData);            

        return wordView;
    }

    /** Set action which switches to the edition view of the selected word */
    private void setEditListener(Button btnEditWord, final DTOWord wordData) {
        btnEditWord.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(WordAdapter.this.context, 
                        AddWord.class);
                i.putExtra("word-name", wordData.getWord());
                i.putExtra("word-language", wordData.getLanguage().
                        getName());
                startActivity(i);           
            }
        });
    }
}   
于 2013-05-21T08:02:41.073 回答