设置onItemClick来定义适配器的ListView [英] Set onItemClick to Custom adapter ListView
问题描述
当我试图过滤时,修正到所需的意图,我的ListView项目的位置的问题,并得到了信息,我可以使用自定义适配器覆盖的问题,我已经做到了,但我不知道如何分配的点击每一个项目,请检查以下code:
公共类IndexPageActivity延伸活动{
ListView控件的ListView;
的EditText editTextB;
@覆盖
保护无效的onCreate(包savfedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.indexpage);
ListView的=(的ListView)findViewById(R.id.pageList);
editTextB =(EditText上)findViewById(R.id.searchB);
listView.setAdapter(新PagesAdapter(本));
listView.setOnItemClickListener((OnItemClickListener)本);
}
}
类SingleRow {
字符串pagedata;
SingleRow(字符串pagedata){
this.pagedata = pagedata;
}
}
类PagesAdapter扩展了BaseAdapter实现OnItemClickListener {
ArrayList的< SingleRow>页次;
上下文语境;
PagesAdapter(上下文C){
上下文= C;
页次=新的ArrayList< SingleRow>();
资源RES = c.getResources();
的String [] pagedatas = res.getStringArray(R.array.pages_data);
的for(int i = 0; I< 463;我++){
pagelist.add(新SingleRow(pagedatas [I]));
}
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回pagelist.size();
}
@覆盖
公共对象的getItem(int i)以{
// TODO自动生成方法存根
返回pagelist.get(ⅰ);
}
@覆盖
众长getItemId(int i)以{
// TODO自动生成方法存根
返回我;
}
@覆盖
公共查看getView(INT I,视图中查看,ViewGroup中viewG){
// TODO自动生成方法存根
LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看排= inflater.inflate(R.layout.single_row,viewG,假);
TextView的PAGETITLE =(TextView中)row.findViewById(R.id.textViewRow);
SingleRow温度= pagelist.get(我);
pagetitle.setText(temp.pagedata);
返回行;
}
@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT I,长ARG3){
// TODO自动生成方法存根
}
}
我会AP preciate给予任何帮助。谢谢伊乌!
修改
将这项工作?
如果(指数== 0){
意图I =新的意图(这一点,WebViewActivity.class);
i.putExtra(keyHTML,文件:///android_asset/page1.html);
startActivity(ⅰ);
}否则,如果(指数== 1){
意图I =新的意图(这一点,WebViewActivity.class);
i.putExtra(keyHTML,文件:///android_asset/page2.html);
startActivity(ⅰ);
编辑中的全部
我刚刚得到你所需要的,我添加了一个过滤器,你的 baseAdapter
,然后在文本更改 EDITTEXT
您过滤的ListView
,然后你去所需要的活动。
这里是全code,但你需要的,我已经改变了主意之后袒露:
- 我改变了页次到ArrayList中,而不是
- 有一个在过滤,当我抹去我在写的
的EditText
它不更新ListView控件,你需要找出原因的错误。 - 我改变了函数的返回值
的getItem(int i)以
从对象到字符串 - 在
onItemClick
你要搜索的名称,而不是位置。
这里是code:
公共类IndexPageActivity扩展活动实现OnItemClickListener {
ListView控件的ListView;
的EditText editTextB;
PagesAdapter适配器1;
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
ListView的=(的ListView)findViewById(R.id.pageList);
editTextB =(EditText上)findViewById(R.id.searchB);
适配器1 =新PagesAdapter(本);
listView.setAdapter(适配器1);
adapter1.notifyDataSetChanged();
listView.setOnItemClickListener(本);
editTextB.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效onTextChanged(CharSequence的CS,诠释ARG1,INT ARG2,
INT ARG3){
//当用户更改文本
。IndexPageActivity.this.adapter1.getFilter()滤波器(cs.toString());
adapter1.notifyDataSetChanged();
}
@覆盖
公共无效beforeTextChanged(CharSequence的arg0中,INT ARG1,
INT ARG2,诠释ARG3){
// TODO自动生成方法存根
}
@覆盖
公共无效afterTextChanged(编辑为arg0){
// TODO自动生成方法存根
}
});
}
@覆盖
公共无效onItemClick(适配器视图<>为arg0,视图V,INT位置,长ARG3){
// TODO自动生成方法存根
我的意图;
字符串名称= adapter1.getItem(位置);
Log.d(ID,名称);
如果(name.equals(项目1))
{
I =新的意图(这一点,anActivity.class);
startActivity(ⅰ);
}
否则如果(name.equals(项目2))
{
I =新的意图(这一点,anActivity2.class);
startActivity(ⅰ);
}
}
}
类SingleRow {
字符串pagedata;
SingleRow(字符串pagedata){
this.pagedata = pagedata;
}
}
类PagesAdapter扩展了BaseAdapter实现过滤的{
ArrayList的<字符串>页次;
名单<字符串> ArrayList的;
上下文语境;
的String [] pagedatas;
PagesAdapter(上下文C){
上下文= C;
页次=新的ArrayList<字符串>();
资源RES = c.getResources();
pagedatas = res.getStringArray(R.array.pages_data);
的for(int i = 0; I< 463;我++){
pagelist.add(pagedatas [I]);
}
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回pagelist.size();
}
@覆盖
公共字符串的getItem(int i)以{
// TODO自动生成方法存根
返回pagelist.get(ⅰ);
}
@覆盖
众长getItemId(int i)以{
// TODO自动生成方法存根
返回我;
}
@覆盖
公共查看getView(INT I,视图中查看,ViewGroup中viewG){
// TODO自动生成方法存根
LayoutInflater充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看排= inflater.inflate(R.layout.single_row,viewG,假);
TextView的PAGETITLE =(TextView中)row.findViewById(R.id.textViewRow);
字符串临时= pagelist.get(我);
pagetitle.setText(临时);
返回行;
}
公共类filter_here扩展过滤器{
@覆盖
保护FilterResults performFiltering(CharSequence的约束){
// TODO自动生成方法存根
FilterResults结果=新FilterResults();
//如果约束为空返回原来的名称
如果(constraint.length()== 0){
Result.values =页次;
Result.count = pagelist.size();
返回结果;
}
ArrayList的<字符串> Filtered_Names =新的ArrayList<字符串>();
字符串filterString = constraint.toString()与toLowerCase()。
字符串filterableString;
的for(int i = 0; I< pagelist.size();我++){
filterableString = pagelist.get(ⅰ);
如果(filterableString.toLowerCase()。包括(filterString)){
Filtered_Names.add(filterableString);
}
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
返回结果;
}
@覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){
// TODO自动生成方法存根
页次=(ArrayList的<字符串>)results.values;
notifyDataSetChanged();
}
}
@覆盖
公共过滤用getFilter(){
// TODO自动生成方法存根
返回新filter_here();
}
}
When I had problems trying to fix the position of my listView items to the desired intent when filtered, and got info I could override the problem using a custom adapter, I have done that but I do not know how to assign the clicks to each items, please check below code:
public class IndexPageActivity extends Activity {
ListView listView;
EditText editTextB;
@Override
protected void onCreate(Bundle savfedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.indexpage);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
listView.setAdapter(new PagesAdapter(this));
listView.setOnItemClickListener((OnItemClickListener) this);
}
}
class SingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
class PagesAdapter extends BaseAdapter implements OnItemClickListener{
ArrayList<SingleRow> pagelist;
Context context;
PagesAdapter(Context c){
context=c;
pagelist = new ArrayList<SingleRow>();
Resources res = c.getResources();
String [] pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(new SingleRow(pagedatas[i]));
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pagelist.size();
}
@Override
public Object getItem(int i) {
// TODO Auto-generated method stub
return pagelist.get(i);
}
@Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewG) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextView pagetitle = (TextView) row.findViewById(R.id.textViewRow);
SingleRow temp=pagelist.get(i);
pagetitle.setText(temp.pagedata);
return row;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int i, long arg3) {
// TODO Auto-generated method stub
}
}
I will appreciate any help given. Thank Yhu!
EDIT
Will this work?
if (index == 0) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
} else if (index == 1) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page2.html");
startActivity(i);
Edited ALL
I just got what you need, I added a filter to your baseAdapter
and then on text change within the editText
You filter the listView
and then you go to the activity needed.
here is the full code but you need to bare in mind that I have changed the following:
- I changed the pageList to ArrayList instead of
- There is a bug in filtering that when I erase what I wrote in the
EditText
it doesnt update the ListView, you need to figure out why. - I changed the returned value of the function
getItem(int i)
from Object to String - Within the
onItemClick
you have to search for the name instead of the position.
here is the code:
public class IndexPageActivity extends Activity implements OnItemClickListener{
ListView listView;
EditText editTextB;
PagesAdapter adapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
adapter1 = new PagesAdapter(this);
listView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listView.setOnItemClickListener(this);
editTextB.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
IndexPageActivity.this.adapter1.getFilter().filter(cs.toString());
adapter1.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
Intent i;
String name = adapter1.getItem(position);
Log.d("id", name);
if (name.equals("Item1"))
{
i = new Intent(this, anActivity.class);
startActivity(i);
}
else if (name.equals("Item2"))
{
i = new Intent(this, anActivity2.class);
startActivity(i);
}
}
}
class SingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
class PagesAdapter extends BaseAdapter implements Filterable{
ArrayList<String> pagelist;
List<String> arrayList;
Context context;
String [] pagedatas;
PagesAdapter(Context c){
context=c;
pagelist = new ArrayList<String>();
Resources res = c.getResources();
pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(pagedatas[i]);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pagelist.size();
}
@Override
public String getItem(int i) {
// TODO Auto-generated method stub
return pagelist.get(i);
}
@Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewG) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextView pagetitle = (TextView) row.findViewById(R.id.textViewRow);
String temp=pagelist.get(i);
pagetitle.setText(temp);
return row;
}
public class filter_here extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults Result = new FilterResults();
// if constraint is empty return the original names
if(constraint.length() == 0 ){
Result.values = pagelist;
Result.count = pagelist.size();
return Result;
}
ArrayList<String> Filtered_Names = new ArrayList<String>();
String filterString = constraint.toString().toLowerCase();
String filterableString;
for(int i = 0; i<pagelist.size(); i++){
filterableString = pagelist.get(i);
if(filterableString.toLowerCase().contains(filterString)){
Filtered_Names.add(filterableString);
}
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
return Result;
}
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
// TODO Auto-generated method stub
pagelist = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return new filter_here();
}
}
这篇关于设置onItemClick来定义适配器的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!