做一個數據類這樣
package ali.search;
public class Data {
public Data() {
}
int id;
private String JobTitle;
private String Department;
private String Locationn;
private String JobRole;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getJobTitle() {
return JobTitle;
}
public void setJobTitle(String jobTitle) {
JobTitle = jobTitle;
}
public String getDepartment() {
return Department;
}
public void setDepartment(String department) {
Department = department;
}
public String getLocation() {
return Locationn;
}
public void setLocation(String location) {
Locationn = location;
}
public String getJobRole() {
return JobRole;
}
public void setJobRole(String jobRole) {
JobRole = jobRole;
}
}
製作Activity類這樣
package ali.search;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
public class SearchActivity extends Activity {
String[] jobtitles = { "Android Developer", "Tester","iOS Developer",
};
String[] departments = { "android", "testing", "iphone" };
String[] locations = { "Delhi", "Mumbai", "Noida" };
String[] jobRoles = { "develop android based apps", "test mobile apps",
"develop iphone based apps" };
private ListView listViewSearch;
private EditText editSearchView;
private SearchAdapter adapter;
private ArrayList<Data> sortedItems = new ArrayList<Data>();
private ArrayList<Data> OrignalItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listViewSearch = (ListView) findViewById(R.id.lvSearch);
editSearchView = (EditText) findViewById(R.id.edittext);
OrignalItems = getListOfType();
sortedItems.addAll(OrignalItems);
adapter = new SearchAdapter(SearchActivity.this, sortedItems);
listViewSearch.setAdapter(adapter);
editSearchView.addTextChangedListener(searchTxtChangeListener);
}
private ArrayList<Data> getListOfType() {
ArrayList<Data> dataList = new ArrayList<Data>();
for (int i = 0; i < 3; i++) {
Data data = new Data();
data.setJobTitle(jobtitles[i]);
data.setDepartment(departments[i]);
data.setLocation(locations[i]);
data.setJobRole(jobRoles[i]);
data.setId(i);
dataList.add(data);
dataList.add(data);// dulicate entry
}
return dataList;
}
private TextWatcher searchTxtChangeListener = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int textlength = editSearchView.getText().length();
sortedItems.clear();
for (int i = 0; i < OrignalItems.size(); i++) {
if (textlength <= OrignalItems.get(i).getJobTitle().length()) {
if (editSearchView
.getText()
.toString()
.equalsIgnoreCase(
(String) OrignalItems.get(i).getJobTitle().subSequence(0,
textlength))) {
sortedItems.add(OrignalItems.get(i));
}
}
}
adapter.notifyDataSetChanged();
}
};
}
最後這個自定義適配器
package ali.search;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SearchAdapter extends BaseAdapter{
Context context;
ArrayList<Data> list;
LayoutParams params;
private static LayoutInflater inflater = null;
public SearchAdapter(Context context, ArrayList<Data> list) {
this.context = context;
this.list = list;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (vi == null) {
vi = inflater.inflate(R.layout.search_list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) vi.findViewById(R.id.txtTitle);
holder.txtDepartment = (TextView) vi.findViewById(R.id.txtDepartment);
holder.txtLocation = (TextView) vi.findViewById(R.id.txtLocation);
holder.txtJobRole = (TextView) vi.findViewById(R.id.txtJobRole);
holder.layoutRelative = (RelativeLayout) vi.findViewById(R.id.layoutRelative);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.txtTitle.setText(list.get(position).getJobTitle());
holder.txtDepartment.setText(list.get(position).getDepartment());
holder.txtLocation.setText(list.get(position).getLocation());
holder.txtJobRole.setText(list.get(position).getJobRole());
return vi;
}
class ViewHolder {
RelativeLayout layoutRelative;
TextView txtTitle;
TextView txtDepartment;
TextView txtLocation;
TextView txtJobRole;
}
}
這是工作的罰款。
來源
2014-06-17 09:34:37
Ali