Title: How to add A TextView Dynamically in the ListView Items Android?
Tags: android;android-layout;android-listview
Question: I want to add a TextView and Set it's value dynamically in the ListView and On click of the item i want to get the value of the item clicked .
I have a array
String[] statesList = {"listItem 1", "listItem 2", "listItem 3"};
My TextView XML is
<?xml version="1.0" encoding="utf-8"?&gt;
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:textStyle="bold"
&gt;
</TextView&gt;
My ListView XML
<ListView
android:id="@+id/list"
android:choiceMode="singleChoice"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_width="290dp"/&gt;
Please help me how could i set those values in the array to the listview .i am new in android.Thanks
Comment: yup each item will have only one textview that have text from the array

Comment: @user2652394 same what u said i want to do this how could i do that please help me

Comment: in the listview item should be like item1 ,then second item2 so on

Comment: each row with a single textview?

Comment: @GauravPandey each row with item like 1st row item listItem 1 second row listItem 2 and so on..?

Comment: @GauravPandey list item meaning same row item 1 and item 2 or row 1 item 1 row2 item2 and so on..

Comment: http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html

Comment: Instead of adding more textview everytime, it's a bad practice and consume alot of memory. Just use a single textview and add `\n` to the `text` everytime you want to add a new textview. Hope this helps.

Comment: @GauravPandey, `if(state == listItem 1) text = your_text else if(state == listItem 2) text = your_text + "\n" + your_second_line_text else if(state == listItem 3) text = your_text + "\n" + your_second_line_text + "\n" + your_third_line_text`. Hope this helps

Here is the accepted answer: Use this code i hope this is what u want
public void showstatesList() {
listView = (ListView) findViewById(R.id.list);
String[] statesList = {"listItem 1", "listItem 2", "listItem 3"};
ArrayAdapter<String&gt; adapter = new ArrayAdapter<String&gt;(this,
android.R.layout.simple_list_item_1, android.R.id.text1, statesList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?&gt; parent, View view,
int position, long id) {
int itemPosition     = position;
String  itemValue    = (String) listView.getItemAtPosition(position);
//              Toast.makeText(getApplicationContext(),
//                "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
//                .show();
}
});

Here is another answer: I hope this will help you..
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
ListView lv;
String s="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView1);
lv=(ListView) findViewById(R.id.listView1);
String value[]={"asda","Ansar","Nisam"};
ArrayAdapter<String&gt;adapter=new ArrayAdapter<String&gt;(getBaseContext(), android.R.layout.simple_list_item_1,android.R.id.text1,value);
lv.setAdapter(adapter);
//list item click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?&gt; arg0, View arg1, int arg2,
long arg3) {
s=s+("\n")+((String) lv.getItemAtPosition(arg2));
tv.setText(s);
}
});

Here is another answer: To create a ListView is an easy thing. All what you do is to create:
-XML code :
--create a  element inside your main XML file, with its size, id attributes :
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainList"
/&gt;
--another XML file , that you wanna make form of your List elements, like the from of your textView etc.. inside 'layout' file :
elementsForm.xml :
if you want to make list of countries for example ,
so create a LinearLayout that you will put text
form inside with "vertical" value to 'android:orientation' attribute
inside it create your TextView :
<TextView
android : layout_width="match_parent"
android : layout_height="wrap_content"
android : textSize="25sp"
android : id="@+id/texty"
/&gt;
INSIDE YOUR JAVA FILE "MainClass" :
--create an inner class and let it inherits from ArrayAdapter
and create a constructor that takes 2 Parameters : the first Type is
Context , the second Type is Data Type that you wanna display it and pass
three values to the super constructor (ArrayAdapter):
1th : the context that your own class takes
2th : your own XML file that will consist and display the value like text View, audio View etc ...
3th : the array of values that we wanna display that our class takes as second parameter of its constructor
public class MyOwnInnerClass extends ArrayAdapter<String&gt;{
public MyOwnInnerClass(Context context, String countries[]){
super(cotext , R.layout. , countries);
inside your class now override the getView() method that takes
@Override
getView(int pos , View convert  , ViewGroup vg);
it consist three parameters that could provide with the element position and to make the elemenet inside our own XML file
and parent as a reference to the parent View :
Now go easy on this method and do ur purpose :
inflate your List :
LayoutInflater li = LayoutInflater.from(getContext());
: we pass to the context of our outclass that will be used
convert = li.inflate(parent , R.layout.elementsForm,false);
we pass to our View paramter of getView() method the inflate li object function and we pass to 3 values
the first :  our own XML file ID ;
the second one : parent the instance of ViewGroup ;
the third one : we pass false ;
now we must getItems using getItem function pass to position parameter :
String st = getItem(position);
create TextView object and pass to your your xml using convert parameter:
TextView tv  = convert.findViewById(R.id.texty);
then set Text to your own text View :
tv.setText(st);
now don't forget the most important statement : the return Type method : return convert ;
---------------------------------------
here we go back to our "MainClass"  , we create instance of ListView , ListAdapter instances :
`ListView lv ;
ListAdapter la ;
` and inside the onCreate method :
0)String [] countries = {"US" , "Canada","Egypt","China"};
1) lv = findViewById(R.id.mainList);
2) la = new MyOwnInnerClass(MainClass.this,countries);
0) we define a String array named countries contains four Values those we wanna display .
1) here we pass to lv object the id of our Element 'ListView' that will consist countries values with View Text ;
2) we passed an anonymous class that its constructor takes two parameters first context of the main class and the second the values we wanna display
the last operation to connect our ListView element with our own ListView Adapter
Class 'D' :
is declaring this function :
lv.setAdapter(la);

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 9, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:08/14/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><android-layout><android-listview>', 'Title': 'How to add A TextView Dynamically in the ListView Items Android?', 'ViewCount': 28026, 'id': 18228869, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}