add textview to listView programmatically android code example

Example: how to add textview to listview in android

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>adapter=new ArrayAdapter<String>(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<?> arg0, View arg1, int arg2,
                        long arg3) {
                     s=s+("\n")+((String) lv.getItemAtPosition(arg2));
                    tv.setText(s);
                }

            });

    }

Tags:

Java Example