How to implement a dynamic list in a fragment?












0














I have a fragment where I would like to implement a dynamic list such that there is a button that you can press to add fields in the list.



I found some code to do just this (also shown below)



Dynamically add elements to a listView Android



I am trying to implement this inside my fragment but have been unsuccessful. A solution would be highly appreciated.



THE CODE I FOUND TO MAKE A DYNAMIC LIST



import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.os.Bundle;
import android.view.View;

public class OrdersDynamicList extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();

//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;

//RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED
int clickCounter=0;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_server);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}

//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}


MY FRAGMENT



import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private OrdersDynamicList ordersDynamicList;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}


FRAGMENT XML



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addBtn"
android:text="Add New Order"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>









share|improve this question


















  • 1




    Technically you have 2 ListViews. One in the activity [ListActivity activity_server] and the other one in the fragment. You are setting, updating and notifying the adapter which attached to Activity. Have simple activity AppCompatActivity and move the addItem to Fragment class.
    – Suryavel TR
    Nov 20 at 20:46
















0














I have a fragment where I would like to implement a dynamic list such that there is a button that you can press to add fields in the list.



I found some code to do just this (also shown below)



Dynamically add elements to a listView Android



I am trying to implement this inside my fragment but have been unsuccessful. A solution would be highly appreciated.



THE CODE I FOUND TO MAKE A DYNAMIC LIST



import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.os.Bundle;
import android.view.View;

public class OrdersDynamicList extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();

//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;

//RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED
int clickCounter=0;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_server);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}

//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}


MY FRAGMENT



import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private OrdersDynamicList ordersDynamicList;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}


FRAGMENT XML



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addBtn"
android:text="Add New Order"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>









share|improve this question


















  • 1




    Technically you have 2 ListViews. One in the activity [ListActivity activity_server] and the other one in the fragment. You are setting, updating and notifying the adapter which attached to Activity. Have simple activity AppCompatActivity and move the addItem to Fragment class.
    – Suryavel TR
    Nov 20 at 20:46














0












0








0







I have a fragment where I would like to implement a dynamic list such that there is a button that you can press to add fields in the list.



I found some code to do just this (also shown below)



Dynamically add elements to a listView Android



I am trying to implement this inside my fragment but have been unsuccessful. A solution would be highly appreciated.



THE CODE I FOUND TO MAKE A DYNAMIC LIST



import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.os.Bundle;
import android.view.View;

public class OrdersDynamicList extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();

//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;

//RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED
int clickCounter=0;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_server);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}

//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}


MY FRAGMENT



import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private OrdersDynamicList ordersDynamicList;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}


FRAGMENT XML



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addBtn"
android:text="Add New Order"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>









share|improve this question













I have a fragment where I would like to implement a dynamic list such that there is a button that you can press to add fields in the list.



I found some code to do just this (also shown below)



Dynamically add elements to a listView Android



I am trying to implement this inside my fragment but have been unsuccessful. A solution would be highly appreciated.



THE CODE I FOUND TO MAKE A DYNAMIC LIST



import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.os.Bundle;
import android.view.View;

public class OrdersDynamicList extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();

//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;

//RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED
int clickCounter=0;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_server);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}

//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}


MY FRAGMENT



import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private OrdersDynamicList ordersDynamicList;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}


FRAGMENT XML



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addBtn"
android:text="Add New Order"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>






java android android-fragments android-listview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 20:04









Oamar Kanji

338311




338311








  • 1




    Technically you have 2 ListViews. One in the activity [ListActivity activity_server] and the other one in the fragment. You are setting, updating and notifying the adapter which attached to Activity. Have simple activity AppCompatActivity and move the addItem to Fragment class.
    – Suryavel TR
    Nov 20 at 20:46














  • 1




    Technically you have 2 ListViews. One in the activity [ListActivity activity_server] and the other one in the fragment. You are setting, updating and notifying the adapter which attached to Activity. Have simple activity AppCompatActivity and move the addItem to Fragment class.
    – Suryavel TR
    Nov 20 at 20:46








1




1




Technically you have 2 ListViews. One in the activity [ListActivity activity_server] and the other one in the fragment. You are setting, updating and notifying the adapter which attached to Activity. Have simple activity AppCompatActivity and move the addItem to Fragment class.
– Suryavel TR
Nov 20 at 20:46




