-1

I have an EditText inside ListView and I want to enter some integer values in all the EditTexts. I have one Button outside the ListView onClicking. From this button I want to get the data from all the EditText from ListView and save in array to show in toast. Also my EditText id is same for all the EditText. Can any buddy give so sample code so I can proceed to next step. Thanks in advance.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listGejalaPilih"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp" >
    </ListView>

    <Button
        android:id="@+id/btnDiagnosis"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Diagnosis Penyakit" >

    </Button>

</LinearLayout> 

Item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textGejalaPilih"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_alignParentLeft="true" />

    <EditText
        android:id="@+id/editPersenYakin"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxLength="3"
        android:layout_alignParentRight="true" >
    </EditText>

</RelativeLayout>

Main.java

package id.app.pakarsawit;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class DetailDiagnosis extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] pilihan = {"pilihan 1","pilihan 2","pilihan 3", "pilihan 4"};

        ListView listView = (ListView)findViewById(R.id.listGejalaPilih);
        Button btn = (Button)findViewById(R.id.btnDiagnosis);

        ArrayAdapter<String> adapterPilih = new LVAdapterDetailDiagnosis(this, pilihan);
        listView.setAdapter(adapterPilih);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

            }
        });

    }
}

LVAdapterDetailDiagnosis.java

package id.app.pakarsawit;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LVAdapterDetailDiagnosis extends ArrayAdapter<String> {

    private final Context context;
    private final String[] values;

    public LVAdapterDetailDiagnosis(Context context, String[] values) {
        super(context, R.layout.item);
        this.context = context;
        this.values = values;
    }

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

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.item, parent, false);

        TextView textView = (TextView)rowView.findViewById(R.id.textGejalaPilih);

        textView.setText(values[position]);

        return rowView;
    }

}
Alca
  • 81
  • 2
  • 13
  • You must use an array list to keep track of index and value, like the way it is done for checkboxes in a list. Use a textwatcher to see if your EditText is populated, if the count is more than zero, consider it is populated. Then add the index and the value into the array list. You must ensure the ArrayList is init in the constructor of your custom adapter using a loop, which loops till the length of your array, with false or dummy values. – Skynet Jul 15 '15 at 08:51
  • Hi I got a solution w8 for me haha – Sheychan Jul 15 '15 at 08:52

1 Answers1

1

try this:

        for (int a = 0; a < listGejalaPilih.getCount(); a++) {
            EditText et = (EditText) listGejalaPilih.getChildAt(a).findViewById(R.id.editPersenYakin);
            arraylist.add(et.getText().toString());
        }

Or if you want Array

        String string[] = new String[listGejalaPilih.getCount()];
        for (int a = 0; a < listGejalaPilih.getCount(); a++) {
            EditText et = (EditText) listGejalaPilih.getChildAt(a).findViewById(R.id.editPersenYakin);
            string[a] = et.getText().toString();
        }
Sheychan
  • 2,415
  • 14
  • 32