gpt4 book ai didi

java - 如何使用Java代码在Android中将复选框添加到 'Grid View'

转载 作者:行者123 更新时间:2023-12-02 05:30:49 25 4
gpt4 key购买 nike

如何在 Android 上使用 Java 添加复选框。当我将 boolean 值添加到“CustomGrid.java”,然后将复选框添加到“MainActivity.java”时,SDK 告诉我

Item mismatch: cannot convert from int[] to boolean

我尝试了一些用Java转换它的方法,但我对这个Java不熟悉,而且我很新(大约4天)。这是我到目前为止所做的:

Mainactivity.java

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.Toast;


public static void main(String[] args) throws Exception {
int i=10;
boolean b = (i != 0);

System.out.println(b);
}

public class MainActivity extends Activity {
GridView grid;
String[] web = {
"Google",
"",
"Instagram",
"Facebook",
"Pinterest",
"Twitter",
} ;


int[] imageId = {
R.drawable.image1,
R.string.share,
R.drawable.image3,
R.drawable.image4,
R.drawable.image6,
R.drawable.image8,
};

boolean check = {
R.id.checkbox1
};

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

CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId, check);
grid=(GridView)findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
}

CustomGrid.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final int[] Imageid;
private final boolean check;
public CustomGrid(Context c,String[] web,int[] Imageid,boolean check2){
mContext = c;
this.Imageid = Imageid;
this.web = web;
this.check = check2;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
CheckBox checkbox = (CheckBox) grid.findViewById(R.id.checkbox1);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
checkbox.setChecked(check);
} else {
grid = (View) convertView;
}

return grid;
}
}

最佳答案

如果您在 Activity_main XML 布局文件(在“网格”GridView 内)中包含复选框,

    CheckBox checkbox = (CheckBox)findViewById(R.id.'yourcheckboxid')

或者您可以通过编程方式创建并添加复选框,

    CheckBox checkbox = new CheckBox();
LayoutParams checkbox_layoutparams = new GridView.LayoutParams(
wrap_content,
wrap_content);
checkbox_layoutparams.addRule(GridView.'your rule')://add rules if need be
grid.addView(checkbox,checkbox_layoutparams);

希望这有帮助...

关于java - 如何使用Java代码在Android中将复选框添加到 'Grid View',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25554444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com