-2

I'm beginning in Android Development and i need close a setContentView() called with a onClickListener() where myCurrent screen is a layout inflated over other Layout.

Here is my code:

public class MainActivity extends Activity {

private SQLiteDatabase myDb;
private String dbPath = "/data/data/com.example.dbapp/databases/testData";

private boolean dbExist(String dbPath){
    File fakeDb = new File(dbPath);
    return fakeDb.exists();
}

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

    if(!dbExist(dbPath)){
    myDb = openOrCreateDatabase(dbPath, MODE_PRIVATE, null);
    myDb.execSQL("CREATE TABLE files(cn_userName INTEGER PRIMARY KEY AUTOINCREMENT);");
    myDb.close();
    }else{
        fillDb();
    }
}

private void fillDb(){

    LinearLayout myScroll = (LinearLayout) findViewById(R.id.idOfmyScroll);
    if(myScroll.getChildCount() > 0){
       myScroll.removeAllViews();
    }

    myDb = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY);
    Cursor myDbCur = myDb.rawQuery("SELECT * FROM files", null);

    for(int i = 0; i < myDbCur.getCount(); i++){
        View name = getLayoutInflater().inflate(R.layout.name_list, null);
        TextView userName = (TextView)name.findViewById(R.id.idName);

        userName.setText(myDbCur.getString(myDbCur.getColumnIndex("cn_userName"));
        myScroll.addView(name);
        myDbCur.moveToNext();
        myDb.close();

        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            setContentView(R.layout.otherUserData);
            userLastName = (TextView)findViewById(R.id.txtLastName);
            nombreContacto.setText(userName + "Her LastName");
            }
        });
    }
}

When i press the back key jump to activity_main and I want the inflated layout

jfidel87
  • 13
  • 3
  • 3
    You need to be much more detailed in your question. Show some example code. What have you tried and what about it isn't working? – Eric Levine Apr 25 '14 at 19:55
  • 1
    What does "inflated over other Layout" mean? – CommonsWare Apr 25 '14 at 19:57
  • You want to close the inflated view and display the one that is in the background? – zgc7009 Apr 25 '14 at 19:59
  • @elevine Below is the code. Thanks you! – jfidel87 Apr 25 '14 at 20:32
  • 1
    Your code doesn't show in the comment. In any case, please post your code in your question, not as a comment. You should be able to edit your question with your code. Just click on the 'edit' link. Furthermore, know that every time you edit a question significantly enough, it pops it back up in the stackoverflow queue, thereby giving it more visibility to everyone. – Stephan Branczyk Apr 25 '14 at 20:32

1 Answers1

1

It sounds like you want to switch fragments? Another option would be to just have a framelayout and drop your fragments in it when you needed to.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />


</LinearLayout>

then you just Fill that container with a fragment in the activity.

FragmentManager fm = getSupportFragmentManager();
Fragment myFragment = fm.findFragmentByTag(MyFragment.TAG);
if (myFragment == null) {
        myFragment = new MyFragment();
}
fm.beginTransaction().add(R.id.container, myFragment, MyFragment.TAG).commit();

Then in your onclick you could use the fragmenttransaction function replace instead of add.

bestdayever
  • 154
  • 6