本文介绍了Android的添加OnClickListener到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在努力创建我所谓的原型新ROM上设置的应用程序,我想一个OnClickListener添加到我的列表视图,但我不能找到合适的方式来做到这一点,因此我已经把这里为帮助,我想知道是否有人能告诉我怎么我会后我下面的活动和感谢。
包fr.xgouchet.tuto.switch preferences;进口的java.util.ArrayList;
进口的java.util.List;。进口的Android preference preferenceActivity;
进口android.widget.ListAdapter;公共类我的prefsActivity扩展$ P $ {pferenceActivity 私人列表<页眉和GT; mHeaders; 保护无效onResume(){
super.onResume(); 的setTitle(设置); 如果(getListAdapter()的instanceof我的prefsHeaderAdapter)
((我的prefsHeaderAdapter)getListAdapter())恢复();
} 保护无效的onPause(){
super.onPause();
如果(getListAdapter()的instanceof我的prefsHeaderAdapter)
((我的prefsHeaderAdapter)getListAdapter())暂停();
} 公共无效onBuildHeaders(列表<页眉和GT;目标){
//调用时,设置屏幕是为第一次
//我们从XML描述加载头 loadHeadersFromResource(R.xml.my_ prefs_headers,目标); mHeaders =目标;
} 公共无效setListAdapter(ListAdapter适配器){
INT I,计数; 如果(mHeaders == NULL){
mHeaders =新的ArrayList<页眉和GT;();
//当保存的状态提供了标题的列表,
// onBuildHeaders不叫
//所以我们从给定的适配器构建它,然后用自己的适配器 数= adapter.getCount();
对于(i = 0; I<计数; ++ I)
mHeaders.add((头)adapter.getItem(一));
} super.setListAdapter(新我prefsHeaderAdapter(这一点,mHeaders));
}
}
解决方案
在preferenceActivity的ListView背后getListView hiddent();
的simpliest例如:
ListView控件的ListView = getListView();
listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
公共无效onItemClick(适配器视图<> AV,观景,INT I,长L){
Toast.makeText(Activity.thismyPos+ I,Toast.LENGTH_LONG).show();
}
});
code看起来像
包fr.xgouchet.tuto.switch preferences;进口的java.util.ArrayList;
进口的java.util.List;。进口的Android preference preferenceActivity;
进口android.widget.ListAdapter;公共类我的prefsActivity扩展$ P $ {pferenceActivity 私人列表<页眉和GT; mHeaders; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); ListView控件的ListView = getListView();
listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
公共无效onItemClick(适配器视图<> AV,观景,INT I,长L){
Toast.makeText(Activity.thismyPos+ I,Toast.LENGTH_LONG).show();
}
});
} 保护无效onResume(){
super.onResume(); 的setTitle(设置); 如果(getListAdapter()的instanceof我的prefsHeaderAdapter)
((我的prefsHeaderAdapter)getListAdapter())恢复();
} 保护无效的onPause(){
super.onPause();
如果(getListAdapter()的instanceof我的prefsHeaderAdapter)
((我的prefsHeaderAdapter)getListAdapter())暂停();
} 公共无效onBuildHeaders(列表<页眉和GT;目标){
//调用时,设置屏幕是为第一次
//我们从XML描述加载头 loadHeadersFromResource(R.xml.my_ prefs_headers,目标); mHeaders =目标;
} 公共无效setListAdapter(ListAdapter适配器){
INT I,计数;
如果(mHeaders == NULL){
mHeaders =新的ArrayList<页眉和GT;();
//当保存的状态提供了标题的列表,
// onBuildHeaders不叫
//所以我们从给定的适配器构建它,然后用自己的适配器 数= adapter.getCount();
对于(i = 0; I<计数; ++ I)
mHeaders.add((头)adapter.getItem(一));
} super.setListAdapter(新我prefsHeaderAdapter(这一点,mHeaders));
}}
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));
}
}
解决方案
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));
}
}
这篇关于Android的添加OnClickListener到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!