如何从GridView的适配器中获取每个EditText的值? [英] How to get the value of each EditText from an adapter from GridView?

查看:82
本文介绍了如何从GridView的适配器中获取每个EditText的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个矩阵计算器应用程序,我无法弄清楚如何获取GridView中每个EditText的值。我需要获取这些值,以便将它们放入另一个矩阵并计算它。



如果用户想要使用3x2矩阵,则GridView看起来像这样。这是一个包含六个EditText的GridView,因为3 * 2 = 6





以上是上图中的XML:

 < RelativeLayout 
android:layout_width =368dp
android:layout_height =495dp
工具:layout_editor_absoluteX =8dp
工具:layout_editor_absoluteY =8dp>

TextView
android:id =@ + id / textView10
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignParentLeft =true
android:layout_alignParentStart =true
android:layout_alignParentTop =true
android:layout_marginLeft =15dp
android:layout_marginStart = 15dp
android:text =请在两个矩阵中输入数字。 />

TextView
android:id =@ + id / textView11
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignLeft =@ + id / textView10
android:layout_alignStart =@ + id / textView10
android:layout_below =@ + id / textView10
android:layout_marginTop =21dp
android:text =Matrix1:
android:textStyle =bold/>

< GridView
android:id =@ + id / grid
android:layout_width =match_parent
android:layout_height =300dp
android:layout_alignLeft =@ + id / textView11
android:layout_alignParentEnd =true
android:layout_alignParentRight =true
android:layout_alignStart =@ + id / textView11
android:layout_below =@ + id / textView11/>

TextView
android:id =@ + id / textView8
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =请填写所有字段
android:textColor =#FF0000
android:visibility =invisible
android:layout_below =@ + id / grid
android:layout_alignLeft =@ + id / grid
android:layout_alignStart =@ + id / grid
android:layout_marginTop =15dp/>

以下是GridView项目的XML。我不知道如何从中获取值:

 < EditText 
android:id =@ + id / editText3
android:layout_width =50dp
android:layout_height =wrap_content
android:gravity =center
android:ems =10
android:inputType =numberDecimal/>

