XML : Edit text from dynamically created textview in layout

im currently creating a set of layouts into a linearLayout everytime a dragable textView is dropped in a certaine zone. When dropped, this calls an action:

   private class ChoiceDragListener implements OnDragListener {      @Override      public boolean onDrag(View v, DragEvent event) {          //handle drag events          switch (event.getAction()) {              case DragEvent.ACTION_DRAG_STARTED:                  //no action necessary                  break;              case DragEvent.ACTION_DRAG_ENTERED:                  //no action necessary                  break;              case DragEvent.ACTION_DRAG_EXITED:                  //no action necessary                  break;              case DragEvent.ACTION_DROP:                  View view = (View) event.getLocalState();                  TextView dropped = (TextView) view;                  String getText = (String) dropped.getText();                    builderSelector(getText);                    break;              case DragEvent.ACTION_DRAG_ENDED:                  //no action necessary                  break;              default:                  break;          }          return true;      }  }    

Then, builderSelector creates the text to be displayed and calls addToScript, wich emulates the click of a button:

  void addToScript() {      textIn.setText(instruction);      buttonAdd.performClick();      textIn.setText("");      instruction = "";  }    

The buttonAdd button then creates the layout and adds it to the linearlayout:

  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);                  }              });                final Button buttonUp = (Button)addView.findViewById(R.id.up);              buttonUp.setOnClickListener(new OnClickListener(){                    @Override                  public void onClick(View v) {                        String test = (String) textOut.getText();                      for (int i=0;i<textContents.size();i++) {                          if (test==textContents.get(i)) {                              Toast.makeText(MainActivity.this, "Bravo", Toast.LENGTH_LONG).show();                                textOut.setText(textContents.get(i-1));                          }                      }                    }});                Button buttonDown = (Button)addView.findViewById(R.id.down);              buttonDown.setOnClickListener(new OnClickListener() {                    @Override                  public void onClick(View v) {                        String test = (String) textOut.getText();                      for (int i=0;i<textContents.size();i++) {                          if (test==textContents.get(i)) {                              Toast.makeText(MainActivity.this, "Bravo", Toast.LENGTH_LONG).show();                                textOut.setText(textContents.get(i+1));                          }                      }                      //String text = textOut.getText().toString();                      //String newText = textIn.getText().toString() + text;                      //textIn.setText(newText);                    }});                textContents.add((String) textOut.getText());              container.addView(addView);          }});    

My question is, how can i swap the text between the created textViews by clicking the up and down buttons created dynamically. My problem is that i have no clue on how to set the text on the next textView with the text of the actual textView. Here are my layouts:

activity_main.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"  tools:context=".MainActivity"  android:padding="5dp"  android:background="#ffffffff">    <FrameLayout      android:layout_width="match_parent"      android:layout_height="match_parent"      android:layout_toRightOf="@+id/frameLayout"      android:layout_toEndOf="@+id/frameLayout"      android:layout_marginTop="100dp"      android:layout_alignParentBottom="true"></FrameLayout>    <LinearLayout      android:orientation="vertical"      android:layout_width="600dp"      android:layout_height="match_parent"      android:layout_alignParentTop="true"      android:layout_alignParentRight="true"      android:layout_alignParentEnd="true"      android:layout_toRightOf="@+id/frameLayout"      android:layout_toEndOf="@+id/frameLayout"      android:id="@+id/linearLayout">        <RelativeLayout          android:layout_width="match_parent"          android:layout_height="wrap_content">          <Button              android:id="@+id/add"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:layout_alignParentRight="true"              android:text="Add"/>          <EditText              android:id="@+id/textin"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:layout_alignParentLeft="true"              android:layout_toLeftOf="@id/add"/>      </RelativeLayout>        <Button          android:id="@+id/showall"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:text="Show All" />        <ScrollView          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:id="@+id/scrollView"          android:padding="15dp">            <LinearLayout              android:id="@+id/container"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:orientation="vertical"              android:padding="50dp"              android:weightSum="1">          </LinearLayout>      </ScrollView>  </LinearLayout>    <FrameLayout      android:layout_width="5dp"      android:layout_height="match_parent"      android:layout_alignParentTop="true"      android:layout_centerHorizontal="true"      android:padding="5dp"      android:id="@+id/frameLayout"      android:background="#ffb4b4b4"></FrameLayout>    <TextView      android:layout_width="378dp"      android:layout_height="270dp"      android:text="DRAG THE ACTION HERE:"      android:id="@+id/mDragTo"      android:background="@mipmap/frame"      android:layout_alignParentBottom="true"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true"      android:layout_toLeftOf="@+id/frameLayout"      android:layout_toStartOf="@+id/frameLayout"      android:textAlignment="center"      android:gravity="center"      android:textSize="30dp"      android:textIsSelectable="false" />    <RelativeLayout      android:orientation="horizontal"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:layout_above="@+id/mDragTo"      android:layout_toLeftOf="@+id/frameLayout"      android:layout_toStartOf="@+id/frameLayout"      android:background="#ffffffff"      android:padding="15dp">        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Telltales"          android:id="@+id/mTelltales"          android:gravity="center|bottom"          android:layout_alignParentLeft="true"          android:layout_marginLeft="30dp"          android:layout_alignParentTop="true"          android:layout_marginTop="30dp"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Wait"          android:id="@+id/mWait"          android:gravity="center|bottom"          android:layout_alignTop="@+id/mTelltales"          android:layout_toRightOf="@+id/mTelltales"          android:layout_toEndOf="@+id/mTelltales"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Fuel"          android:id="@+id/mFuel"          android:gravity="center|bottom"          android:layout_alignTop="@+id/mWait"          android:layout_toRightOf="@+id/mWait"          android:layout_toEndOf="@+id/mWait"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Gauge"          android:id="@+id/mGauge"          android:gravity="center|bottom"          android:layout_alignTop="@+id/mFuel"          android:layout_toRightOf="@+id/mFuel"          android:layout_toEndOf="@+id/mFuel"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Display"          android:id="@+id/mDisplay"          android:gravity="center|bottom"          android:layout_alignTop="@+id/mGauge"          android:layout_toRightOf="@+id/mGauge"          android:layout_toEndOf="@+id/mGauge"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Loop"          android:id="@+id/mLoop"          android:gravity="center|bottom"          android:layout_below="@+id/mTelltales"          android:layout_alignLeft="@+id/mTelltales"          android:layout_alignStart="@+id/mTelltales"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Jump"          android:id="@+id/mJump"          android:gravity="center|bottom"          android:layout_alignTop="@+id/mLoop"          android:layout_toRightOf="@+id/mLoop"          android:layout_toEndOf="@+id/mLoop"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Chime"          android:id="@+id/mChime"          android:gravity="center|bottom"          android:layout_below="@+id/mFuel"          android:layout_alignLeft="@+id/mFuel"          android:layout_alignStart="@+id/mFuel"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Confirm Action"          android:id="@+id/mConfirmAction"          android:gravity="center|bottom"          android:layout_alignTop="@+id/mChime"          android:layout_toRightOf="@+id/mChime"          android:layout_toEndOf="@+id/mChime"          android:background="@drawable/drag_objects" />        <TextView          android:layout_width="100dp"          android:layout_height="100dp"          android:text="Comments"          android:id="@+id/mComments"          android:gravity="center|bottom"          android:background="@drawable/drag_objects"          android:layout_alignTop="@+id/mConfirmAction"          android:layout_toRightOf="@+id/mConfirmAction"          android:layout_toEndOf="@+id/mConfirmAction"          android:singleLine="false" />  </RelativeLayout>    </RelativeLayout>    

row.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="wrap_content">  <Button      android:id="@+id/remove"      android:layout_width="50dp"      android:layout_height="wrap_content"      android:layout_alignParentRight="true"      android:background="@drawable/ic_action_discard" />    <Button      android:layout_width="50dp"      android:layout_height="wrap_content"      android:id="@+id/up"      android:layout_toLeftOf="@+id/down"      android:background="@drawable/ic_fa_chevron_up" />    <Button      android:id="@+id/down"      android:layout_width="50dp"      android:layout_height="wrap_content"      android:layout_toLeftOf="@id/remove"      android:background="@drawable/ic_fa_chevron_down" />    <TextView      android:id="@+id/textout"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:layout_alignParentLeft="true"      android:layout_toLeftOf="@id/remove"/>    </RelativeLayout>    

Currently i can set the text of the actual textView with the text of the next upward or downward, but remains the issue of setting the text of the adjacent textView. Any ideas?

No comments:

Post a Comment