gpt4 book ai didi

java - 将 onItemClick 设置为自定义适配器 ListView

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:55 26 4
gpt4 key购买 nike

当我在尝试将我的 listView 项目的位置固定为过滤时所需的 Intent 时遇到问题,并获得信息我可以使用自定义适配器覆盖该问题,我已经做到了,但我不知道如何分配点击次数对于每个项目,请检查以下代码:

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

}
}

我将不胜感激任何帮助。谢谢宇!

编辑

这行得通吗?

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

最佳答案

全部编辑

我刚得到你需要的东西,我在你的 baseAdapter 中添加了一个过滤器,然后在 editText 中的文本更改你过滤了 listView然后你去参加需要的 Activity 。

这是完整的代码,但您需要记住我已经更改了以下内容:

  • 我将 pageList 改为 ArrayList 而不是
  • 过滤时有一个错误,当我删除我在 EditText 中写的内容时,它不会更新 ListView,您需要找出原因。
  • 我将函数 getItem(int i) 的返回值从 Object 更改为 String
  • onItemClick 中,您必须搜索名称而不是位置。

代码如下:

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

关于java - 将 onItemClick 设置为自定义适配器 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20127922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com