0

I want to display several edit texts in a grid view. `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;
}
}`

The adapter which I made works but what I don't know is how to get data from several edit texts and store into an array.
Regards Devan
Update
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 Answers1

0

A similar question was asked here. One answer suits your needs well.

What you would do is the following: You name your edit fields following a certain pattern like edittext[number] where [number] is between 0 and the number of edit texts minus 1. The answer suggests how to loop through all these fields. Using the getText function of the edit texts you can get the entered text and add it to an array / list.

Here's some code how you can achieve that:

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
}
Patrick
  • 184
  • 1
  • 1
  • 12
  • @patrik what values should I use for' some_value' and 'some_other_value' variables in that loop. I didn't understand it well.Coud you post me a sample code. – ElectroDEV Jul 02 '17 at 13:48
  • I updated my answer and added some code, hope it helps. – Patrick Jul 02 '17 at 14:16
  • @patrik You told to name the edittexts. How should I do that. Should I post other files too. – ElectroDEV Jul 02 '17 at 14:52
  • At some point you must create the edit texts. Either statically in an XML layout or dynamically using (Java) code. Do you have that? – Patrick Jul 02 '17 at 14:58
  • I have edited my post. Hope it helps you to answer my questions. If have any queries regarding my files please ask. – ElectroDEV Jul 02 '17 at 15:15
  • From your newly-added code I cannot see whether all EditTexts are generated or not. Does it work that you see all EditTexts you want to provide to the user? – Patrick Jul 02 '17 at 18:02
  • Yes patriknow the grid view shows all the edit texts that I want to show to the iser – ElectroDEV Jul 03 '17 at 01:51