[英]How to use search function in my custom listview(fetching data from website and display in listview)
我有列表視圖,它從網站獲取數據並顯示在列表視圖中,我想使用搜索功能,在我的列表視圖中我擁有標題和內容。我想僅使用內容進行搜索就足夠了。如何為我的列表視圖使用搜索功能。
它在這一行顯示問題List newListTwo = new List(); 錯誤是無法實例化類型列表
Myactivity.java
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
lv1 =(ListView)findViewById(R.id.list);
lv =(ListView)findViewById(R.id.list);
btnGetSelected = (Button) findViewById(R.id.btnget);
btnGetSelected.setOnClickListener(this);
myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
String selection = myFilter.getText().toString().toLowerCase();
List<Application> newListTwo=new List<Application>();
int textlength = selection.trim().length();
System.err.println("selection" + textlength);
newListTwo.clear();
for (int i = 0; i < items.size(); i++)
{
// -------------- seach according to the content starts with -------------------------
if(items.get(i).getContent().toLowerCase().startsWith(selection))
{
System.err.println("Selection: " + selection);
newListTwo.add(items.get(i));
}
}
//---------------- Again Call your List View ------------------
adapter=new ApplicationAdapter(MainActivity.this, newListTwo);
setListAdapter(adapter);
}
private void setListAdapter(ApplicationAdapter adapter) {
// TODO Auto-generated method stub
}
});
//praycount.setOnClickListener(this);
initView();
}
private void initView(){
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://www.ginfy.com/api/v1/posts.json";
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
對於我的代碼如何使用搜索功能,我也有我的applicaton.java和applicationadapter.java
Applicationadapter.java
@SuppressLint("NewApi")
public class ApplicationAdapter extends ArrayAdapter<Application> implements
TextToSpeech.OnInitListener{
private List<Application> items;
private LayoutInflater inflator;
private MainActivity activity;
private ProgressDialog dialog;
public TextToSpeech tts;
public ImageButton btnaudioprayer;
public TextView text1;
ArrayAdapter<String> adapter;
public ApplicationAdapter(MainActivity context, List<Application> items){
super(context, R.layout.activity_row, items);
this.items = items;
inflator = LayoutInflater.from(getContext());
activity=context;
}
@Override
public int getCount(){
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
tts = new TextToSpeech(activity, ApplicationAdapter.this);
//View v = convertView;
if ( convertView == null ){
convertView = inflator.inflate(R.layout.activity_row, null);
holder = new ViewHolder();
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.count = (TextView) convertView.findViewById(R.id.count);
holder.pray = (Button) convertView.findViewById(R.id.pray);
holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
holder.btnaudioprayer = (ImageButton) convertView.findViewById(R.id.btnaudioprayer);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view,
boolean isChecked) {
int getPosition = (Integer) view.getTag();
items.get(getPosition).setSelected(view.isChecked());
}
});
holder.pray.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int getPosition= (Integer)v.getTag();
StringBuffer sb1 = new StringBuffer();
sb1.append("ID :");
sb1.append(Html.fromHtml(""+items.get(getPosition).getId()));
sb1.append("\n");
activity.praydata(items.get(getPosition).getId());
//activity.showAlertView(sb1.toString().trim());
//activity.praydata(Integer.parseInt(sb1.toString().trim()));
}
});
holder.btnaudioprayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View V) {
View parent = (View)V.getParent();
ViewHolder vh = (ViewHolder)parent.getTag();
TextView tv = vh.text1;
speakOut(tv.getText().toString());
}
});
Application app = items.get(position);
holder.chk.setTag(position);
holder.pray.setTag(position);
holder.text2.setText(Html.fromHtml(app.getTitle()));
holder.text1.setText(Html.fromHtml(app.getContent()));
holder.count.setText(app.getCount()+"");
holder.chk.setChecked(app.isSelected());
return convertView;
}
static class ViewHolder {
public TextView text2;
public TextView text1;
public TextView count;
public CheckBox chk;
public Button pray;
public ImageButton btnaudioprayer;
private TextToSpeech tts;
}
//return convertView;
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
使用addTextChangeListener根據內容顯示列表:
searchContentEditText.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)
{
selection= searchContentEditText.getText().toString().toLowerCase();
List newListTwo=new ArrayList();
textlength = selection.trim().length();
System.err.println("selection" + textlength);
newListTwo.clear();
for (int i = 0; i < contactList.size(); i++)
{
// -------------- seach according to the content starts with -------------------------
if(firstList.get(i).getContent().toLowerCase().startsWith(selection))
{
System.err.println("Selection: " + selection);
newListTwo.add(firstList.get(i));
}
}
//---------------- Again Call your List View ------------------
adapter=new ContactListAdapter(MyActivity.this, newListTwo);
setListAdapter(adapter);
}
});
您需要使用TextWatcher實現MyActivity.java類。
public class MyActivity extends Activity implements TextWatcher {
myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(this);
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Group[] group_array = null;
this.array_sort = new ArrayList<Group>();
try {
if(user != null) {
group_array = this.user.getUser_groups().getData();
}
int textlength = this.search_bar.getText().length();
for(int i=0; i<group_array.length; i++) {
if(textlength <= group_array[i].getGroup_name().length()) {
if(((String) group_array[i].getGroup_name().subSequence(0, textlength))
.equalsIgnoreCase(this.search_bar.getText().toString()))
{
this.array_sort.add(group_array[i]);
}
}
}
if(this.array_sort.size() > 0) {
Group[] groups = new Group[this.array_sort.size()];
FetchGroups.this.group_adapter = new GroupAdapter(FetchGroups.this,R.layout.row_fetch_groups,
android.R.layout.simple_list_item_1, this.array_sort.toArray(groups));
FetchGroups.this.groups_list.setAdapter(group_adapter);
}
} catch(Exception e) {
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.