GridView中的EditText获取数据并进入数组

时间:2017-07-01 14:53:58

标签: android android-edittext android-gridview

我想在网格视图中显示多个编辑文本。  `package com.devan.test1;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;


public class CustomGrid extends BaseAdapter {
private final String[] web;
View grid;
private Context mContext;

public CustomGrid(Context c, String[] web) {
    mContext = c;

    this.web = web;
}



@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

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_single, null);
        EditText textView = (EditText) grid.findViewById(R.id.grid_text);


    } else {
        grid = convertView;
    }

    return grid;
}
}`

我制作的适配器有效,但我不知道如何从多个编辑文本中获取数据并存储到数组中。
关心Devan
更新
MainActivtiy.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends Activity {
GridView grid;
String[] web = {
        "Google",
        "Github",
        "Instagram",
        "Facebook",
        "Flickr",
        "Pinterest",
        "Quora",
        "Twitter",
        "Vimeo",
        "WordPress",
        "Youtube",
        "Stumbleupon",
        "SoundCloud",
        "Reddit",
        "Blogger"

};


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

    final CustomGrid adapter = new CustomGrid(MainActivity.this, web);
    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();

        }
    });
    Button b1=(Button) findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public  void onClick(View v)
        {
            Intent i =new Intent(getApplicationContext(),Main2Activity.class);
            i.putExtra("l",adapter.getBundle());
            startActivity(i);
        }
    });

}

}

activity_main.xml中

<RelativeLayout 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"
tools:context=".MainActivity">

<GridView
    android:id="@+id/grid"
    android:layout_width="fill_parent"
    android:layout_height="450dp"
    android:columnWidth="100dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/grid"
    android:id="@+id/button1"
    />

<RelativeLayout 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"
tools:context=".MainActivity">

<GridView
    android:id="@+id/grid"
    android:layout_width="fill_parent"
    android:layout_height="450dp"
    android:columnWidth="100dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/grid"
    android:id="@+id/button1"
    />

</RelativeLayout>

Main2Activity.java

import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.widget.TextView;

 public class Main2Activity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    TextView t=(TextView) findViewById(R.id.tc1);
    t.setText(getIntent().getStringExtra("l"));
}
}

activity_main2.xml

<android.support.constraint.ConstraintLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.devan.test1.Main2Activity">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tc1"
    />

</android.support.constraint.ConstraintLayout>

grid_single.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp">


<EditText
    android:id="@+id/grid_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:textSize="15sp" />


   </LinearLayout>

1 个答案:

答案 0 :(得分:0)

提出了类似的问题here。一个answer很适合您的需求。

您将执行以下操作:您可以按照某种模式命名您的编辑字段,例如 edittext [number] ,其中 [number] 介于0和0之间编辑文本减1.答案建议如何遍历所有这些字段。使用编辑文本的 getText 功能,您可以获取输入的文本并将其添加到数组/列表中。

这里有一些代码如何实现:

String idPrefix = "edittext_";
final int NUMBER_OF_EDIT_TEXTS = 5; // You might want to retrieve this value from some array / list size
for (int i = 0; i < NUMBER_OF_EDIT_TEXTS; i++) {
  int resID = mContext.getResources().getIdentifier(idPrefix + i, "id", getPackageName());
  EditText edt = (EditText) grid.findViewById(resId);
  String text = edit.getText();
  // Do something with text
}