Reputation: 252
I have been trying to create a settings app for my new rom called "ProtoType" and i am trying to add an OnClickListener to my listview but i cant find the appropriate way to do so and as a result i have turned to here for help and i was wondering if anybody can show me how i'll post my activity below and thanks.
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));
}
}
Upvotes: 0
Views: 15213
Reputation: 5336
I think it should be implemented in your adapter. This is example of a custom adapter. You can specify settings and listeners for elements in items.
/** 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);
}
});
}
}
Upvotes: 0
Reputation: 3350
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));
}
}
Upvotes: 3