Add random data from listview items into Textview - java

When you click the button as it appears in the code, the data in the edit text is added as a list view.
When I click the button, I want to add a random data from the listview items and add it in the textview.
public class MainActivity extends AppCompatActivity {
ListView listgor;
EditText textgir;
Button katgir;
Button kuraceker;
TextView katilimcisecer;
ArrayList<String> data = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data.add("Hello World");
listgor = (ListView) findViewById(R.id.listeler);
textgir = (EditText) findViewById(R.id.sira1);
katgir = (Button) findViewById(R.id.katilimciekle);
kuraceker = (Button) findViewById(R.id.kuracek);
katilimcisecer = (TextView) findViewById(R.id.katilimci) ;
listgor.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data));
katgir.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
data.add(textgir.getText().toString());
textgir.setText("");
}
});
}
}

To add data from listview items to TextView use code below.
int randomIndex = ThreadLocalRandom.current().nextInt(0, data.size())
yourTextView.setText(data.get(randomIndex))

Related

Delete a listview item with a button in it self in android studio

As I am very new to java pls help me on this. I have a custom list view in my main activity and a Custom adapter with it. In my every list item there is a delete button that should delete that item when it clicked. I can not remove data from my arraylist when i am inside my custom adapter. Pls helm me in coding this delete button.
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText getItem;
Button AddButton;
Button DellButton;
public static ArrayList<String> myData = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView)
findViewById(R.id.listView);
getItem = (EditText) findViewById(R.id.newItem);
AddButton = (Button) findViewById(R.id.AddButton);
MyAdapter adapter = new MyAdapter(this, myData);
list.setAdapter(adapter);
AddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String result = getItem.getText().toString();
myData.add(result);
adapter.notifyDataSetChanged();
}
});
}
MyAdapter.java
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, ArrayList<String> records) {
super(context, 0, records);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String item = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_custom, parent, false);
}
final TextView lst_txt = (TextView) convertView.findViewById(R.id.list_Txt2);
Button plusbut = (Button) convertView.findViewById(R.id.plusbut);
Button minusbut = (Button) convertView.findViewById(R.id.minusbut);
final TextView sum = (TextView) convertView.findViewById(R.id.sum);
Button cal = (Button) convertView.findViewById(R.id.calButton);
Button delete = (Button) convertView.findViewById(R.id.btnDel);
lst_txt.setText(item);
minusbut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int sumAll = Integer.parseInt(sum.getText().toString());
int sum1 = sumAll - 1;
sum.setText(String.valueOf(sum1));
}
});
plusbut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int sumAll = Integer.parseInt(sum.getText().toString());
int sum1 = sumAll + 1;
sum.setText(String.valueOf(sum1));
}
});
cal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s = sum.getText().toString();
Intent intent = new Intent(getContext(), calll.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("sumFL", s);
getContext().startActivity(intent);
}
});
return convertView;
}
}
Please first try to remove object from list by using the position of item with check the validation with list size and then call notifyItemadapter to update the list view.
Use ViewHolder class for all view like textview, button etc. And initialize them inside the condition
if(convert view==null){
Initialize holder object here and
Inflate your layout and
Initialize button like
holder.deletebutton = convert view.findviewbyid from xml
settag(holder)
}
Again get the holdet using the gettag in
else{
//Here
}
Put All click event and text update etc. Outside of above condition
holder.deletbutton.setonclicklistener{
int pos = view.getag
list.remove(pos)
Notifyadapter here
}
holder.deletebutton.settag(position)

When adding Values to Arraylist from one class to another, Listview does not display updated values

I am having one Array list in Class-A and two textfields one from Class-A and other from Class-B,
While adding values to Array List through Class-A textfield it works fine for me,
and when trying to add from Class-B textfield, it gets added to Array list but does not display the updated list in the listview(Blank)
Below is the code i have tried till now
Class-A :-
public static ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
ListView listView;
EditText quickTask;
Button addButton;
Button moreButton;
public static ArrayList<String> addToList( String i ) {
arrayList.add(i);
return arrayList;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
quickTask = (EditText) findViewById(R.id.quicktaskeditText);
listView = (ListView) findViewById(R.id.TasksListView);
arrayAdapter = new ArrayAdapter<String>(ClassA.this, android.R.layout.simple_list_item_1,arrayList);
addButton = (Button) findViewById(R.id.Addbutton);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String task = quickTask.getText().toString();
arrayList.add(task);
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
});
moreButton = (Button) findViewById(R.id.moreButton);
moreButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent secondActIntent = new Intent(getApplicationContext(),ClassB.class);
startActivity(secondActIntent);
}
});
}
Class-B :-
EditText Title;
Button AddButton;
ArrayList<String> alist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Title = (EditText) findViewById(R.id.Title);
AddButton = (Button) findViewById(R.id.secondAddButton);
AddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String task = Title.getText().toString();
//Adding text to Class-A arraylist
alist = ClassA.addToList(task);
Intent startIntent = new Intent(getApplicationContext(),ClassA.class);
startActivity(startIntent);
}
});
}
Because you don't notify ListView that contents of ArrayList changed.
You have notifyDataSetChanged only in button listener. You need to add it in Class-A#onResume or in addTolist.

How to add items from Edittext to listview?

