1

I m trying this tutorial : http://android-er.blogspot.in/2014/01/get-text-from-dynamically-added-view.html \ where i m trying to edit the text when user click the insert button, it works fine only if i put position as static value. but when i try to get position this app unfortunat stop.

public class MainActivity extends Activity {
 EditText textIn;
 Button buttonAdd;
 LinearLayout container;
 Button buttonShowAll;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textIn = (EditText)findViewById(R.id.textin);
  buttonAdd = (Button)findViewById(R.id.add);
  container = (LinearLayout)findViewById(R.id.container);

  buttonAdd.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    LayoutInflater layoutInflater = 
      (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View addView = layoutInflater.inflate(R.layout.row, null);
    final TextView textOut = (TextView)addView.findViewById(R.id.textout);
    textOut.setText(textIn.getText().toString());
    Button buttonRemove = (Button)addView.findViewById(R.id.remove);
    buttonRemove.setOnClickListener(new OnClickListener(){

     @Override
     public void onClick(View v) {
      ((LinearLayout)addView.getParent()).removeView(addView);
     }});

    Button buttonInsert = (Button)addView.findViewById(R.id.insert);
    buttonInsert.setOnClickListener(new OnClickListener(){

     @Override
     public void onClick(View v) {
                        //int position = (Integer)v.getTag();   // I m trying to get position of button here but stops the app
                        int position = 2; // Static value for changing the textview works fine
                        String showallPrompt = "";
                        View childView = container.getChildAt(position);
                        TextView childTextView = (TextView) (childView.findViewById(R.id.textout));
                        String childTextViewText = (String) (childTextView.getText());
                        showallPrompt += position + ": " + childTextViewText + "\n";
                        Toast.makeText(MainActivity.this,
                                showallPrompt,
                                Toast.LENGTH_LONG).show();
                        ((TextView) (childTextView.findViewById(R.id.textout))).setText("HELLO ");
     }});

    container.addView(addView, 0);
   }});

  LayoutTransition transition = new LayoutTransition();
  container.setLayoutTransition(transition);
  }
}

So here, i used to get position by

//int position = (Integer)v.getTag();   // I m trying to get position of button here but stops the app
    int position = 2; // Static value for changing the textview works fine

but not working . How to solve this problem.Actually i m tring to do listview with editable text and show as textview.

User Learning
  • 3,165
  • 5
  • 30
  • 51
  • Can you add the log? – WannaBeGeek Apr 29 '16 at 09:22
  • java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference i m getting this @WannaBeGeek – User Learning Apr 29 '16 at 09:24
  • please check if you are setting the tag correctly , for more info about the exception , can look at the similar ques :http://stackoverflow.com/questions/21314110/getting-a-nullpointer-at-view-gettag – WannaBeGeek Apr 29 '16 at 09:41
  • int position = (Integer)v.getTag(); this is wrong type casting it should be like this Integer position = (Integer)v.getTag(); there is a big defference in int and Integer – Adeel Turk Apr 29 '16 at 10:23

0 Answers0