当我搜索列表视图项,然后单击愿望项目,但它的列表视图中打开第一个位置 [英] When I search listview item and then click desire item, it does open first position of List View
问题描述
我检索国家的ListView。当我点击国家列表项目,而不搜索,它的正常工作。但是当我在搜索框中键入巴西和巴西单击,然后打开Country_Details.Java活动,并通过阿巴边境的细节。
当我在搜索框中键入巴西和巴西的点击,我想OT传递的WebView巴西的细节。
结果展开来源$ C $ C : https://开头的车程。 google.com/open?id=0B46bPR7LKLjpZFRhcV9DdmIweTQ
结果在这里,我的code。
包com.nasir.search; 进口的java.util.ArrayList;
进口java.util.Arrays中; 进口android.app.Activity;
进口android.app.ListActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.BaseAdapter;
进口android.widget.EditText;
进口android.widget.ListView;
进口android.widget.TextView;
公共类Search_Country扩展ListActivity { 私人的EditText SEARCHTEXT;
私人的ListView ListText; 私有String [] = Number_List {阿富汗,阿尔巴尼亚,阿尔及利亚,巴西}; 私人的ArrayList<串GT; array_sort;
INT长度限制:Textlength = 0; 公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.search_country); SEARCHTEXT =(EditText上)findViewById(R.id.listview_search);
ListText =(ListView控件)findViewById(android.R.id.list); array_sort =新的ArrayList<串GT;(Arrays.asList(Number_List));
setListAdapter(新bsAdapter(本)); SearchText.addTextChangedListener(新TextWatcher()
{
公共无效afterTextChanged(编辑S)
{ } 公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT)
{ } 公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数)
{
。=长度限制:Textlength SearchText.getText()长();
array_sort.clear();
的for(int i = 0; I< Number_List.length;我++)
{
如果(与长度限制:Textlength LT = Number_List [I]。长度())
{
如果(Number_List [I] .toUpperCase()。包括(SearchText.getText()。toString()方法。与toUpperCase()。修剪()))
{
array_sort.add(Number_List [I]);
}
}
}
AppendList(array_sort);
}
}); ListText.setOnItemClickListener(新OnItemClickListener()
{
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
开关(位置)
{
情况下0:意向意图=新意图(Search_Country.this,Country_Details.class);
intent.putExtra(头的getString(R.string.html_afganistan));
startActivity(意向);
打破;
案例1:意向intent1 =新意图(Search_Country.this,Country_Details.class);
intent1.putExtra(头的getString(R.string.html_albenia));
startActivity(intent1);
打破;
案例2:意向intent2 =新意图(Search_Country.this,Country_Details.class);
intent2.putExtra(头的getString(R.string.html_algeria));
startActivity(intent2);
打破; 案例3:意向intent3 =新意图(Search_Country.this,Country_Details.class);
intent3.putExtra(头的getString(R.string.html_brazil));
startActivity(intent3);
打破; }
}
});
} 公共无效AppendList(ArrayList的<串GT; STR)
{
setListAdapter(新bsAdapter(本));
} 公共类bsAdapter延伸BaseAdapter
{
活动CNTX; 公共bsAdapter(Activity上下文)
{
this.cntx =背景;
} 公众诠释getCount将()
{
返回array_sort.size();
} 公共对象的getItem(INT位置)
{
返回array_sort.get(位置);
} 众长getItemId(INT位置)
{
返回array_sort.size();
} 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
{
查看排= NULL;
LayoutInflater吹气= cntx.getLayoutInflater();
行= inflater.inflate(R.layout.search_country_listview,NULL);
TextView的电视=(TextView中)row.findViewById(R.id.listview_seacrh_text);
tv.setText(array_sort.get(位置));
返回行;
}
}
}
问题是在这里,你是静态位调用web视图,只是调用相应的数组列表
像下面。
如果(array_list.get(位置).equals(阿富汗)){
意向意图=新意图(Search_Country.this,Country_Details.class); intent.putExtra(头的getString(R.string.html_afganistan)); startActivity(意向);
}
I have Searchable country ListView. When I click Country List Item without search, it's works properly. but when I type Brazil in search box and click Brazil, then open Country_Details.Java Activity and pass Afganistan details.
when I type Brazil in Search box and click Brazil, I want ot pass Brazil details in WebView.
Full Source Code : https://drive.google.com/open?id=0B46bPR7LKLjpZFRhcV9DdmIweTQ
Here My Code.
package com.nasir.search;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class Search_Country extends ListActivity {
private EditText SearchText;
private ListView ListText;
private String[] Number_List = { "Afghanistan", "Albania", "Algeria", "Brazil"};
private ArrayList<String> array_sort;
int textlength = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.search_country);
SearchText = (EditText) findViewById(R.id.listview_search);
ListText = (ListView) findViewById(android.R.id.list);
array_sort = new ArrayList<String>(Arrays.asList(Number_List));
setListAdapter(new bsAdapter(this));
SearchText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
textlength = SearchText.getText().length();
array_sort.clear();
for (int i = 0; i < Number_List.length; i++)
{
if (textlength <= Number_List[i].length())
{
if(Number_List[i].toUpperCase().contains(SearchText.getText().toString().toUpperCase().trim()))
{
array_sort.add(Number_List[i]);
}
}
}
AppendList(array_sort);
}
});
ListText.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent intent = new Intent(Search_Country.this, Country_Details.class);
intent.putExtra("header", getString(R.string.html_afganistan));
startActivity(intent);
break;
case 1: Intent intent1 = new Intent(Search_Country.this, Country_Details.class);
intent1.putExtra("header", getString(R.string.html_albenia));
startActivity(intent1);
break;
case 2: Intent intent2 = new Intent(Search_Country.this, Country_Details.class);
intent2.putExtra("header", getString(R.string.html_algeria));
startActivity(intent2);
break;
case 3: Intent intent3 = new Intent(Search_Country.this, Country_Details.class);
intent3.putExtra("header", getString(R.string.html_brazil));
startActivity(intent3);
break;
}
}
});
}
public void AppendList(ArrayList<String> str)
{
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
this.cntx = context;
}
public int getCount()
{
return array_sort.size();
}
public Object getItem(int position)
{
return array_sort.get(position);
}
public long getItemId(int position)
{
return array_sort.size();
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = null;
LayoutInflater inflater = cntx.getLayoutInflater();
row = inflater.inflate(R.layout.search_country_listview, null);
TextView tv = (TextView) row.findViewById(R.id.listview_seacrh_text);
tv.setText(array_sort.get(position));
return row;
}
}
}
Problem is here, you are call webview on static position, instead you call accordingly your array list Like below.
if( array_list.get(position).equals("Afghanistan")){
Intent intent = new Intent(Search_Country.this, Country_Details.class); intent.putExtra("header", getString(R.string.html_afganistan)); startActivity(intent);
}
这篇关于当我搜索列表视图项,然后单击愿望项目,但它的列表视图中打开第一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!