Note that there are some explanatory texts on larger screens.

plurals
  1. POGet text from edittext inside a listview
    text
    copied!<p><strong>Question part: 1</strong></p> <p>I have created an activity which contains product id and product name as list items. Each row contains an edittext which can be used to enter quantity for a particular product. The rows also contain a checkbox to select the particular product.</p> <p>This is how the list looks like:</p> <p><img src="https://i.stack.imgur.com/4h9zF.png" alt="List"> </p> <p>When I click on the list items, I can get the id and name of the particular list item, but I also want to get the quantity entered by the user for the list item.</p> <p>This is the <strong>activity responsible for generating the listview</strong>:</p> <pre><code>public class PollStationActivity extends Activity { // Hashmap for ListView ArrayList&lt;HashMap&lt;String, String&gt;&gt; PSList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); String status_code_from_prev; List&lt;HashMap&lt;String, String&gt;&gt; fillMaps=null; String alert_message; String quantity_message; //quantity edittext EditText quantity_edit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_poll_station); //quantity edittext quantity_edit = (EditText)findViewById(R.id.qty_editText); //database insertion DatabaseHelper db = new DatabaseHelper(this); ContentValues values = new ContentValues(); try { db.createDataBase(); values.put("_id", "1"); values.put("name", "rose"); db.insert(values); } catch (IOException e) { e.printStackTrace(); } db.close(); ArrayList&lt;TestItem&gt; PSList = new ArrayList&lt;TestItem&gt;(); try { db.createDataBase(); PSList = db.getAllData(); } catch (IOException e) { e.printStackTrace(); } db.close(); fillMaps = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); Iterator&lt;TestItem&gt; i = PSList.iterator(); while(i.hasNext()) { HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); TestItem objPSItem = i.next(); map.put("name", objPSItem.NAME); map.put("Id", objPSItem.ID); //map.put("quantity", objPSItem.QUANTITY); fillMaps.add(map); } Log.i("Size: ", ""+fillMaps.size()); //populating listview from database ListView listView1 = (ListView) findViewById(R.id.poll_list_listView); if (null != listView1 &amp;&amp; null != PSList) { listView1.setAdapter(new ListAdapter(PollStationActivity.this, R.id.ListViewContainer, new String[fillMaps.size()])); } } //class for the list and on click handler class ListAdapter extends ArrayAdapter&lt;String&gt; { private final Context context; private final String[] values; public ListAdapter(Context context, int textViewResourceId, String[] objects) { super(context, textViewResourceId, objects); // TODO Auto-generated constructor stub this.context = context; this.values = objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub // return super.getView(position, convertView, parent); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View rowView = inflater.inflate(R.layout.list_layout, parent, false); final HashMap&lt;String, String&gt; map = fillMaps.get(position); TextView textView = (TextView) rowView .findViewById(R.id.list_label_name); textView.setText("("+map.get("Id")+") "+map.get("name")); rowView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { rowView.setBackgroundResource(R.drawable.list_bg_pressed); Handler handler = new Handler(); Runnable r = new Runnable() { public void run() { rowView.setBackgroundResource(R.drawable.list_bg); } }; handler.postDelayed(r, 200); //alert box AlertDialog.Builder alertDialog = new AlertDialog.Builder(PollStationActivity.this); // Setting Dialog Title alertDialog.setTitle("Please Note!"); // Setting Dialog Message alertDialog.setMessage("Are you sure you want to select "+"("+map.get("Id")+") "+map.get("name")+"?"); // Setting Positive "Yes" Button alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { // Write your code here to invoke YES event // Intent intent = new Intent(RegisterFirstActivity.this, RegisterSecondActivity.class); // // intent.putExtra("AC_Code", map.get(TAG_CODE)); // RegisterFirstActivity.this.startActivity(intent); } }); // Setting Negative "NO" Button alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Write your code here to invoke NO event dialog.cancel(); } }); // Showing Alert Message alertDialog.show(); } }); return rowView; } } public void makeAToast(String str) { //yet to implement Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } } </code></pre> <p>This is the <strong>TestItem</strong> class:</p> <pre><code> public class TestItem { public String ID; public String NAME; public String QUANTITY; public boolean selected; // Empty constructor public TestItem(){ } // constructor public TestItem(String id, String name, String quantity){ this.ID = id; this.NAME = name; this.QUANTITY = quantity; } // constructor public TestItem(String name, String quantity){ this.NAME = name; this.QUANTITY = quantity; } // getting ID public String getID(){ return this.ID; } // setting id public void setID(String id){ this.ID = id; } // getting name public String getName(){ return this.NAME; } // setting name public void setName(String name){ this.NAME = name; } // getting phone number public String getQuantity(){ return this.QUANTITY; } // setting quantity public void setQuantity(String quantity){ this.QUANTITY = quantity; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } } </code></pre> <p>This is the <strong>activity_poll_station.xml</strong>:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/main_bg"&gt; &lt;RelativeLayout android:id="@+id/ListViewContainer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/poll_label_textView" android:layout_centerHorizontal="true" android:layout_marginTop="28dp" &gt; &lt;ListView android:id="@+id/poll_list_listView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" &gt; &lt;/ListView&gt; &lt;/RelativeLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>This is the <strong>list_layout.xml</strong>:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/library_linear_layout" android:layout_width="match_parent" android:layout_height="70dp" android:layout_alignParentLeft="true" android:background="@drawable/list_bg" android:padding="5dp" &gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="44dp" android:background="@null" &gt; &lt;TextView android:id="@+id/list_label_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="name" android:textAppearance="?android:attr/textAppearanceSmall" android:textSize="17sp" /&gt; &lt;CheckBox android:id="@+id/item_checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true"/&gt; &lt;EditText android:id="@+id/qty_editText" android:layout_width="75dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:ems="10" android:maxLines="1" android:inputType="number" &gt; &lt;/EditText&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>I want to how to extract text from the edittext which I have created for every list item. If I try to extract the text on </p> <pre><code>rowView.setOnClickListener(new View.OnClickListener() </code></pre> <p>like this:</p> <pre><code>// Setting Dialog Message alertDialog.setMessage("Are you sure you want to select "+"("+map.get("Id")+") "+map.get("name")+"Quantity: "+quantity_edit.getText().toString()+"?"); </code></pre> <p>The I am getting a <code>null pointer exception</code> getting generated due to the <code>rowView.setOnClickListener(new View.OnClickListener()</code></p> <p>What should I do? What should be the work around?</p> <p>Thanks in advance!</p> <p>//------------------------------------------------------------------------------//</p> <p><strong>Question part: 2</strong></p> <p>Now I want to do something like, I want to remove a particular row on clicking it. The row will only be deleted from the existing listview and a new list will be shown, how to do that?</p> <p>Thanks once again!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload