如何在onItemSelected上创建GridView项目(textview)的.setText()?

时间:2015-04-09 12:24:20

标签: java android onclicklistener android-gridview settext

我是android的初学者。在这里,我想在点击时更改GridView中项目的文本,我想知道它是如何完成的....

这是我的代码:

 public class MainActivity extends Activity implements OnItemClickListener  {
    TextView tv;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gv = (GridView) findViewById(R.id.gridView1);
        b = (Button) findViewById(R.id.button1);

        gv.setAdapter(new custom(this));
        gv.setOnItemClickListener(this);


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub


    }

}

自定义类:

public class custom extends BaseAdapter  {
private LayoutInflater li;


Context ctx;
Button b2;

int v = 0;
String items = "Krishna";

public custom(Context c) {
    ctx = c;
    li=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

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

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

    return items.charAt(position);
}

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

    return position;
}

@Override
public View getView(int position, View convertview, ViewGroup parent) {
    View b1=convertview;
    if(convertview==null){

        b1=li.inflate(R.layout.c_layout, null);
    }
    b2 =(Button) b1.findViewById(R.id.button2); 
    b2.setText(String.valueOf(items.charAt(position)));
    b2.setHeight((int) 60);
    b2.setGravity(Gravity.CENTER);
    b2.setClickable(true);
    if ((position % 2) == 0) {

        katre();
    } else {
        katre2();
    }


    return b1;

}

public void katre() {
    b2.setBackgroundResource(extracted());
}

private int extracted() {
    return R.color.button_material_dark;
}

public void katre2() {
    b2.setBackgroundResource(R.color.material_blue_grey_900);
}

}

和activity_main.xml:

         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" 
            android:layout_gravity="center_horizontal"/>
        <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="7"
        android:stretchMode="columnWidth"
         >

    </GridView>

    </LinearLayout>

最后是c_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<Button

    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</RelativeLayout>

请帮帮我......

1 个答案:

答案 0 :(得分:1)

  

想要在点击时更改gridview中项目的文本

使用view的{​​{1}}参数从点击的项目

访问按钮
onItemClick

修改

或者您可以通过在@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Button clickedItem =(Button) view.findViewById(R.id.button2); clickedItem.setText("Clicked"); } 方法中将OnClickListener添加到按钮来执行此操作:

getView