我正在使用Android应用程序。在那里,首先我需要存储来自webservice的所有数据,然后从db查询数据并在listview中更新。
我的listview项目包含复选框和按钮。当我单击按钮或复选框时,它正在工作(显示LOG),但是当我单击列表视图中的按钮时,我需要启动子活动。
这是我的Adapter类,
public class GuestListAdapter extends BaseAdapter{
Context context;
ArrayList<String > arrayListFirstValue;
ArrayList<String > arrayListSecondValue;
ArrayList<String > arrayListThirdValue;
public GuestListAdapter(Context mcontext, ArrayList<String> arrListFV,ArrayList<String> arrListSV,ArrayList<String> arrListGuestTV)
{
arrayListFirstValue =arrListFV;
arrayListSecondValue =arrLisSV;
arrayListThirdValue =arrListTV;
context = mcontext;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayListFirstValue.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayListFirstValue.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.guest_list_item, parent, false);
}
TextView txtFirstValue = (TextView)view.findViewById(R.id.txt_firstname);
txtFirstValue .setText(arrayListFirstValue.get(position));
TextView txtSecondValue = (TextView)view.findViewById(R.id.txt_lastname);
txtSecondValue .setText(arrayListSecondValue.get(position));
TextView txtThirdValue = (TextView)view.findViewById(R.id.txt_guest_count);
txtThirdValue .setText(arrayListThirdValue.get(position));
Button btnInfo = (Button)view.findViewById(R.id.btn_info);
CheckBox checkBoxCheckins = (CheckBox)view.findViewById(R.id.checkBoxIsCheckedIn);
btnInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Adap button **********");
}
});
checkBoxCheckins.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println("Adap checked **********");
}
});
return view;
}
}
最后我的问题是,当我点击listview中的按钮时,我需要启动子活动。 请告诉我如何实现这一目标。
答案 0 :(得分:0)
您有责任使用传递给适配器的活动上下文来启动新活动,如下所示:
Intent intent = new Intent(context,Activity.class);
startActivity(intent);
答案 1 :(得分:0)
这很简单。只需将您当前的监听器更改为:
修改:因为您最初没有在标签的子活动中告诉该列表视图。
btnInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, YourNewTabActivity.class);// Your tab parent activity.
intent.putExtra(Constants.TAB_INDEX, tab index number) // pass the index of the tab you want to move to.
startActivity(intent);
}
});
在标签活动(父活动)中,您可以创建一个变量来存储所需标签的索引
private int tabindex = 0;
在onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_screen);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabWidget = getTabWidget();
if (getIntent().getExtras() != null) {
tabindex = getIntent().getExtras().getInt(Constants.TAB_INDEX,
0); // you check if this parent started from other activity. You child activity will pass the desire tab index here. If nothing is special, just display the 1st tab.
}
initTab();
}
}
protected void initTab() {
Intent intent;
TabHost.TabSpec spec;
// Add tab child
spec = tabHost.newTabSpec("Inbox").setIndicator(
getTabIndicatorView(MainTabActivity.this, "Hộp thư",
R.drawable.tin_nhan_tab_selector));
intent = new Intent(this, com.vtcmobile.ui.InboxActivity.class);
// intent.putExtra(Constants.INTENT_IS_LOAD, isload);
spec.setContent(intent);
tabHost.addTab(spec);
....
tabHost.setCurrentTab(tabindex); // use the tab index to get the right tab you want.
tabHost.getTabWidget().focusCurrentTab(tabindex);
}