我正在開發一個應用程序,並且我想過濾ListView
中的項目,但項目ID被定義爲來自另一個類(構造函數)的字符串,並且過濾器不起作用。 (什麼也沒有發生)如何爲ListView製作過濾器?
我想過濾器從這些構造函數中搜索這些字符串,但我不知道如何去做。
這裏是我的mainActivity
類:
package com.example.industrialcraft;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.example.R;
public class Items extends Activity {
private List<Items_id> myItems = new ArrayList<Items_id>();
ArrayAdapter<Items_id> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.items_industrialcraft);
populateItems();
adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.list_industrialcraft);
list.setAdapter(adapter);
registerClickCallback();
}
private void populateItems() {
myItems.add(new Items_id("Bronze Helmet", "Bronze_Helmet"));
myItems.add(new Items_id("Bronze Chestplate", "Bronze_Chestplate"));
myItems.add(new Items_id("Bronze Leggings", "Bronze_Leggings"));
myItems.add(new Items_id("Bronze Boots", "Bronze_Boots"));
myItems.add(new Items_id("Nano-Helmet", "Nano_Helmet"));
myItems.add(new Items_id("Nano-Bodyarmor", "Nano_Bodyarmor"));
myItems.add(new Items_id("Nano-Leggings", "Nano_Leggings"));
myItems.add(new Items_id("Nano-Boots", "Nano_Boots"));
myItems.add(new Items_id("QuantumSuit Helmet", "Quantumsuit_Helmet"));
myItems.add(new Items_id("QuantumSuit Bodyarmor", "Quantumsuit_Bodyarmor"));
myItems.add(new Items_id("QuantumSuit Leggings", "Quantumsuit_Leggings"));
myItems.add(new Items_id("QuantumSuit Boots", "Quantumsuit_Boots"));
myItems.add(new Items_id("Scuba Helmet", "Scuba_Helmet"));
myItems.add(new Items_id("Hazmat Suit", "Hazmat_Suit"));
myItems.add(new Items_id("Hazmat Suit Leggings", "Hazmat_Suit_Leggings"));
myItems.add(new Items_id("Rubber Boots", "Rubber_Boots"));
myItems.add(new Items_id("BatPack", "Batpack"));
myItems.add(new Items_id("CF Backpack", "CF_Backpack"));
myItems.add(new Items_id("Composite Vest", "Composite_Vest"));
myItems.add(new Items_id("Electric Jetpack", "Electric_Jetpack"));
myItems.add(new Items_id("Jetpack", "Jetpack"));
myItems.add(new Items_id("Lappack", "Lappack"));
myItems.add(new Items_id("Nightvision Goggles", "Nightvision_Goggles"));
myItems.add(new Items_id("Solar Helmet", "Solar_Helmet"));
myItems.add(new Items_id("Static Boots", "Statatic_Boots"));
myItems.add(new Items_id("Booze Barrel", "Booze_Barrel"));
myItems.add(new Items_id("Crop-Matron", "Crop_Matron"));
myItems.add(new Items_id("Crop", "Crop"));
myItems.add(new Items_id("Coffee Beans", "Coffee_Beans"));
myItems.add(new Items_id("Coffee Powder", "Coffee_Powder"));
myItems.add(new Items_id("Grin Powder", "Grin_Powder"));
myItems.add(new Items_id("Hydration Cell", "Hydration_Cell"));
myItems.add(new Items_id("Hops", "Hops"));
myItems.add(new Items_id("Stone Mug", "Stone_Mug"));
myItems.add(new Items_id("Coffee", "Coffee"));
myItems.add(new Items_id("Dark Coffee", "Dark_Coffee"));
myItems.add(new Items_id("Cropnalyzer", "Cropnalyzer"));
myItems.add(new Items_id("Electric Hoe", "Electric_Hoe"));
myItems.add(new Items_id("Fertilizer", "FErtilizer"));
myItems.add(new Items_id("Weed-EX", "Weed_EX"));
}
private class MyListAdapter extends ArrayAdapter<Items_id>{
public MyListAdapter(){
super(Items.this, R.layout.items_industrialcraft_buttons, myItems);
}
EditText search = (EditText) findViewById(R.id.search);
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.items_industrialcraft_buttons, parent, false);
}
// Find item to work with
Items_id currentItem = myItems.get(position);
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence currentItem, int arg1, int arg2, int arg3) {
// When user changed the
Items.this.adapter.getFilter().filter(currentItem);
}
@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
}
});
//Fill the view
TextView makeText = (TextView)itemView.findViewById(R.id.textId);
makeText.setText(currentItem.getId());
return itemView;
}
}
private void registerClickCallback() {
ListView list = (ListView)findViewById(R.id.list_industrialcraft);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
Items_id clicked = myItems.get(position);
//Find item
Items_id currentItem = myItems.get(position);
String abc = currentItem.getActivity();
String activitys = abc;
try{
Class classes = Class.forName("com.example.industrialcraft." + activitys);
Intent i = new Intent(Items.this, classes);
startActivity(i);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
});
}
}
這裏是我的Items_id
類:
package com.example.industrialcraft;
public class Items_id {
private String id;
private String activity;
public Items_id(String id, String activity){
super();
this.id = id;
this.activity = activity;
}
public String getId() {
return id;
}
public String getActivity() {
return activity;
}
}
這並不幫助,因爲我不將項目添加到一個只有意向,但我用一個數組創建多個活動和Intent,其中包含我想要運行的活動的名稱!這是一個問題!再次對不起英格蘭感到抱歉 – captainbuzz123
請刪除EditText search =(EditText)findViewById(R.id.search); –