Technically you have 2 ListViews. One in the activity [ListActivity activity_server] and the other one in the fragment. You are setting, updating and notifying the adapter which attached to Activity. Have simple activity AppCompatActivity and move the addItem to Fragment class.
– Suryavel TR
Nov 20 at 20:46












1 Answer
1






active

oldest

votes


















0














import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private Button addToList;
private ArrayList<String> listItems;
private ArrayAdapter<String> listViewAdapter;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

this.listItems = new ArrayList<String>();

this.listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
this.listItems
);


ListView listView = (ListView) v.findViewById(R.id.list);
listView.setAdapter(listViewAdapter);

this.addToList = v.findViewById(R.id.addBtn);
this.addToList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listItems.add("hi");
listViewAdapter.notifyDataSetChanged();
}
});

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}





share|improve this answer

















  • 1




    Thanks for sharing your solution. Could you add a sentence or two explaining what you did to solve your problem?
    – Hulk
    Nov 26 at 9:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400724%2fhow-to-implement-a-dynamic-list-in-a-fragment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private Button addToList;
private ArrayList<String> listItems;
private ArrayAdapter<String> listViewAdapter;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

this.listItems = new ArrayList<String>();

this.listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
this.listItems
);


ListView listView = (ListView) v.findViewById(R.id.list);
listView.setAdapter(listViewAdapter);

this.addToList = v.findViewById(R.id.addBtn);
this.addToList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listItems.add("hi");
listViewAdapter.notifyDataSetChanged();
}
});

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}





share|improve this answer

















  • 1




    Thanks for sharing your solution. Could you add a sentence or two explaining what you did to solve your problem?
    – Hulk
    Nov 26 at 9:04
















0














import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private Button addToList;
private ArrayList<String> listItems;
private ArrayAdapter<String> listViewAdapter;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

this.listItems = new ArrayList<String>();

this.listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
this.listItems
);


ListView listView = (ListView) v.findViewById(R.id.list);
listView.setAdapter(listViewAdapter);

this.addToList = v.findViewById(R.id.addBtn);
this.addToList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listItems.add("hi");
listViewAdapter.notifyDataSetChanged();
}
});

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}





share|improve this answer

















  • 1




    Thanks for sharing your solution. Could you add a sentence or two explaining what you did to solve your problem?
    – Hulk
    Nov 26 at 9:04














0












0








0






import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private Button addToList;
private ArrayList<String> listItems;
private ArrayAdapter<String> listViewAdapter;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

this.listItems = new ArrayList<String>();

this.listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
this.listItems
);


ListView listView = (ListView) v.findViewById(R.id.list);
listView.setAdapter(listViewAdapter);

this.addToList = v.findViewById(R.id.addBtn);
this.addToList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listItems.add("hi");
listViewAdapter.notifyDataSetChanged();
}
});

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}





share|improve this answer












import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class FragmentNewTable extends Fragment {
private FragmentNewTableListener listener;
private Button addToList;
private ArrayList<String> listItems;
private ArrayAdapter<String> listViewAdapter;

public interface FragmentNewTableListener {
void onInputFragmentNewTableSent(CharSequence input);
}


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_new_table, container, false);

this.listItems = new ArrayList<String>();

this.listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
this.listItems
);


ListView listView = (ListView) v.findViewById(R.id.list);
listView.setAdapter(listViewAdapter);

this.addToList = v.findViewById(R.id.addBtn);
this.addToList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listItems.add("hi");
listViewAdapter.notifyDataSetChanged();
}
});

return v;
}

public void updateEditText(CharSequence newText) {
//editText.setText(newText);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentNewTableListener) {
listener = (FragmentNewTableListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}

@Override
public void onDetach() {
super.onDetach();
listener = null;
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 22:20









Oamar Kanji

338311




338311








  • 1




    Thanks for sharing your solution. Could you add a sentence or two explaining what you did to solve your problem?
    – Hulk
    Nov 26 at 9:04














  • 1




    Thanks for sharing your solution. Could you add a sentence or two explaining what you did to solve your problem?
    – Hulk
    Nov 26 at 9:04








1




1




Thanks for sharing your solution. Could you add a sentence or two explaining what you did to solve your problem?
– Hulk
Nov 26 at 9:04




Thanks for sharing your solution. Could you add a sentence or two explaining what you did to solve your problem?
– Hulk
Nov 26 at 9:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400724%2fhow-to-implement-a-dynamic-list-in-a-fragment%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen