numbering the textviews - android

I am new in android programming.
I am creating a app where I supposed to use multiple textviews. Number of TextViews go on changing according to number of records fetched from database.
Here, i was trying to number textviews as textView1, textView2, textView3,...
but as i am unaware about the number of records, i can not define them statically
is there any way to do so dynamically
like we do in PHP
e.g
$count = 1;
if(condition)
{
textView.$count;
$count++;
}
Thanx in advance.

You should create a List of TextViews and populate it dynamically
List<TextView> list = new LinkedList<TextView>();

Find the total number of record at runtime and create the TextView in a loop for each textView and finally ad it to the current layout.
For example:
for(int i=0;i<data.size;i++){
TextView tv=new TextView(this);
tv.setText(data.get(i));
currentLayput.addView(tv);
}
where data is some vector or Arraylist in which you can store the data from database.

Related

All TextView in RecyclerView displaying same data

I have an arraylist containing 100 words. I want to assign each word to an individual textview of RecyclerView, but all the textviews of my Recyclerview show the same data, i.e the last word.
for(String s:arraylist){
viewholder.textview.setText(s);
}
Above is my Adapter Code for RecyclerView. How should I resolve this?
Try this:
viewholder.textview.setText(arraylist.get(position));
Instead of
for(String s:arraylist){
viewholder.textview.setText(s);
}
Use following below code for Set the values in textview from the ArrayList in `RecyclerView.
viewholder.textview.setText(arrayList.get(position));
I hope this may help you,

How to get text from TextViews which are created dynamically in android?

I do not know the number. of textview which should be created at runtime. List of Textviews are getting from server. I am creating textviews using below function. Since i am new to android i unable to get text from all textviews. Please suggest me the solution
private void textview(String text)
{
TextView txt_view= new TextView(this);
txt_view.setText(text);
linearLayout.addView(txt_view);
}
Please suggest me the solution i m stuck in getting the text from many textview
Above image is my form, what i want to get text from all views which are created dynamically and store to firebase
add your TextView to HashMap with a unique key
and match your key and get value from map, it will surely help you. after that at time of fetching data use for loop and get data using that unique key
Just get it from your TextView object, like this:
txt_view.getText().toString();
Use same object of TextView to gt text of it. txt_view.getText().toString();
You can get same TextView object by finding childs of LinearLayout.
How about to store them in an ArrayList and iterate over it to get all and maniupulate them afterwards.
ArrayList<TextView> alTextViews = new ArrayList<>();
private void textview(String text) {
TextView txt_view= new TextView(this);
alTextView.add(txt_view);
txt_view.setText(text);
linearLayout.addView(txt_view);
}
with
alTextView.get(0).getText().toString();
you'll get the text

List of Strings in multiple buttons

I have a list of Strings (Video tags) and I wanna show each of them in a button (same as when you want to send a text for multiple users) and when I click on each an action happens for that box. I took a look at:
https://github.com/kpbird/chips-edittext-library
It generate each string in a box but finally I don't have access to specific item onclick.
Does anyone has any suggestion what to use or how to make it?!
Read out the array of strings and
programmatically add a button with the text of each
string to for example a LinearLayout.
pseudo code:
ArrayList<string> list = getResources.array("id");
LinearLayout l = (Linearlayout) findViewById(R.id.linearlayout);
foreach(String s in list)
{
l.addView(new Button(s));
}
Something like that.

Update Text View as User Click on it

I have 8 TextView in my Linearlayout and What I am trying to achieve is as soon as user clicks on any TextView it should get updated with the next Textview below it ...
I am trying to make simple game where Image will be randomly displayed and the user has to find that Image name from the List displayed as different 8 TextView...
eg : If there is 8 TextView with List Dog, Cat , Bike , Horse, Cow etc.. and say Image of Cat has shown up then I want cat to disapper now every textview should get updated one row above so I can add more views at the end ...
I hope am not confusing because I am confused here ...
If you place all of the TextViews in an Array or a List, then it becomes simple array manipulation:
public void onClick(View pressed){
boolean tvFound = false; //have we found the clicked tv yet?
for(int i=0;i<mTextViews.length;i++){ //loop through all tvs
if(pressed.getId() == mTextViews[i]){ //if this is the tv that was clicked on
tvFound = true;
}
if(tvFound){ // if we have found the tv that was clicked on
if(i<mTextViews.length-1){ //if we arent on the last tv
mTextViews[i].setText(mTextviews[i+1].getText()); //then replace txt with next tvs text
}else{ //if we ARE on the last tv
mTextViews[i].setText(""); //replace text with nothing
}
}
}
}
I also want to note that if you don't want to loop through all of the TextViews to find the one that was clicked on, you can map the ResId of each TextView to its index in the array, then you can manipulate just the necessary TextViews.

how to display text in edit text in the list view

My team and I are making an android chat application. We want to display the messages sent by the users in a ListView. How can we display the text entered in an EditText in the list view. Or is there any other way to achieve our goal?
It all depends on what type of adapter you're using to back the view. If you're using an ArrayAdapter, just add the text to the array each time you have a new message you want to display (for instance, every time a button was clicked). If you're using a CursorAdapter, you'd then have to add the text to the ContentProvider that the Cursor is viewing. Beyond that, I can't tell you much else. We'd need more details about what you're doing in order to answer any further.
I've done this with some different by your needs. I just get the editText values to my database and printed into EditText. Just see this code -
EditText b = (EditText)findViewById(R.id.editText1);
b.setVisibility(1);
output = (TextView) this.findViewById(R.id.editText1);
output1 = (TextView) this.findViewById(R.id.editText2);
String s1= output1.getText().toString();
this.dh = new DataHelper(this);
this.dh.insert(s1);
List<String> names = this.dh.selectAll();
StringBuilder sb = new StringBuilder();
sb.append("Names in database:\n\n");
for (String name : names)
{
sb.append("\t - \t" + (name) + "\n");
}
this.output.setText(sb.toString());
Just change this with your needs. Try out using this
You can do this using the custom list view. In the custom list view use the custom adapter in which you can take an edit TextView. You can see this how to implement custom list view here. In this list view a TextView is used but you can replace this TextView with EditText view.

Categories

Resources