Sliding Drawer, Handle position and width of the drawer. - android

I recently stumbled upon the sliding drawer in Android, after creating my first project which implements the same I have a few issues. I am using a list view inside of the sliding drawer and for that I need to implement onClickListener and extend Activity, now everything is working fine, albeit the sliding drawer opens and occupies almost all of the space in the view, I want it to open only half screen. I went through various posts on internet which ask me to extend SlidingDrawer and use method onMeasure which is not feasible for me (Or is it?). Secondly I would want to place the handle button on the top not centre. Below is my code:
activity_main.xml~~~>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="top"
>
<TextView
android:text=""
android:gravity="center|center_vertical"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<SlidingDrawer
android:layout_width="wrap_content"
android:id="#+id/SlidingDrawer"
android:handle="#+id/slideButton"
android:content="#+id/contentLayout"
android:animateOnClick="true"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slideButton"
>
</Button>
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/contentLayout"
android:orientation="vertical"
android:gravity="center"
android:padding="10dip"
android:background="#0080FF"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
.java file~~~~>
public class slidingDrawerDemo extends ListActivity implements OnClickListener {
private ArrayAdapter<String> listAdapter ;
Button slideButton,b1, b2,b3,b4;
SlidingDrawer slidingDrawer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slideButton = (Button) findViewById(R.id.slideButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
ListView LV = getListView(); //(ListView)findViewById(R.id.list);
String []Test ={"Residential","Commercial","Customised Projects","SEZ","Brochure"};
ArrayList<String> Plist = new ArrayList<String>();
Plist.addAll(Arrays.asList(Test));
listAdapter = new ArrayAdapter<String>(this, R.layout.list_dem, Plist);
LV.setAdapter( listAdapter );
LV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView LV, View view,int position, long id) {
Toast.makeText(slidingDrawerDemo.this,""+position, Toast.LENGTH_SHORT).show();
if(position==0){
initOne();
}else if (position==1){
initTwo();
}else if (position==2){
initThree();
}else if (position==3){
initFour();
}else if (position==4){
initFive();
}
}
});
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
// slideButton.setBackgroundResource(R.drawable.closearrow);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
// slideButton.setBackgroundResource(R.drawable.openarrow);
}
});
}
My list_dem.xml file~~~>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>

I found the answer:
in my Slider defination in xml:
android:topOffset="100.5dp"

Related

Filter from Android onCreateOptionsMenu Item

I want to add filter button to my onCreateOptionsMenu. When button was clicked i need to show layout with checkboxes and apply button.
Only way that i know how to do this - start new activity from onOptionsItemSelected and do all operations there. But is there more efficient way? Without leaving current activity. Something like pop-up window etc.
You can do it By Using this Material Dialog
https://github.com/drakeet/MaterialDialog Library and Below code, Just
put the gravity of Dialog TOP|RIGHT.
private void showMaterialDialog() {
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice);
for (Card card : cards) {
arrayAdapter.add(card.getName());
}
alert = new MaterialDialog(this);
View mView = getLayoutInflater().inflate(R.layout.custom_view_virtual_card, null);
ListView listView = (ListView) mView.findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
AppCompatButton btnCancel = (AppCompatButton) mView.findViewById(R.id.btnCancel);
AppCompatButton btnOk = (AppCompatButton) mView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAsyncForRequestVirtualCard(selectedCardTypeId);
alert.dismiss();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedCardTypeId = cards.get(position).getId();
Log.d("request", cards.get(position).getId() + " " + cards.get(position).getName());
}
});
alert.setView(mView);
alert.show();
}
custom_view_virtual.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Select Card Type"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/colorAccent" />
<View
android:layout_width="match_parent"
android:layout_height="0.25dp"
android:background="#color/divider" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:divider="#color/transparent"
android:theme="#style/ProgressBarStyle"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnCancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="#string/cancel"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnOk"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
Don't create a new activity instead create a new java class in that apply your sorting logic to filter the content and when your button is pressed that activity should return all your sorted content using List class or ArrayList class or anything else then display it.

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>

Get access to checked item and position outside setOnItemClickListener

I have a list view with single selection mode and would like to get access the position outside the setOnItemClickListener event
As of now I can access its position as shown below
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//btnNxt.setEnabled(true);
Intent i = new Intent(getApplicationContext(),
NewActivity.class);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Now I would like to know how do I access the selected position outside(ie..on button click event).
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here I need to get the position and selected item
}
});
Listview.xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#3366CC"
android:dividerHeight="2dp"
android:choiceMode="singleChoice"/>
ItemDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.abc.widget.CheckableRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/sampleimg"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<TextView android:id="#+id/itemname"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/itemdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/carname"
android:focusable="false"/>
<TextView android:id="#+id/itemprice"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/cardesc"/>
<com.example.abc.widget.InertCheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:button="#drawable/checkbox" />
</RelativeLayout>
</com.example.abc.widget.CheckableRelativeLayout>
OrElse how do I check If any listview item is checked or not on button click ?
EDIT:
This is the way I'm trying to access the position from button click
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
myposition,
Toast.LENGTH_SHORT).show();
}
});
Simply make a member variable (declare it outside of a method like before onCreate() and use that.
public class ...
{
int pos;
// onCreate() and such
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
pos = position; // initialize it
...
}
});
then use pos inside your onClick() or wherever you need to in the Activity.
If I completely missed something in your question then please explain.

