0

如何让用户单击列表菜单中的选定项目,选择后将意图转到另一个页面。我知道这个问题在stackoverflow中被问了很多,但我已经经历了大部分,我仍然迷路。希望我能在这里得到一些帮助。

示例:当用户在列表中选择“a”时,它将转到另一个活动我如何onclick listener在此代码中实现?

编辑后,我的意图以另一种方式不起作用。
我在我的主程序上添加了代码,但是当我运行程序时,我仍然无法点击

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // write your start another activity code here
            switch(i){
            case 0:{
                Intent intent1 = new Intent(LeftAndRightActivity.this, DisplayActivity.class);
                startActivity(intent1);
            }
            }
        }
    });

rightlistfragment.xml

public class RightListFragment extends ListFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.rightlist, null);

}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SampleAdapter adapter = new SampleAdapter(getActivity());
    adapter.add(new SampleItem("a"));
    adapter.add(new SampleItem("b"));
    adapter.add(new SampleItem("c"));
    adapter.add(new SampleItem("d"));
    adapter.add(new SampleItem("e"));
    adapter.add(new SampleItem("f"));
    setListAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // write your start another activity code here
            switch(i){
            case 0:{
                startActivity(new Intent(RightListFragment.this,FragmentBindAcc.class));
            }
            }
        }       

    TextView text1 = (TextView) getView().findViewById(R.id.loginName);

    text1.setText("TEST");
}

private class SampleItem {
    public String tag;
    public int iconRes;
    public SampleItem(String tag) {
        this.tag = tag; 

    }
}

public class SampleAdapter extends ArrayAdapter<SampleItem> {

    public SampleAdapter(Context context) {
        super(context, 0);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.rightrow, null);
        }
        ImageView icon = (ImageView) convertView.findViewById(R.id.arrow);
        icon.setImageResource(getItem(position).iconRes);
        TextView title = (TextView) convertView.findViewById(R.id.row_title);
        title.setText(getItem(position).tag);


        return convertView;
    }

}
   }

谢谢

4

6 回答 6

2

您可以通过以下方式获得此setOnItemClickListener信息ListView

list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            // TODO Auto-generated method stub
            switch(arg2){
            case 0:{  

                    startActivity(new Intent(getApplicationContext(),ActivityA.class));                     
                }
            break;  
            case 1:{

                    startActivity(new Intent(getApplicationContext(),ActivityB.class));
                                    }   
            break;
            case 2:{  
                ExampleQuestion.topicNo=3;                          
                startActivity(new Intent(getApplicationContext(),ActivityC.class));                     
            }
            break;              
}

您可以将其视为listview包含 A、B 和 C。当用户选择 A 时,它是案例 0,对于 B 案例 2 和对于 C 案例 3。

于 2013-10-29T07:17:18.247 回答
0
RightListFragment rightListFragment = new RightListFragment();
rightListFragment.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int index, long l)         
            {
                swtich(index)
                {
                   // your implementaion
                }
            }
        });
于 2013-10-29T07:26:51.547 回答
0

使用此代码

列表视图 .setOnItemClickListener(new OnItemClickListener()

{

    @Override

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

        // TODO Auto-generated method stub



        if(arg2==0){  

                startActivity(new Intent(yourclass.this,ActivityA.class));                     
            }

     if(arg2=1){

                startActivity(new Intent(yourclass.this,ActivityB.class));}
  if(arg2==2)
      {  

            startActivity(new Intent(yourclass.this,ActivityC.class));                     
        }


        }` 
于 2013-10-29T08:56:05.070 回答
0
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        /* your code */
    };
})
于 2013-10-29T07:12:29.423 回答
0
// try this way
public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        SampleAdapter adapter = new SampleAdapter(getActivity());
        adapter.add(new SampleItem("a"));
        adapter.add(new SampleItem("b"));
        adapter.add(new SampleItem("c"));
        adapter.add(new SampleItem("d"));
        adapter.add(new SampleItem("e"));
        adapter.add(new SampleItem("f"));
        setListAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                // write your start another activity code here
            }
        });
        TextView text1 = (TextView) getView().findViewById(R.id.loginName);

        text1.setText("TEST");
}
于 2013-10-29T07:10:19.440 回答
0

尝试在 getView 方法中提供 onClickListner

  title.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent i = new Intent(context,anotherActivity.class);
                                      context.startActivity(i);

                        }
                    });
于 2013-10-29T07:10:26.690 回答