I have been striving for a couple of hours but I still can't figure out why my latest edittext value overwrites my old listview item.
The problem is that, suppose I type a string s1 in my edittext and press the add to listview button, then the string s1 gets entered successfully. Now if I type a string s2 in my edittext and press the add to listview button, the string s2 now replaces s1 in the listview, and thus now it is just showing s2 in my listview. How do I fix this?
Here is my code:
String str=edittext.getText().toString();
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(My_activity.this, android.R.layout.simple_list_item_multiple_choice, list);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(str.length()!=0) {
list.add(str);
}
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
});
Edit: I am trying to take the input string from one activity and trying to add it to the listview of another activity. This is where the problem arises. Please help me.
1st Activity:
public static String str;
add.setOnClickListener(new (new View.OnClickListener() {
#Override
public void onClick(View view) {
str=edittext.gettext().toString();
}
});
2nd Activity:
List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(My_activity.this, android.R.layout.simple_list_item_multiple_choice, list);
if (Main_activity.i == R.id.event_1 || Main_activity.i == R.id.rem_1) {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
if(str.length()!=0) {
list.add(str);
adapter.notifyDataSetChanged();
}
else{
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "No
text found", Snackbar.LENGTH_LONG);
snackbar.show();
}
}
public class ListViewExample extends AppCompatActivity {
List<String> list = new ArrayList<String>();
ListView listView;
EditText editText;
ArrayAdapter<String> adapter;
FloatingActionButton fab
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view_example);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listView = (ListView) findViewById(R.id.listView);
editText = (EditText) findViewById(R.id.et);
fab = findViewById(R.id.fab);
adapter = new ArrayAdapter<String>(ListViewExample.this, android.R.layout.simple_list_item_multiple_choice, list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name = editText.getText().toString().trim();
if(!name.isEmpty()) {
list.add(name);
adapter.notifyDataSetChanged();
editText.setText("");
}
}
});
}
This is my Activity, and this is work for me.
Although the above code is correct, please try to modularize your code. It will make your life easier when comes to debugging it and sequentially follow it. See below
private final List<String> list = new ArrayList<>();
private ArrayAdapter<String> mAdapter;
private EditText editText;
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
public void initViews() {
editText = findViewById(R.id.et);
Button addOptionsBtn = findViewById(R.id.button);
addOptionsBtn.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
addOptionToList();
}
});
mAdapter =
new ArrayAdapter<>(getBaseContext(),
android.R.layout.simple_list_item_multiple_choice,
list);
ListView listView = findViewById(R.id.options_list_view);
listView.setAdapter(mAdapter);
}
private void addOptionToList() {
String option = editText.getText().toString();
if (option.isEmpty()) {
return;
}
list.add(option);
mAdapter.clear();
mAdapter.addAll(list);
mAdapter.notifyDataSetChanged();
}
I've finally found the solution. I guess the adapter wasn't updating after getting a new item. So what I did was:
1st Activity:
public static String str;
add.setOnClickListener(new (new View.OnClickListener() {
#Override
public void onClick(View view) {
str=edittext.gettext().toString();
list.add(s3);
}
});
2nd Activity:
public static List<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(My_activity.this, android.R.layout.simple_list_item_multiple_choice, list);
adapter.notifyDataSetChanged();
if (Main_activity.i == R.id.event_1 || Main_activity.i == R.id.rem_1) {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
if(str.length()!=0) {
list.add(str);
}
else{
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "No
text found", Snackbar.LENGTH_LONG);
snackbar.show();
}
}

How to update TextView using listView every second [duplicate]

This question already has an answer here:
Display Strings from a listView every second [closed]
(1 answer)
Closed 5 years ago.
I have a simple android application. Inside that app, there is an editText which inputs an item to a listView on button click. Then when a button with onClick ''start'' is clicked, it displays a textView which shows the listview items as a whole.
public class Math extends AppCompatActivity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
TextView displayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math);
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
displayText = (TextView)findViewById(R.id.displayText);
//displayText is textView that displays list values.
}
public void AddItem(View v) {
//Where items are added to the list
EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
items.add(itemText);
etNewItem.setText("");
}
public void start(View v) {
//Where it displays text
RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
lc.setVisibility(View.GONE);
RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
tc.setVisibility(View.VISIBLE);
for (int i = 0; i < items.size(); i++) {
displayText.setText(displayText.getText().toString() + "\n" + items.get(i));
}
}
}
Is there any way that I can display the listView items in displayText one by one every second.
Here is updated code, as per your requirement::
public class Math extends AppCompatActivity {
int listSize=0;
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
TextView displayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math);
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
displayText = (TextView)findViewById(R.id.displayText);
//displayText is textView that displays list values.
}
public void AddItem(View v) {
//Where items are added to the list
EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
items.add(itemText);
etNewItem.setText("");
}
public void start(View v) {
//Where it displays text
RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
lc.setVisibility(View.GONE);
RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
tc.setVisibility(View.VISIBLE);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if(count>items.size())
timer.cancel();
else {
displayText.setText(displayText.getText().toString() + "\n" + items.get(count));
}
}
});
}
}, 0, 1000);
}
}
}
I think it will work as per your expectation, if something to change please tell me I will do.
thanks and happy coding.

spinner element to listview on click app crash(android)

so im a beginner in android/java programming and im trying to do the following: the selected item from my spinner should go in a listview when I click a button.So far I did this,eclipse doesnt show any errors or warnings,but my app crash when I try to launch it:
public class MainActivity extends ActionBarActivity {
Button button3;
Button button2;
Button button4;
ListView listView1;
Spinner s1;
String text;
ArrayAdapter<String> adapter;
ArrayList<String> list;
int itemPos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button2 = (Button) findViewById(R.id.button2);
s1 = (Spinner) findViewById(R.id.spinner1);
listView1 = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, list); //this part here might not be correct
listView1.setAdapter(adapter);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String text = s1.getSelectedItem().toString();
adapter.add(text);
}});
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
itemPos = position;
}
});}}
I dont get why it crash, how to fix that?
thank you!
Declare,
ArrayList<String> list = new ArrayList<String>();
Now, you can not text in ArrayAdapter directly, for that we have created ArrayList.
So do like this way,
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String text = s1.getSelectedItem().toString();
list.add(text);
// To notify adapter after that changes in data
adapter.notifyDataSetChanged();
}
});
Let's if that's the problem.

Categories

Resources