我正在使用6个项目(2 * 3)创建网格视图。
我正在实现此功能以动态更改设置。
因为我正在使用带有文本视图的网格视图。
my code is:
public class MyGridAdaptor extends BaseAdapter
{
String[] keyPad = { "1", "2", "3", "4", "5", "6" };
String[] functionName = { "Wi-Fi", "Bluetooth", "GPS", "Brightness", "Wi-Fi Tethering", "Mobile data", };
String[][] functionStatus = { { "OFF", "ON" }, { "OFF", "ON" }, { "OFF", "ON" }, { "OFF", "ON" }, { "OFF", "ON" }, { "OFF", "ON" } };
LayoutInflater inflater;
Context mContext;
TextView keyNumber, fName, fStatus;
RelativeLayout gridItem;
int counter = 0;
public MyGridAdaptor(Context applicationContext)
{
this.mContext = applicationContext;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
final int pos = position;
if (view == null)
{
view = inflater.inflate(R.layout.grid_item, null);
DisplayMetrics metrics = new DisplayMetrics();
metrics = mContext.getResources().getDisplayMetrics();
int height = metrics.heightPixels;
int heightAdj = 85;
view.setMinimumHeight((height - heightAdj) / 2);
keyNumber = (TextView) view.findViewById(R.id.tvNo);
fName = (TextView) view.findViewById(R.id.tvName);
fStatus = (TextView) view.findViewById(R.id.tvStatus);
gridItem = (RelativeLayout) view.findViewById(R.id.gridItem);
}
else
{
view.getTag();
}
keyNumber.setText(keyPad[position]);
fName.setText(functionName[position]);
fStatus.setText(functionStatus[position][0]);
gridItem.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
counter++;
Toast.makeText(mContext, functionName[pos]+" "+pos+" "+(counter%2)+" "+functionStatus[pos][counter%2], Toast.LENGTH_SHORT).show();
fStatus.setText(functionStatus[pos][counter%2]);
notifyDataSetChanged();
}
});
return view;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return keyPad.length;
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return keyPad[position];
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
}
我在 Toast 获得了正确的值,但 Textview 中的值没有变化。
答案 0 :(得分:0)
使用以下代码:
gridItem.setTag(position); // Added by Sagar Zala
gridItem.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
int pos=(Integer)v.getTag(); // Added by Sagar Zala
counter++;
Toast.makeText(mContext, functionName[pos]+" "+pos+" "+(counter%2)+" "+functionStatus[pos][counter%2], Toast.LENGTH_SHORT).show();
fStatus.setText(functionStatus[pos][counter%2]);
notifyDataSetChanged();
}
});
答案 1 :(得分:0)
试试这个,
public class MainActivity extends Activity {
Rectangle rectangle;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
rectangle = new Rectangle(this);
setContentView(rectangle);
}
}
class Rectangle extends View {
Paint paint;
Rectangle(Context context) {
super(context);
paint = new Paint();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
new Larger_Rectangle(getContext());
return true;
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.RED);
canvas.drawRect(10, 30, 50, 70, paint);
}
}
class Larger_Rectangle extends View {
Paint paint;
Larger_Rectangle(Context context) {
super(context);
paint = new Paint();
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLUE);
canvas.drawRect(60,100,120,150,paint);
}
}
答案 2 :(得分:0)
你为什么这样做?将onitemselected的侦听器设置为gridview,并在项目单击时,将单击特定网格项的索引。使用索引调用getChildAtIndex(网格选择索引)。这将返回相对布局并调用relativelayout.getChildAtIndex(3)。这是你的textview。更新文本并享受
答案 3 :(得分:0)
请尝试以下代码:
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
/*
ImageView checkImage= (ImageView) view.findViewById(R.id.image);
//checkImage.setTag(R.drawable.checkimg);
//checkImage.setImageResource(R.drawable.checkimg);
checkImage.setBackgroundDrawable(new PaintDrawable(Color.BLUE));
PaintDrawable drawable = (PaintDrawable) checkImage.getBackground();
int color = drawable.getPaint().getColor();
int drawableId = (Integer) checkImage.getTag();
Log.d("getTag",String.valueOf(color));
*/
TextView text = (TextView) view.findViewById(R.id.text);
text.setText("Selected");
//ImageView imageView = (ImageView) view;
//imageView.setImageResource(R.drawable.checkimg);
}
});`