Can we add multiple listviews in a single layout - android

I have Three list to display.How to add them on single layout?
my layout file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/list3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
I am using CustomAdapter for each list
mylistview1 = (ListView) findViewById(R.id.list1);
mylistview2 = (ListView) findViewById(R.id.list2);
mylistview3 = (ListView) findViewById(R.id.list3);
//1
CustomAdapterA adapterA = new CustomAdapterA(this, rowItems1);
mylistview1.setAdapter(adapterA);
mylistview1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String member_name = rowItems1.get(position).getMember_name();
Toast.makeText(getApplicationContext(), "" + member_name,
Toast.LENGTH_SHORT).show();
}
});
//2
CustomAdapterM adapterM = new CustomAdapterM(this, rowItems2);
mylistview2.setAdapter(adapterM);
mylistview2.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String member_name = rowItems2.get(position).getMember_name();
Toast.makeText(getApplicationContext(), "" + member_name,
Toast.LENGTH_SHORT).show();
}
});
//3
CustomAdapterS adapterS = new CustomAdapterS(this, rowItems3);
mylistview3.setAdapter(adapterS);
mylistview3.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String member_name = rowItems3.get(position).getMember_name();
Toast.makeText(getApplicationContext(), "" + member_name,
Toast.LENGTH_SHORT).show();
}
});
I tried with putting each layout in separate Linear Layout but didnot worked,
can anyone suggest what should be done here???

Try making making each of the ListViews take up exactly 1/3 of the screens height and see if they're visible then.
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1
/>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1
/>
<ListView
android:id="#+id/list3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1
/>

Initially a listview looks like it occupies the whole screen. You have to set their widths accordingly to see them in one screen.

If your idea is to finish one list the next one will be visible the answer is:
No, you cannot do that. On your adapter you should Override getViewTypeCount() and return 3 and then generate the correct view for each position.
If you're idea is to have each list occupying 1/3 of the screen you must use the layout_weight parameter in your XML like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<ListView
android:id="#+id/list3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>

Related

Touch not working on list view

I am using touch event on LinearLayout in which there is a list view(Its a child). When I touch on list view part touch event does not gets called. below is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llFavourite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/llProgressBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:gravity="center"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Loading"
android:indeterminate="true"
android:progressDrawable="#android:drawable/progress_indeterminate_horizontal"
android:visibility="gone" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
android:focusable="true">
</ListView>
</LinearLayout>
My animation:
final Animation slidedown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down_half);
llFavourite.setOnTouchListener(new OnSwipeTouchListener(MidnightMainActivity.this) {
#Override
public void onSwipeBottom() {
llFavourite.startAnimation(slidedown);
inflateFavouriteLayout(MODE_REFRESH);
}
});
but when I click on layout part event gets called. Is there any property which i should enable or disable?
Thanks in advance.
android:focusable="false"
android:focusableInTouchMode="false"
doesn't work for ImageButton.
In your layout xml, add this property to root layout
android:descendantFocusability="blocksDescendants"
from your calling class add
ListView lView = (ListView) findViewById(R.id.list);
lView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(position);
}
});

ImageButton click event works, but ListView click event does not

ListView:
<ListView android:id="#+id/list11"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="#00000000">
</ListView>
custome.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_groupname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_weight="3"
android:text="asdasd"
android:textColor="#color/TextColor" />
<ImageButton
android:id="#+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/delete_gp"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
Activity.java:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long itemValue = listView.getItemIdAtPosition(position);
Log.e("==>", "" + itemValue);
}
});
When I click on the <ImageButton> it works fine, but when I click on <ListView> it does not work.
How can I solve this problem?
ImageButton takes focus when you click on list item
Add the below
android:descendantFocusability="blocksDescendants"
to the root element in custome.xml
You will use this code and it will ask to cast the object you just put the casting after it will fine....
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long itemValue = parent.getItemIdAtPosition(position);
Log.e("==>", "" + itemValue);
}
});
add android:clickable="true" in the textview and the imagebutton also

ListView item not found

My app is using landscape full screen mode and the navigation drawer. I am using listView in my app along with an edittext. The edittext is the search bar that will search the listview. Both the listview and the edittext are in the navigation drawer. But, when there is no list item that matches the searched word, the listview gets empty.
So how can I add a "Item not found" message instead of the blank listview?
I searched a lot on the internet and found a method setEmptyView(); but I couldn't understand it and hence it is not working. Please help me! Maybe this question is already asked here but please give me an easy explanation.
Here is my code:
MainActivity.java
public class MainActivity extends FragmentActivity {
final String[] data = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Flourine","Noen","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium"};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
//the code below will automatically close the keyboard when the user will touch the listview
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
}
mainactivity.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="240dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="#+id/searchbar"
android:layout_width="230dp"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:padding="10dp"
android:background="#drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint=" search" >
</EditText>
<TextView
android:id="#+id/notfound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Medium"
android:gravity="center">
</TextView>
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Yes you have mentioned about setEmptyView() correctly, you should use it if you would want to show empty message whenever ListView gets empty.
Now here is a xml layout and code depicts how to use setEmptyView exactly.
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/layoutTitlebar" >
<ListView
android:id="#+id/listViewFriends"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/friendBGColor"
android:cacheColorHint="#00000000" >
</ListView>
<TextView
android:id="#+id/empty"
style="#android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="#string/strNoRecordsFound" >
</TextView>
</LinearLayout>
Now, you have to set this empty view (i.e. TextView) to ListView by using:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
// set your adapter here
// set your click listener here
// or whatever else
listViewFriends.setEmptyView(findViewById(R.id.empty));
This method is all that you need
http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View)
Have in mind it accepts a view, so you have to inflate the layout and fill out any/all text you may have there by yourself.
EDIT:
Let's assume you have a layout named "empty_text" like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="There are no entries in this list"
android:gravity="center"/>
</LinearLayout>
Hint: I burned in the string for example purposes, heed the IDE warning and use a string identifier for I18n's sake
Now you would use this code to make it all work with the ListView:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View emptyTextView = inflater.inflate(R.layout.empty_text, null, false);
listView.setEmptyView(emptyTextView);
This code assumes you are executing inside an Activity, but just in case you don't plan on pasting it on an Activity, any Context instance will work.
That should be it.
ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
Then your listview will automatically use this view when its adapter is empty.
Detailed Solution:
Layout:
<ListView
android:id="#+id/listViewFriends"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/friendBGColor"
android:cacheColorHint="#00000000">
</ListView>
<TextView
android:id="#+id/empty"
android:text="#string/strNoRecordsFound"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Large"
android:gravity="center">
</TextView>
</LinearLayout>
Class File:
ListView listViewFriends = (ListView) findViewById(R.id.listViewFriends);
listViewFriends.setEmptyView(findViewById(R.id.empty));
Source

