无法检测到gridview上单击的按钮

时间:2015-01-01 08:38:22

标签: android android-layout gridview calculator

我正在开发一个简单的计算器Android应用程序,它将评估表达式并在文本框中显示结果。但我无法检测按钮clicks.I 想要定义我自己的适配器查看我的代码:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp"
     >

    <EditText 
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:hint="@string/expression"
        />

    <GridView 
        android:id="@+id/grid"
        android:numColumns="4"
        android:columnWidth="20dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingTop="100dp"
        ></GridView>

</LinearLayout>

gridcontent.xml:

<?xml version="1.0" encoding="utf-8"?>
<Button 
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:clickable="true"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

MainActivity.java:

package com.example.calculator;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        ArrayList<Character> arr = new ArrayList<Character>();
        String str = "789/456*123-.0=+";
        for(int i = 0;i<str.length();i++)
            arr.add(str.charAt(i));
        ArrayAdapter<Character> adapter = new ArrayAdapter<>(this, R.layout.gridcontent, arr);

        GridView grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(adapter);

        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, position + "done", Toast.LENGTH_SHORT).show();

            }
        });

    }   
}

注意:我是android的新手,玩的东西:) 感谢您的帮助

1 个答案:

答案 0 :(得分:0)

Create Adapter for grid like

Class Gridlistadapter extends ArrayAdapter<Character>
{

public Gridlistadapter (Context context, int resource,
            List<Character> objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        this.context = context;


    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.gridcontent, null);

        }
        Character data = getItem(position);
        Button button1= (Button) view.findViewById(R.id.button1);

        button1.setText(""+data);
        button1.setOnClickListener(new Listener(position));
        return view;
    }

    class listener implements OnClickListener {
        private int position;


        listener(int position) {
            this.position = position;

        }


        @SuppressLint("UseValueOf")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
              Toast.makeText(context, position + "done", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

package com.example.calculator;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        ArrayList<Character> arr = new ArrayList<Character>();
        String str = "789/456*123-.0=+";
        for(int i = 0;i<str.length();i++)
            arr.add(str.charAt(i));
        Gridlistadapter  adapter = new Gridlistadapter (this, R.layout.gridcontent, arr);

        GridView grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(adapter);

        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, position + "done", Toast.LENGTH_SHORT).show();

            }
        });

    }   
}