以下是我的代码,需要实现网格视图。
package com.example.dhavalpatel.atira;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.TableLayout;
import android.widget.Toast;
public class second extends ActionBarActivity
{
GridView gv;
String[] value1=value1=new String[]{"0","0","0","0","0"};
String imc_met;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
gv=(GridView)findViewById(R.id.gridView);
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,value1);
gv.setAdapter(ad);
}
问题:
我希望能够找到在网格视图上单击的单元格,并能够从所选单元格中检索其中的相应值。
答案 0 :(得分:1)
您必须使用OnItemClickListener,如下所示
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//based on the position you have to get value
String value=value1[position];
}
});
更新:
是的,您可以更改您选择的单元格的值,如下所示
在setOnItemClickListener的onItemClick( - , - , - , - )中写如下
value1[position]=234;
ad.notifyDatasetChanged();
希望这会对你有所帮助。
答案 1 :(得分:0)
尝试使用方法gridView.getItemAtPosition(position)
和类似的东西:
@Override
protected void onListItemClick(ListView l, View v, int position, long ida) {
super.onListItemClick(l, v, position, ida);
Cursor mycursor = (Cursor) getListView().getItemAtPosition(position);
//and then get the values like mycursor.getString(1) for example
}
答案 2 :(得分:-1)
您需要在gridView上绑定click事件,如as。
gv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) v).getText(), Toast.LENGTH_SHORT).show();
}
});