以下是GridView的MatrixAdapter:

  public class MatrixAdapter extends BaseAdapter {
Context context;
列表< Matrix> matrixList;

//或创建一个int [rows] [columns]更改为此后
public MatrixAdapter(上下文上下文,列表< Matrix> matrixList){
this.context =上下文;
this.matrixList = matrixList;

$ b @Override
public int getCount(){

return matrixList.size();
}

@Override
public Object getItem(int i){

return matrixList.get(i);
}

@Override
public long getItemId(int i){

return i;
}

@Override
public View getView(int i,View view,ViewGroup viewGroup){
View v = View.inflate(context,R.layout。 grid_item,NULL);
return v;
}
}

和Matrix类:

  public class Matrix {
public int i;
public int j;
public Matrix(int i,int j)
{
this.i = i;
this.j = j;




$ b $ p $最后,这里是我所在的Java代码从GridView中的每个EditText获得值,但我不知道该怎么做:

 私人MatrixAdapter适配器; 
私人列表< Matrix> matrixList;
private int rows;
private int列;

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

//获得矩阵的矩阵
Bundle extras = getIntent()。getExtras();
rows = extras.getInt(rows);
列= extras.getInt(列);

// MATRIX 1 INPUT

//初始化网格
GridView grid =(GridView)findViewById(R.id.grid);
grid.setNumColumns(columns);

//创建一个矩阵对象列表
//或者创建一个EditText [rows] [columns]将其更改为
// Matrix [] [] matrix1 = new矩阵[行] [列];
matrixList = new ArrayList<>(); //这仅仅适用于EDIT_TEXTS矩阵
//获取矩阵值将来自用户输入

//向每个项目添加一些内容
for(int i = 0; i {
for(int j = 0; j {
matrixList.add(new Matrix(i,j ));



//创建一个适配器(矩阵适配器)
adapter = new MatrixAdapter(getApplicationContext(),matrixList);

//连接到网格的适配器
grid.setAdapter(adapter);

}

public void nextMatrix(View view)//按钮进入下一个矩阵
{
//它收集用户输入的内容太
//检查一个字段是否为空
//但是如何从每个EditText获取值?

}

public void setEmptyToZero(View view)//将空文本框填充为零
{
//如何使程序添加零到文本字段?
//首先遍历textField矩阵。如果一个空字段是
//找到,则向其添加零

EditText textField;
布尔型fieldIsEmpty;
for(int i = 0; i {
for(int j = 0; j {
//如何从EditText矩阵获取值?




解决方案

我希望这段代码能帮助你,

  @Override 
public View getView(int position,View convertView,ViewGroup父类){
ViewHolder viewHolder;

if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.R.layout.grid_item,parent,false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder =(ViewHolder)convertView.getTag();
}

项目currentItem =(Item)getItem(position);
viewHolder.itemName.setText(currentItem.getItemName()); //如果已经在你的列表中:

return convertView;

}



私人类ViewHolder {
EditText itemName;

pre $ public ViewHolder(View view){
itemName =(EditText)view.findViewById R.id.editText3);
itemname.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String text = itemName..getText()。toString() ;

}
});
}

}


I'm working on a Matrix Calculator app and I'm having trouble figuring out how to get a value of each EditText that are in the GridView. I need to get the values in order to put them in another matrix and calculate it.

Here is what the GridView looks like if the user wanted to use a 3x2 matrix. It's a GridView containing six EditTexts because 3*2=6

Here is the XML for that picture above:

<RelativeLayout
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:text="Please input the numbers on both matrices." />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView10"
        android:layout_alignStart="@+id/textView10"
        android:layout_below="@+id/textView10"
        android:layout_marginTop="21dp"
        android:text="Matrix1: "
        android:textStyle="bold" />

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignLeft="@+id/textView11"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignStart="@+id/textView11"
        android:layout_below="@+id/textView11" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please fill in all of the fields"
        android:textColor="#FF0000"
        android:visibility="invisible"
        android:layout_below="@+id/grid"
        android:layout_alignLeft="@+id/grid"
        android:layout_alignStart="@+id/grid"
        android:layout_marginTop="15dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_alignStart="@+id/textView8"
        android:layout_below="@+id/textView8"
        android:layout_marginTop="11dp"
        android:onClick="setEmptyToZero"
        android:text="Set Empy Fields to Zero" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button4"
        android:layout_marginLeft="28dp"
        android:layout_marginStart="28dp"
        android:layout_toEndOf="@+id/button4"
        android:layout_toRightOf="@+id/button4"
        android:onClick="nextMatrix"
        android:text="Next Matrix" />

</RelativeLayout>

Here is the XML for the GridView items. I don't know how to get the values from them:

<EditText
    android:id="@+id/editText3"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:ems="10"
    android:inputType="numberDecimal" />

Here is the MatrixAdapter for the GridView:

public class MatrixAdapter extends BaseAdapter {
    Context context;
    List<Matrix> matrixList;

    //Or create an int[rows][columns] CHANGE INTO THAT LATER
    public MatrixAdapter(Context context, List<Matrix> matrixList) {
        this.context = context;
        this.matrixList = matrixList;
    }

    @Override
    public int getCount() {

        return matrixList.size();
    }

    @Override
    public Object getItem(int i) {

        return matrixList.get(i);
    }

    @Override
    public long getItemId(int i) {

        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=View.inflate(context,R.layout.grid_item,null);
        return v;
    }
} 

And the Matrix class:

public class Matrix {
    public int i;
    public int j;
    public Matrix(int i, int j)
    {
        this.i = i;
        this.j = j;
    }
}

And finally, here is the Java code where I am going to get the values from each EditText in GridView, but I don't know how to do that:

private MatrixAdapter adapter;
private List<Matrix> matrixList;
private int rows;
private int columns;

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

    // GET THE MATRIX DIMENSIONS FOR BOTH MATRICES
    Bundle extras = getIntent().getExtras();
    rows = extras.getInt("rows");
    columns = extras.getInt("columns");

    //MATRIX 1 INPUT

    // INITIALISE THE GRID
    GridView grid=(GridView)findViewById(R.id.grid);
    grid.setNumColumns(columns);

    // CREATE A LIST OF MATRIX OBJECTS
    //Or create an EditText[rows][columns] CHANGE INTO THAT LATER
    //Matrix[][] matrix1 = new Matrix[rows][columns];
    matrixList = new ArrayList<>(); //THIS IS ONLY FOR THE MATRIX OF EDIT_TEXTS
    //Getting the matrix values will come from the user input

    // ADD SOME CONTENTS TO EACH ITEM
    for (int i=0;i<rows;i++)
    {
        for (int j=0;j<columns;j++)
        {
            matrixList.add(new Matrix(i,j));
        }
    }

    // CREATE AN ADAPTER  (MATRIX ADAPTER)
    adapter = new MatrixAdapter(getApplicationContext(),matrixList);

    // ATTACH THE ADAPTER TO GRID
    grid.setAdapter(adapter);

}

public void nextMatrix(View view) //The button goes to the next matrix
{
    //It collects what the user inputted too
    //Checks if one of the fields are empty
    //But how to get the values from each EditText?

}

public void setEmptyToZero(View view) //Fills the empty text boxes to zero
{
    //how to make the program add zeros to the text fields?
    //iterate through the textField matrix first. If an empty field is
    //found, add zero to it

    EditText textField;
    boolean fieldIsEmpty;
    for (int i=0;i<rows;i++)
    {
        for (int j=0;j<columns;j++)
        {
            //How to get values from the EditText Matrix?
        }
    }
}

解决方案

I hope this code will help you,

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

if (convertView == null) {
    convertView = LayoutInflater.from(context).inflate(R.layout.R.layout.grid_item, parent, false);
    viewHolder = new ViewHolder(convertView);
    convertView.setTag(viewHolder);
} else {
    viewHolder = (ViewHolder) convertView.getTag();
}

Item currentItem = (Item) getItem(position);
viewHolder.itemName.setText(currentItem.getItemName());  // if already in your  list:

return convertView;

}

private class ViewHolder { EditText itemName;

public ViewHolder(View view) {
    itemName = (EditText)view.findViewById(R.id.editText3);
    itemname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           String text = itemName..getText().toString();

    }
  }); 
}

}

这篇关于如何从GridView的适配器中获取每个EditText的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