0

「ProtoType」と呼ばれる新しい rom 用の設定アプリを作成しようとしていて、リストビューに 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

On PreferenceActivity listView is hiddent behind getListView(); The simpliest example:

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();
        }
    });

Code will look like

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 に答える