2
這是類。我想在longpress上添加上下文菜單,但作爲新手我發現我很困難。請幫忙。Android:幫助從列表視圖創建上下文菜單
private ListView lvUsers;
private ArrayList<UserBO> mListUsers;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
mListUsers = getUsers();
lvUsers = (ListView) findViewById(R.id.lv_user);
lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));
}
public ArrayList<UserBO> getUsers(){
DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(this);
try {
dbAdapter.createDataBase();
} catch (IOException e) {
Log.i("*** select ",e.getMessage());
}
dbAdapter.openDataBase();
String query="SELECT * FROM user;";
ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
dbAdapter.close();
ArrayList<UserBO> usersList = new ArrayList<UserBO>();
for (int i = 0; i < stringList.size(); i++) {
ArrayList<String> list = stringList.get(i);
UserBO user = new UserBO();
try {
user.id = Integer.parseInt(list.get(0));
user.name = list.get(1);
user.age = list.get(2);
} catch (Exception e) {
Log.i("***" + Select.class.toString(), e.getMessage());
}
usersList.add(user);
}
return usersList;
}
// ***ListAdapter***
private class ListAdapter extends ArrayAdapter<UserBO> { // --CloneChangeRequired
private ArrayList<UserBO> mList; // --CloneChangeRequired
private Context mContext;
public ListAdapter(Context context, int textViewResourceId,ArrayList<UserBO> list) { // --CloneChangeRequired
super(context, textViewResourceId, list);
this.mList = list;
this.mContext = context;
}
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
try{
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_item, null); // --CloneChangeRequired(list_item)
}
final UserBO listItem = mList.get(position); // --CloneChangeRequired
if (listItem != null) {
// setting list_item views
((TextView) view.findViewById(R.id.tv_id)).setText(listItem.getId()+"");
((TextView) view.findViewById(R.id.tv_name)).setText(listItem.getName());
((TextView) view.findViewById(R.id.tv_age)).setText(listItem.getAge()+"");
}}catch(Exception e){
Log.i(Select.ListAdapter.class.toString(), e.getMessage());
}
return view;
}
}
歡迎#1。您需要回顧以前的問題,並將所提供的答案之一標記爲正確的答案。 – 2010-11-23 23:01:50
對不起梅拉。我不知道我必須這樣做。現在已經完成了,謝謝。 – Riza 2010-11-23 23:10:49