How to get data from dynamically created EditText views and insert it into an array?

Posted by Snwspeckle on Stack Overflow See other posts from Stack Overflow or by Snwspeckle
Published on 2012-09-20T02:48:29Z Indexed on 2012/09/20 3:37 UTC
Read the original article Hit count: 136

So basically what I need my program to do at this point is that when I click the submit button, I need to loop through each dynamic row of the ListView and grab the value in the EditText view and then insert that into an array which I will do further calculations after. Here is my code right now.

package com.hello_world;

import java.util.ArrayList;

import com.hello_world.ByteInputActivity.MyAdapter.ViewHolder;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ByteInputActivity extends Activity {

private ListView myList;
private MyAdapter myAdapter;
private Integer resQuestions;
private Integer indexVal = 0;
private View caption;
ViewHolder holder;
ArrayList<Integer> intArrayList = new ArrayList<Integer>();

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

    //Gets number of questions from MainActivity
    Bundle extras = getIntent().getExtras();
    if(extras !=null) {
        resQuestions = extras.getInt("index");
    }

    myList = (ListView) findViewById(R.id.FieldList);
    myList.setItemsCanFocus(true);
    myAdapter = new MyAdapter();
    myList.setAdapter(myAdapter);

    Button submit = (Button) findViewById(R.id.btn_New);
    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            for (int i = 0; i < myList.getCount() ; i++) {
                View vListSortOrder;
                vListSortOrder = myList.getChildAt(i);     
                String temp = holder.caption.getText().toString();
                    Log.e("VALUES", "" +temp);
            }
        }
    });
}

public class MyAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    public ArrayList myItems = new ArrayList();

    public MyAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0; i < resQuestions; i++) {
            ListItem listItem = new ListItem();
            listItem.caption = "Index " + i;
            listItem.indexText = "Index " + i;
            myItems.add(listItem);
            indexVal += 1;
        }       
        notifyDataSetChanged();
    }

    public int getCount() {
        return myItems.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item, null);
            holder.indexText = (TextView) convertView .findViewById(R.id.textView1);
            holder.caption = (EditText) convertView .findViewById(R.id.ItemCaption);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //Fill EditText with the value you have in data source
        holder.caption.setText("");
        holder.caption.setId(position);

        holder.indexText.setText("Index " + position);
        holder.indexText.setId(position);

        //we need to update adapter once we finish with editing
        holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus){
                    final int position = v.getId();
                    final EditText Caption = (EditText) v;
                    myItems.set(position, Caption.getText().toString());                    
                }
            }
        });

        return convertView;
    }

class ViewHolder {
    EditText caption;
    TextView indexText;
}

class ListItem {
    String caption;
    String indexText;
}
}
}

© Stack Overflow or respective owner

Related posts about android

Related posts about loops