Android Gridview选择无效

时间:2015-07-23 12:41:11

标签: android gridview

当点击gridview中的图标时,它显示为“你点击了这个”。 相反,我需要转到与页面对应的新页面。

这是我的代码

mainactivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gv=(GridView) findViewById(R.id.gridView1);
    gv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

和customadapter.java

public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {

    result=prgmNameList;
    context=mainActivity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView tv;
    ImageView img;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.programlist, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);

    rowView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });

    return rowView;
}

}

我应该做些什么改变?

4 个答案:

答案 0 :(得分:0)

在您的主要活动中实施AdapterView.OnItemClickListener,并将侦听器设置为Gridview为gridview.setOnItemClickListener(this);。然后,您将更好地处理每个项目上的点击事件。

另请参阅此链接

onClick for GridView

答案 1 :(得分:0)

在rowView OnclicListener

中使用此代码
 rowView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
Intent i = new Intent(context, secondActivity.class);     
       startActivity(i);
        }
    });

答案 2 :(得分:0)

在您的代码中

而不是显示toast启动新的片段或活动。

    @Override
            public void onClick(View v) {
                // comment below line which display toast and un comment start acitivity
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
               // startActivity(new Intent(context,SecondActivity.class));
            }

答案 3 :(得分:0)

不是在rowView上附加onClickListener,而是在GridView上设置OnItemClickListener

gv.setOnItemClickListener(new OnItemClickListener() 
{
     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3)
     {
          String value=prgmNameList[index];
          Intent intent = new Intent(this, NextActivity.class);
          startActivity(intent);
     }
});