使用onclicklistener在单击列表视图上启动新活动

时间:2014-07-22 08:00:20

标签: android json

**我需要帮助才能在listview中进行简单的单击以使用onclicklistener启动新活动。我正在使用JSON来解析数据。我的代码是    **

 public class MainActivity extends Activity {

        private CategoryAdapter adapter;
        private CategoriesBean cb;

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ListView lv = (ListView) findViewById(R.id.list);

            cb = callCategory();

            List<Category> catList = new ArrayList<Category>();

            adapter = new CategoryAdapter(this, cb.getCategoryBn());
            lv.setAdapter(adapter);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            MenuInflater mif = getMenuInflater();
            mif.inflate(R.menu.actionbar_menu, menu);
            return super.onCreateOptionsMenu(menu);
        }

        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {

            case R.id.share:
                return true;

            case R.id.settings:
                Intent in = new Intent(MainActivity.this, AppSettings.class);
                startActivity(in);

            case R.id.feedback:
                return true;

            case R.id.about:
                return true;

            default:
                return super.onOptionsItemSelected(item);
            }

        }

        private CategoriesBean callCategory() {
            CategoriesBean cb = new CategoriesBean();

            NetworkClass netClass = NetworkClass.getInstance(MainActivity.this);
            cb = netClass.callCategory();

            return cb;
        }

        private void showData() {
            DbAdapter1 helper = DbAdapter1.getInstance(MainActivity.this);
            Cursor cur = helper.fetchQuery("select * from jokes");
        }

        public class CategoryAdapter extends BaseAdapter {

            Context context;
            List<Category> catList = new ArrayList<Category>();

            TextView categoryName;
            TextView categoryCount;

            public CategoryAdapter(Context context, List<Category> items) {
                this.context = context;
                this.catList = items;
            }

            public int getCount() {
                return catList.size();
            }

            public Object getItem(int position) {
                return catList.get(position);
            }

            public long getItemId(int position) {
                return position;
            }

            public View getView(final int position,View convertView,ViewGroup parent)
            {

                if(convertView==null)
                {
                    LayoutInflater vi =    (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
                    convertView=vi.inflate(R.layout.inflate_categoryrow,null);
                }

                categoryName = (TextView)convertView.findViewById(R.id.categoryname);
                categoryName.setText(catList.get(position).getCatName());
                categoryCount=(TextView)convertView.findViewById(R.id.categorycount);
                categoryCount.setText(catList.get(position).getCatCount());






                convertView.setOnClickListener(new OnClickListener() {
  

应该在这个onClick方法中获取列表的位置并开始一个新的活动

                    @Override
                    public void onClick(View v) {



                        Intent i=new Intent(MainActivity.this,CategoryDetail.class);


                        startActivity(i);

                    }
                });

                return convertView;
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

您可以为列表视图设置onItemClickListener,例如:

listview.setOnItemClickListener(new onItemClickListener(){
    @Override
    protected void onListItemClick(ListView parent, View v, int position, long id){
        //Do stuff
    }
});

答案 1 :(得分:0)

position发送到其他活动:

Intent i=new Intent(MainActivity.this,CategoryDetail.class);
Bundle bundle = new Bundle(); 
bundle.putInt("Position", position); //position is one of the method arguments
i.putExtras(bundle);
startActivity(i);

要在其他活动中检索此信息,请在onCreate方法上设置此行:

int position = getIntent().getExtras().getInt("Position");

答案 2 :(得分:0)

由于您的位置变量是最终的,您可以在onClick块中使用它:

@Override
public void onClick(View v) {
  //In case you need the object at the position
  Object objectAtPosition = getItem(position);

  Bundle bundle = new Bundle();
  bundle.putInt("YOUR_POSITION_EXTRA", position);
  Intent i=new Intent(MainActivity.this,CategoryDetail.class);
  i.putExtras(bundle);

  startActivity(i);

}

那里看不到任何问题。如果这不是解决方案,您可以尝试更具体地解决您的问题吗?