two textviews listview android

I have implemented search functionality in my listview which is in my navigation drawer. But currently it has only one textview. I want to be able to add two textviews on the same line without the need of a different class or adapter. Having said that, I still want to be able to search the first textview. So basically my question is : How do I add two textviews to a row in a listview without using any other class?
Here's my code:
listitem_row.xml
<!-- This is the xml which i use to display one textview on a list row-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
</LinearLayout>
main_activity.xml
<!--This is my main activity xml where I have the navigation drawer and the
listview inside the drawer-->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="-65dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="#+id/searchbar"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:drawableLeft="#drawable/search"
android:padding="10dp"
android:background="#drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint="Search" >
</EditText>
<RelativeLayout
android:id="#+id/empty"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="-65dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<TextView
android:id="#+id/empty_text"
android:text="#string/notfound"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#ffababab"
android:textSize="15dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center">
</TextView>
</RelativeLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
//This is my mainactivity
public class MainActivity extends Activity {
final String[] elename ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","gadoliium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einsteinium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium","Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};
final String[] nos = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118"};
//I have already added the String elename to my listview
//and I want to add String nos to it without using any other class.
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//The line below adds the string elename to the listview
//but using the same line i want to add the string nos if possible.
adapter = new ArrayAdapter<String>(this, R.layout.listitem_row,R.id.textView1, elename);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
//Change view according to numbers
drawer.closeDrawer(linearLayout);
}
});
//filter list view after search instantly
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
//The line below removes all the spaces while searching
String searchedquery = cs.toString().replaceAll(" ","");
//The line below searches the listview and shows the results
MainActivity.this.adapter.getFilter().filter(searchedquery);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
//hide the keyboard after search on touch list view
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
//The code below hides the keyboard when the user touches the listview
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
navList.setEmptyView(findViewById(R.id.empty));
}
}
For binding two TextViews you need to create Custom Array Adapter. Go to below links for better understanding:
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
http://prativas.wordpress.com/category/android/custom-listview-in-android-using-custom-arrayadapter/
http://jmsliu.com/1390/rss-reader-app-android-tutorial-1-listview-and-arrayadapter.html
https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
Update:
ListView searching Functionality:
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
http://www.myandroidsolutions.com/2013/08/04/android-listview-with-searchview/
http://sunil-android.blogspot.in/2013/09/custom-listview-alertdialog-with-filter.html
http://android-solution-sample.blogspot.in/2011/10/android-search-in-custom-listview.html
Here are the ideas. Go to any links and customized as per your requirement.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout>

android how to access customized listview item

i tryied to custom my ListView , so i use this item layout 「 list_item.xml」
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="10dp"
android:textSize="16dip"
android:textColor="#000000"
android:gravity="center_vertical"
android:background="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1">
</TextView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#FFFFFF"
android:padding="10dip"
android:gravity="center_vertical">
<ImageView
android:id="#+id/pause"
android:src="#drawable/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pauseBtn_onClick"
/>
<ImageView
android:id="#+id/play"
android:src="#drawable/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playBtn_onClick"/>
</RelativeLayout>
</LinearLayout>
just as what you saw, there are 2 images in this layout, what i wanna do is,
when user click any item , the play_icon(the second image in the listview item) , will
disappear.
so i write code as the following :
mp3_listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View _view, int index , long arg3)
{
String _url = musicList.get(index).get("link").toString();
LinearLayout _container = (LinearLayout)mp3_listView.getChildAt(index);
RelativeLayout _container2 = (RelativeLayout) _container.getChildAt(1);
ImageView img = (ImageView)_container2.getChildAt(1);
img.setVisibility(View.INVISIBLE);
Toast.makeText(tabDigTest.this , "GG : "+_container.findViewById(R.id.play) , Toast.LENGTH_SHORT).show();
}
}
but something strange occurred .... more than one item's img be hidden because of the:
img.setVisibility(View.INVISIBLE);
i don't why because i only set one img to INVISIBLE ......i think i need help O_o
To retrieve the user click, I guess you set up an OnItemClickListener.
ListView lv = (fecth your list vieW)
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//you might want to use 'view' here
}
});
In the onItemClick method, the view parameter is the view you returned in your ListAdapter's getView method. So if say, you have returned a LinearLayout containing three childrens with id ("textview", "imageview1", "imageview2"), you would simply call view.findViewById(R.id.imageview1) to retrieve the first ImageView.

Categories

Resources