-1

我正在使用一个 android 应用程序。在那,首先我需要存储来自 web 服务的所有数据,然后从 db 查询数据并在 listview 中更新。

我的列表视图项目包含复选框和按钮。当我单击按钮或复选框时,它工作正常(显示 LOG)但是当我单击列表视图中的按钮时我需要启动子活动。

这是我的适配器类,

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;
    }


}

最后我的问题是当我单击列表视图中的按钮时我需要启动子活动。请告诉我如何实现这一目标。

4

2 回答 2

0

您必须使用传递给适配器的活动上下文来启动一个新活动,如下所示:

Intent intent = new Intent(context,Activity.class);
startActivity(intent);
于 2012-11-22T08:46:58.280 回答
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);

    }
于 2012-11-22T09:05:35.160 回答