Android GridLayout Row索引

时间:2012-12-17 08:07:11

标签: android checkbox toast grid-layout

我有一个类似下面代码的设计版面,我想为每个复选框编写一个代码,以便在选中时向我显示所选行的索引,但我不知道如何动态地从gridlayout获取此属性而不是在Toast.makeText()中手动输入索引。如果你在这个问题上帮助我,我将不胜感激。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:columnCount="5"
  android:rowCount="4" 
  android:orientation="horizontal" 
  android:background="#FF1354"
  android:id="@+id/Grd">
  <TextView android:text=" R 1, C 1 " 
       android:background="@drawable/border" />

  <TextView 
      android:text=" R 1, C 2 "
       android:background="@drawable/border"/>

  <TextView android:text=" R 1, C 3 "
       android:background="@drawable/border" />

  <TextView android:text=" R 1, C 4 "
      android:background="@drawable/border" />

  <CheckBox 
      android:id="@+id/chk1"
      android:text=""
     style="?android:attr/starStyle"  />

  <TextView android:text=" R 2, C 1 "
      android:background="@drawable/border" />

  <TextView android:text=" R 2, C 2 "
      android:background="@drawable/border" />

  <TextView android:text=" R 2, C 3 "
      android:background="@drawable/border" />

  <TextView android:text=" R 2, C 4 "
       android:background="@drawable/border" />

  <CheckBox
      android:id="@+id/chk2"
       android:text=""
   style="?android:attr/starStyle"  />

  <TextView android:text=" R 3, C 1 "
       android:background="@drawable/border" />
  <TextView android:text=" R 3, C 2 "
      android:background="@drawable/border" />

  <TextView android:text=" R 3, C 3 "
       android:background="@drawable/border" />

  <TextView android:text=" R 3, C 4 " 
      android:background="@drawable/border"/>

  <CheckBox 
      android:id="@+id/chk3"
      android:text=""
       style="?android:attr/starStyle"/>

  <TextView android:text=" R 4, C 1 "
    android:background="@drawable/border" />

  <TextView android:text=" R 4, C 2 "
      android:background="@drawable/border" />

  <TextView android:text=" R 4, C 3 "
       android:background="@drawable/border" />

  <TextView android:text=" R 4, C 4 "
      android:background="@drawable/border" />

  <CheckBox 
      android:id="@+id/chk4"
      android:text=""
      style="?android:attr/starStyle" />

</GridLayout>

这是我的代码:

package com.example.hiiiiiiiiiiiiii;

import com.example.mynewtestoftablecheck.MyNewTestTableCheck;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.Toast;

public class Hiiiiiiiiiiiiiiiiiiiiiiii extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hiiiiiiiiiiiiiiiiiiiiiiii);


      CheckBox chk1=(CheckBox)findViewById(R.id.chk1);
      chk1.setOnClickListener(new View.OnClickListener() {
          GridLayout Grd=(GridLayout)findViewById(R.id.Grd);


        @Override
        public void onClick(View arg0) {
            {
                if (((CheckBox) arg0).isChecked()) {
                    Toast.makeText(Hiiiiiiiiiiiiiiiiiiiiiiii.this,
                               "1st Row checked", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(Hiiiiiiiiiiiiiiiiiiiiiiii.this,
                            "1st Row Unchecked", Toast.LENGTH_LONG).show();
        }
                }

            }
        });



      CheckBox chk2=(CheckBox)findViewById(R.id.chk2);
      CheckBox chk3=(CheckBox)findViewById(R.id.chk3);
      CheckBox chk4=(CheckBox)findViewById(R.id.chk4);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_hiiiiiiiiiiiiiiiiiiiiiiii, menu);
        return true;
    }
}

1 个答案:

答案 0 :(得分:1)

grid.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:columnCount="5"
  android:rowCount="4" 
  android:orientation="horizontal" 
  android:background="#FF1354"
  android:id="@+id/Grd">
</GridLayout>

单独item_grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

  <TextView android:text=" R 1, C 1 " 
       android:background="@drawable/border" />

  <TextView 
      android:text=" R 1, C 2 "
       android:background="@drawable/border"/>

  <TextView android:text=" R 1, C 3 "
       android:background="@drawable/border" />

  <TextView android:text=" R 1, C 4 "
      android:background="@drawable/border" />

  <CheckBox 
      android:id="@+id/chk1"
      android:text=""
     style="?android:attr/starStyle"  />

</LinearLayout>

然后你自定义grig适配器

public class GridviewAdapter extends BaseAdapter {

    private Activity activity;
    ArrayList<HashMap<String, String>> mapList;

    public GridviewAdapter(Activity activity,
            ArrayList<HashMap<String, String>> mapList_) {
        super();
        this.mapList = mapList_;
        this.activity = activity;

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return mapList.size();
    }

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

    public static class ViewHolder {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.item_grid, null);

            view.txtViewTitle = (TextView) convertView
                    .findViewById(R.id.myImageViewText);



            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText("NAME");


        return convertView;
    }
你活动中的

adapter = new GridviewAdapter(this, mapList);
        // Set custom adapter to gridview
        gridView = (GridView) findViewById(R.id.gvMain);
        gridView.setNumColumns(GridviewAdapter.rowCount);
        gridView.setAdapter(adapter);

        gridView.setSelector(android.R.color.transparent);
        // gridView.setColumnWidth(GridviewAdapter.returnDisplayMetrics(this));

        gridView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {



            }
        });