嗨,我是安卓新手。
1)我有一个带有名称和复选框的列表视图。我想在此列表上有一个搜索功能,用户可以在其中搜索特定名称。
2)我希望用户能够一键选择列表视图中的所有名称。当用户按下 selectAll 按钮时,应选中所有复选框。
我是一个完全的初学者,完全糊涂了。
谢谢
public class NewstipsActivity extends Activity {
CheckboxAdapter listItemAdapter;
ArrayList<String> listData;
Button getValue;
Button getchoice;
Button selectAll;
EditText edt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getchoice = (Button) findViewById(R.id.getchoice);
getValue = (Button)findViewById(R.id.get_value);
selectAll = (Button)findViewById(R.id.selectAll);
edt=(EditText)findViewById(R.id.EditText01);
ListView list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String,Object>>();
JSONObject json = getJSON.getJSONfromURL("http://test.com/myurl.php");
try {
JSONArray results = json.getJSONArray("results");
for(int i = 0; i < results.length(); i++){
// creating new HashMap
HashMap<String, Object> map=new HashMap<String, Object>();
JSONObject e = results.getJSONObject(i);
// adding each child node to HashMap key => value
map.put("n_id", String.valueOf(i));
map.put("friend_image", R.drawable.icon);
map.put("friend_username", " " + e.getString("n_newspaper"));
map.put("friend_id", " " + e.getString("n_id"));
map.put("selected", false);
listData.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
listItemAdapter = new CheckboxAdapter(this, listData);
list.setAdapter(listItemAdapter);
//problem1 search
list.setTextFilterEnabled(true);
edt.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged( CharSequence s, int arg1, int arg2, int arg3)
{
Toast.makeText (NewstipsActivity.this, "On Search", Toast.LENGTH_LONG).show();
}
@Override
public void beforeTextChanged( CharSequence arg0, int arg1, int arg2, int arg3)
{
}
@Override
public void afterTextChanged( Editable arg0)
{
}
});
getValue.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HashMap<Integer, Boolean> state =listItemAdapter.state;
String options="Newspapers: ";
for(int j=0; j<listItemAdapter.getCount(); j++){
System.out.println("state.get("+j+")=="+state.get(j));
if(state.get(j)!=null){
@SuppressWarnings("unchecked")
HashMap<String, Object> map=(HashMap<String, Object>) listItemAdapter.getItem(j);
String username=map.get("friend_username").toString();
String id=map.get("friend_id").toString();
options+="\n"+id+"."+username;
}
}
Toast.makeText(getApplicationContext(), options, Toast.LENGTH_LONG).show();
Intent intent = new Intent(NewstipsActivity.this, NewstipsPhoto.class);
startActivity(intent);
}
});
getchoice.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NewstipsActivity.this, NewstipsPhoto.class);
startActivity(intent);
}
});
selectAll.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
public class CheckboxAdapter extends BaseAdapter {
Context context;
ArrayList<HashMap<String, Object>> listData;
HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();
public CheckboxAdapter(Context context, ArrayList<HashMap<String, Object>> listData) {
this.context = context;
this.listData = listData;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.item, null);
ImageView image = (ImageView) convertView.findViewById(R.id.friend_image);
image.setBackgroundResource((Integer) listData.get(position).get("friend_image"));
TextView username = (TextView) convertView.findViewById(R.id.friend_username);
username.setText((String) listData.get(position).get("friend_username"));
TextView id = (TextView) convertView.findViewById(R.id.friend_id);
id.setText((String) listData.get(position).get("friend_id"));
CheckBox check = (CheckBox) convertView.findViewById(R.id.selected);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
state.put(position, isChecked);
} else {
state.remove(position);
}
}
});
check.setChecked((state.get(position) == null ? false : true));
return convertView;
}
}
}