Reputation:
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"?>
<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"
>
</TextView>
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"/>
Please help me how could i set those values in the array to the listview .i am new in android.Thanks
Upvotes: 6
Views: 29673
Reputation: 11
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"
/>
--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"
/>
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>{
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);
Upvotes: 1
Reputation: 6350
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> adapter = new ArrayAdapter<String>(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<?> 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();
}
});
}
Upvotes: 3
Reputation: 364
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>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);
}
});
}
Upvotes: 1