I'm trying to change the image of my listview dynamically.
But I can't figure how to change it because using setImageResource doesn't work.
Here's my code snippet:
public void onCreate(Bundle savedInstanceState) {
ImageView imgIco=(ImageView)findViewById(R.id.imgIco);
Integer id =getIntent().getExtras().getInt("id");
if(id==2) {
String[] valenzeComposti = getResources().getStringArray(R.array.int_valenze);
// I cannot do this ---------------> imgIco.setImageResource(R.drawable.ico1);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2,R.id.txtItem, valenzeComposti));
ListView lv2 = getListView();
lv2.setTextFilterEnabled(true);
lv2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v,int position, long id) {
Intent intent = new Intent(getApplicationContext(),InterClassi.class);
intent.putExtra("id",(int)id);
startActivity(intent);
}
});
}
The listview are created as follows -
The listview xml (R.layout.list_item2)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="5dip">
<ImageView
android:id="#+id/imgIco"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginRight="5dp"
>
</ImageView>
............
</RelativeLayout>
Please help me.. Thank you in advance
Maybe you should make you own implementation of ListAdapter. The most common way is extending BaseAdapter. List8 in the API Demos is a good example for making custom ListAdapters.
u may create your custom list view by extending baseadapter class to dynamically pass resources to listview. Have a look at this link-http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
hope this helps.
Related
I'm new to android, I want to use my custom layout file instead of simple_list_item_1 but its not allowing to do so. I want to add an backgroud image(which is defined in layout->custom_view.xml) to each items in the list OR help me to set the backgroud for the whole page, In the below code where should I use the setContentView Method. Also let me know if any changes are to be made in the below .xml file.
public class Planet extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
//setContentView(R.layout.custom_view); Tried to set the view from here but the application is crashing...
String[] planet_names = new String[] {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Sun"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planet_names);
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
Intent myIntent = new Intent(Planet.this, Galaxy.class);
startActivityForResult(myIntent, 0);
}
}
});
}
custom_view.xml file is as follows,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Simply use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.yourcustomview_withonetextview,R.id.yourcustomlayouttextviewid, planet_names);
You don't set the contentview in a ListActivity. The ListActivity already does that for you. Use an Activity, if you want to customize your layout.
You need to call super.onCreate() before setContentView(). And if you are going to use a custom layout in a ListActivity, you need to give the ListView the id #android:id/list.
I recently got interested in Android coding and working my way through several tutorials. however, right now i m stuck for a day and hope you guys can help me out.
basically i want to create a main menu and a submenu for every main menue entry. (right now this is only the case for the first entry)
For the main menu I used a ListView with different icons (realized with a custom adapter). Selecting one of the menu entries works fine in general, but if I choose to configure the onitemclick to start a new activity which is another (simple) ListView which uses the ArrayAdapter it breaks after switching into the submenu activity.
Strangely if i use the ArrayAdapter in both ListViews it is no problem; also when using CustomAdapter in both.
Here is the code, thanks in advance for any help ;)
main menu:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array contentmenu
String[] contentmenu = getResources().getStringArray(R.array.mainmenu_cats);
// storing icons in Array
TypedArray iconarray = getResources().obtainTypedArray(R.array.mainmenu_icons);
// Binding resources Array to ListAdapter
this.setListAdapter(new ImageAndTextAdapter(this, R.layout.list_item, contentmenu, iconarray));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
switch( position )
{
case 0: Intent newActivity0 = new Intent(getApplicationContext(),SubActivity.class);
startActivity(newActivity0);
break;
case 1: Intent newActivity1 = new Intent(getApplicationContext(),SubActivity2.class);
startActivity(newActivity1);
break;
}
}
});
}
submenu:
public class SubActivity extends ListActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] submen_1 = getResources().getStringArray(R.array.submenu_1);
// Binding resources Array to ListAdapter
setListAdapter(new ArrayAdapter<String>(ctx, R.layout.list_item1, R.id.submen1, submen_1));
ListView sublv1 = getListView();
// listening to single list item on click
sublv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
Intent newActivity = new Intent(getApplicationContext(),Page1.class);
// sending data to new activity
newActivity.putExtra("position",position);
startActivity(newActivity);
}
});
}
xml main menu:
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/option_icon"
android:layout_width="48dp"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
sub menu:
list_item1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/icon1"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:src="#drawable/ic_action_google_play" />
<TextView
android:id="#+id/submen1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
Currently you are not defining Hight and Width for LinearLayout in both list_item.xml and list_item1.xml layouts . add height-width as :
In list_item.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- PUT YOUR ImageView or other xml elements here -->
</LinearLayout>
and do same for list_item1.xml layout
I'm trying to create a ListView that displays 2 TextViews per row.
However I keep getting a NullPointerException when opening the Activity and I have no idea why (logCat logs don't tell me which line(s) on my end create the problem).
Here's the relevant code:
The main class EventViewActivity
public class EventViewActivity extends ListActivity {
public String[] eventTitles;
#Override
public void onCreate(Bundle savedInstanceState) {
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.eventview_layout);
eventTitles = new String[]{
getResources().getString(R.string.eventtitle1),
getResources().getString(R.string.eventtitle2),
getResources().getString(R.string.eventtitle3),
getResources().getString(R.string.eventtitle4),
getResources().getString(R.string.eventtitle5)
};
setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_title, eventTitles));
setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_descr, eventTitles));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "You have clicked an item", Toast.LENGTH_SHORT).show();
}
}
layout.xml is:
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView android:id="#+id/android:eventlist" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/eventview_background"/>
</LinearLayout>
Thanks!
does your eventview_layout has a ListView with id android:id="#android:id/list" ..? if not add it.. i assume its not done
a small correction..
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/eventview_background"/>
</LinearLayout>
its necessary that the contentView of ListACtivity has a listView with exact same Id..
I think the way you are approaching for ListView is not the correct.
Please have A look on this sample LINK to have complete view how create a ListView with custom adapter.
The above link has sample application created with listview and each listitem having two text fields as your requirement.
You should call the:
super.onCreate();
first!
I wanted to create a search view like the one Google uses. For this I created the following XML layout, which basically is a search bar and a button in the upper section of the screen and a ListView at the bottom of it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutSearch"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FF394952">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<EditText android:layout_height="wrap_content" android:id="#+id/searchTextBar" android:layout_width="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/searchButton" android:text="Buscar"></Button>
</LinearLayout>
<ListView
android:id="#+id/searchResultList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0" />
</LinearLayout>
And this is the code of the textViewResource that the ArrayAdapter demands on its constructor:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
Now, this is the code of the activity. So far, I just want to display the view with the contents (that's why I'm using a static String array for now).
public class SearchActivity extends Activity{
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item, COUNTRIES);
ListView lv = (ListView)this.findViewById(R.id.searchResultList);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
However, when I run the activity I see the search bar but it doesn't display the ListView.
I've tried changing the extension of SearchActivity to ListActivity, but the program just crashes when I try to start it. I'm also aware of the existence of the Search Interface, but I just want to see this particular method work.
Why it doesn't display the contents of the ListView? Is there a way to fix this?
Thanks in advance
If you are going to use ListActivity you should be aware that ListActivity already has a ListView instance. You need to call its setListAdapter method to set the adapter for its ListView instead of instantiating your own ListView and setting the adapter on it. You can call getListView to get a handle on ListActvity's ListView and then set the click listener on that.
If you want to extend ListActivity then you must have a ListView with id #android:id/list. Change the id of your ListView, that should fix the crash when extending ListActivity.
I want to change text color of the list item ,below is my code
public class HelloListView extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, name));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}); }}
My List.xml
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty set"
/>
< /LinearLayout >
Thanks :)
You are currently using an android built-in row component as a view for each row :
android.R.layout.simple_list_item_1
If you want to customize it, pick the code and put your own version in your app. The original code can be found on google code project for android :
Then you can customize xml and change the color attribute or change for a new selector state.
If you want to something even more custom, override your list's adapter getView method and provide a custom component inflating this xml and providing additional methods to be filled and display your data for each row.
Regards,
Stéphane
If you want to change the appearance of the elements in the list, Use the CustomListView and CustomAdapter.
http://www.androidpeople.com/android-custom-listview-tutorial-part-2
http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html
This Links may help you......