如何为打开新活动的网格视图添加OnClickListener?

时间:2017-11-20 13:24:39

标签: java android gridview onclicklistener

我为gridview添加了OnCLickListener,但它返回错误。 我在哪里做错了。 我用过Intent来处理这个Listener。

Activity.java

public class AlbumsActivity extends Fragment{

    public AlbumsActivity() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        @SuppressLint("InflateParams") ViewGroup root = (ViewGroup) inflater.inflate(R.layout.grid_layout, null);
        GridView gridView=(GridView)root.findViewById(R.id.grid_view);
        gridView.setAdapter(new ImageAdapter(getActivity()));
        gridView.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Intent is what you use to start another activity
                Intent myIntent = new Intent(getActivity(), NowPlayingActivity.class);
                startActivity(Intent);
            }

        });
        return root;
    }
}

3 个答案:

答案 0 :(得分:2)

解决此问题:

Intent myIntent = new Intent(getActivity(), NowPlayingActivity.class);
startActivity(Intent);

更改为:

Intent myIntent = new Intent(getActivity(), NowPlayingActivity.class);
startActivity(myIntent);

答案 1 :(得分:1)

您应该使用import React,{Component} from 'react'; import {Field, reduxForm} from 'redux-form'; import {connect} from 'react-redux'; import {fetchCeoData} from '../../actions/CMS'; class CEOForm extends Component{ constructor(props) { super(props); this.renderTextarea = this.renderTextarea.bind(this); } componentDidMount(){ this.props.fetchCeoData(); } submit(values){ console.log(values); } componentWillReceiveProps(nextProps){ console.log(nextProps); } renderTextarea({_id, title, description, content, input}){ return( <div key={_id} className="cms-form-row"> <label htmlFor={title}>{description}</label> <textarea {...input} value={content} rows="6" cols="50"></textarea> </div> ) } render(){ return( <section id="ceo-form"> <h1 className="cms-section-header">Narzędzia CEO</h1> <div className="ceo-form-wrapper"> <form onSubmit={this.props.handleSubmit(this.submit)}> {this.props.ceoData.map((item) => { return <Field name={item.title} key={item._id} title={item.title} content={item.content} description={item.description} component={this.renderTextarea} /> })} <button type="submit">Wyślij !</button> </form> </div> </section> ) } } function mapStateToProps(state){ return{ ceoData: state.ceoData } } function loadData(store) { return store.dispatch(fetchCeoData()); } CEOForm = connect(mapStateToProps, {fetchCeoData})(reduxForm({form: 'ceo', enableReinitialize : true})(CEOForm)); export default{ loadData, component: CEOForm } 方法而不是gridView.setOnItemClickListener

另外,请在以后的帖子中添加错误详情(stacktrace)。

答案 2 :(得分:1)

你应该使用“myIntent”

 Intent myIntent = new Intent(getActivity(), NowPlayingActivity.class);
 startActivity(myIntent);