ListView Item not clickable

im trying to make an activity that searches a DB based on an input string and then returns the results in a list view. All this is working however, My listview items are not clickable. I have tried changing the focus and clickable attributes but nothing has worked so far.
Main XML (add_friend_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:id="#+id/AddFriendHeaderLayout"
android:layout_marginTop="3dip"
android:layout_height="45dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center">
<EditText
android:id="#+id/et_SearchFriend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/bt_SearchFriend"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:focusable="false"
android:icon="#android:drawable/ic_search_category_default"
android:text="Search" >
</Button>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="60dip"
android:layout_marginBottom="5dip"
android:layout_alignParentBottom="true"
android:layout_above="#+id/AddFriendHeaderLayout"
android:id="#+id/searchFriendFooterLayout">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</RelativeLayout>
ListView XML (single_listview_layout.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="wrap_content"
android:clickable="true" >
<TextView
android:id="#+id/tv_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
</LinearLayout>
Main Java Function (SearchFriendActivity.java):
public class SearchFriendActivity extends ListActivity {
//Progress Dialog
private ProgressDialog pDialog;
private Button bt_searchFriend;
private EditText et_Search_String;
private ListView tv_listview;
String[] arrFriends;
String[] arrUserId;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
setContentView(R.layout.add_friend_layout);
bt_searchFriend= (Button) findViewById(R.id.bt_SearchFriend);
et_Search_String = (EditText) findViewById(R.id.et_SearchFriend);
tv_listview = (ListView) findViewById(R.id.tv_list);
String searchString="";
//searchString = String.valueOf(et_Search_String.getText());
Log.i ("log_search","value of searchString: " + searchString);
bt_searchFriend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
try{
searchFriends aTask = new searchFriends();
aTask.execute(String.valueOf(et_Search_String.getText()));
String rResult = aTask.get();
// Convert aResult to array
arrFriends = convertJSONData(rResult);
arrUserId = convertJSONDataToUserArray(rResult);
setListAdapter(new ArrayAdapter<String>
(getBaseContext(),
R.layout.single_listview_layout,
R.id.tv_list,
arrFriends));
}catch(Exception e){
Log.e ("log_search_load",e.toString());
}
}
});
tv_listview = getListView();
//handler for List Item click
tv_listview.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(),
"Friend request sent for " +
((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
Log.i("log_search_listclick", "Listview clicked");
}
});
}catch(Exception e){
Log.e ("log_search_load2",e.toString());
}
}
Firstly, don't use focusable and clickable. When you use setOnclick or another they are auto creating.
Secondly, remove try cache blog then start application. Because when the problem created program will contining and you can't understand where is the problem.
===>Thirdly<====
you are used (tv_listview=...) second twice
tv_listview = (ListView) findViewById(R.id.tv_list);
tv_listview = getListView();
then you are trying tv_listview.setOnItemClickListener..... If second view is not first view seton not working on "R.id.tv_list".

Why the item selected return nothing when it was called?

I'm trying to create a Request Responses activity and get the itemSelected for use of next activity which is store them into user friendlist. But the itemSelected return nothing when the "Accept" button is clicked.(It should display itemSelected in toast dialog) Anyone knows what happen it is? Additionally, I like to ask why the item in the listView was located at the not accurate location? it seems like more upper that usual, for the second item.How to adjust it properly?
The figure below is layout of NotificationView.java:
NotificationView.java
public class NotificationView extends ListActivity{
String[] people;
ListView lv;
Button btn1,btn2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
NotificationManager mnotis =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mnotis.cancel(getIntent().getExtras().getInt("notificationID"));
String i = getIntent().getExtras().getString("name");
people = i.split("[,]");
Log.d("how",i);
lv= (ListView) findViewById(android.R.id.list);
btn1 = (Button)findViewById(R.id.acceptbtn);
btn2 = (Button)findViewById(R.id.closebtn);
ArrayAdapter<String> list = new ArrayAdapter<String>(NotificationView.this,R.layout.friendlist_item, R.id.friend_name,people);
lv.setAdapter(list);
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
lv = getListView();
int i = lv.getCount();
Log.d("count",String.valueOf(i));
runOnUiThread(new Runnable(){
public void run(){
String itemSelected = "Selected items: \n";
for(int i=0;i<lv.getCount();i++){
if(lv.isItemChecked(i)){
itemSelected += lv.getItemAtPosition(i) + "\n";
}
}
Toast.makeText(NotificationView.this,itemSelected,Toast.LENGTH_SHORT).show();
}
});
}
});
}}
view.xml
<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="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<FrameLayout android:id="#+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal" >
<Button
android:id="#+id/acceptbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.46"
android:text="Accept" />
<Button
android:id="#+id/closebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.49"
android:text="Close" />
</LinearLayout>
</FrameLayout>
friendlist_item.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="vertical" >
<CheckBox android:id="#+id/friend_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight = "88dip"
android:textSize="20dip"
android:textStyle="bold"/>
</LinearLayout>
Your Checkbox isn't related with the method that you are calling, for listview with multiselect items follow the next guide: http://dj-android.blogspot.com/2012/04/milti-selection-listview-android-with.html

Categories

Resources