2014-08-28 31 views
0

如何在Android上使用Java添加複選框。當我添加一個boolean值以「CustomGrid.java」,然後將複選框添加到我的「MainActivity.java」的SDK告訴我如何使用Java代碼在Android中將複選框添加到「Grid View」中

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; 
    } 
} 

回答

1

如果您包括您的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); 

希望這有助於...

1

R.id.checkbox1

只是一個指向'checkbox1'CheckBox類型View的整數。因此,您收到的類型不匹配錯誤。

boolean check = { 
     R.id.checkbox1 

就需要看更多的東西是這樣的:

CheckBox checkbox = (CheckBox) grid.findViewById(R.id.checkbox1); 
boolean check = checkbox.isChecked(); 
+0

謝謝你,但我可能做錯了什麼。我添加了「布爾檢查= checkbox.isChecked();」但我的MainActivity.java不會檢測到它,並提供相同的錯誤,以及那個,sdk警告我局部變量「檢查」沒有被使用(所以我認爲它是因爲MainActivity.java沒有檢測到它)什麼出錯了,並再次感謝您試圖幫助 – user256272 2014-08-29 15:02:58

+0

我的回答不僅僅是一個解決方案的建議,而只是一個跳出我的錯誤。稍後我會看看你的代碼,看看我能否給你更完整的幫助! – 2014-08-29 16:23:03

+0

謝謝! – user256272 2014-08-29 16:41:37

相關問題