I have a problem with intent it does not work - android

I need a help with my app, I am creating music app by using listView and adapter,image for main activity
i use imagebutton to go to details activity but when i click on it no thing happen
this is main activity code
package com.example.musicano;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Song> songs=new ArrayList<Song>();
songs.add(new Song("See You Again","Wiz Knalifa"));
songs.add(new Song("Sorry","Justin Bieber"));
songs.add(new Song("Uptown Funk","Mark Ronson "));
songs.add(new Song("Blank Space","Taylor Swift"));
songs.add(new Song("Shake It Off","Taylor Swift"));
songs.add(new Song("Lean On","Major Lazer"));
songs.add(new Song("Hello","Adele"));
songs.add(new Song("Roa","Kary Perry"));
songs.add(new Song("Sugar","Sugar"));
songs.add(new Song("All About That Bass","Meghan Trainor"));
songs.add(new Song("Dark Horse","Katy Perry"));
songs.add(new Song("Counting Stars","Onerepublic"));
songs.add(new Song("Baby","Justin Biebe"));
songs.add(new Song("Chandelier","Meghan Sia"));
ListView listView=(ListView) findViewById(R.id.list);
SongAdapter adapter=new SongAdapter(this,songs);
listView.setAdapter(adapter);
LayoutInflater inflater = getLayoutInflater();
View view= inflater.inflate(R.layout.list_item,listView,false);
ImageButton playMusic = (ImageButton)view.findViewById(R.id.playButton);
playMusic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Details.class);
startActivity(intent);
}
});
}
}
and custom adapter code is
package com.example.musicano;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class SongAdapter extends ArrayAdapter<Song> {
public SongAdapter (Activity context, ArrayList<Song>songs){
super(context,0,songs);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
Song cureentSong=getItem(position);
View listItemView=convertView;
if(listItemView==null){
listItemView= LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
TextView songName=(TextView) listItemView.findViewById(R.id.text_view_song_name);
TextView artistName=(TextView) listItemView.findViewById(R.id.text_view_artist_name);
ImageButton playMusic=(ImageButton) listItemView.findViewById(R.id.playButton);
songName.setText(cureentSong.getSongName());
artistName.setText(cureentSong.getArtistName());
return listItemView;
}
}
and activity_main.xml contain
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list"
tools:context=".MainActivity">
</ListView>
and list_item layout contain
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:id="#+id/relativlayout"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text_view_song_name"
tools:text="one"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#232F34"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/text_view_artist_name"
tools:text="two"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#232F34"/>
</LinearLayout>
<ImageButton
android:id="#+id/playButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play"
android:layout_marginRight="26dp"
android:layout_marginTop="26dp"
/>
</RelativeLayout>
that is it

What is reason for doing view.findViewById, the button is in MainActivity itself right?
Try the following and check if this works?
Hope this helps
ImageButton playMusic = (ImageButton)findViewById(R.id.playButton);
playMusic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Details.class);
startActivity(intent);
}
});
If the image button is in list_item_layout then add the click listener in the adapter instead of MainActivity. Then it should work.
Add the following in your adapter code:
ImageButton playMusic=(ImageButton) listItemView.findViewById(R.id.playButton);
playMusic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,Details.class);
intent .addFlags(FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
});
Use the context used in your Adapters constructor create a instance
private Context mContext;
and inside your adapter's constructor assign it to context as shown below:
this.mContext = context
Hope this helps.

Related

How to delete a particular view on the click of button in a ListView in Android?

I have made a custom view that consists of two text views and one button. I just want when i click on the button then the respective view is gone and list adjust itselt.
here is my MainActivity.java
package com.dv.deletev;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView lv;
String name[]={"Ankit","Arora","Arun","yadav"};
String no[]={"AnAnaAN","cccc","bbbbb","aaa"};
static CustomAdapter obj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
obj=new CustomAdapter(MainActivity.this,name,no);
lv.setAdapter(obj);
//obj.notifyDataSetChanged();
}
public static CustomAdapter take()
{
return obj;
}
}
CustomAdapter.java
package com.dv.deletev;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.dv.deletev.*;
public class CustomAdapter extends ArrayAdapter<String>
{
String name[];
String no[];
Context con;
MainActivity a;
public CustomAdapter(Context con,String a[],String b[])
{
super(con,R.layout.second,a);
name=a;
this.con=con;
no=b;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater lv=(LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=lv.inflate(R.layout.second, null);
TextView tv1=(TextView)convertView.findViewById(R.id.textView1);
TextView tv2=(TextView)convertView.findViewById(R.id.textView2);
Button bt=(Button)convertView.findViewById(R.id.button1);
final List<String> arr1;
final List<String> arr2;
arr1=(List<String>) Arrays.asList(name);
arr2=(List<String>) Arrays.asList(no);
final ArrayList<String> arr=new ArrayList<String>(arr1);
final ArrayList<String> ar=new ArrayList<String>(arr2);
bt.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
arr.remove(position);
ar.remove(position);
a.take().notifyDataSetChanged();
//CustomAdapter.this.notifyDataSetChanged();}
}
});
tv1.setText(name[position]);
tv2.setText(no[position]);
return convertView;
}
}
Second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="30dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
activity_main.xml
-----------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.dv.deletev.MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
there is no such problem on logic part everything works fine, ListView appeared is what i wanted but on the click of button nothing happens.
Any suggestions...?
There are a lot of problems in this code, sorry if i will seem salty, i fixed only some necessary things:
two arrays of string with same length passed in an adapter. It's better to use a custom object that wraps these two string instead of two arrays
the transformation to arraylist without any purpose
the reference to the adapter from activity context instead from the adapter itself
the confusion about use arrays items but remove lists items
the code indentation
the code variable's name
package com.dv.deletev;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.dv.deletev.*;
public class CustomAdapter extends ArrayAdapter<String>
{
String name[];
String no[];
Context con;
MainActivity a;
//declare them global
ArrayList<String> arr;
ArrayList<String> ar;
public CustomAdapter(Context con,String a[],String b[])
{
super(con,R.layout.second,a);
name=a;
this.con=con;
no=b;
// initialize them in constructor
arr = new ArrayList<String>(Arrays.asList(name));
ar = new ArrayList<String>(Arrays.asList(no);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater lv=(LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=lv.inflate(R.layout.second, null);
TextView tv1=(TextView)convertView.findViewById(R.id.textView1);
TextView tv2=(TextView)convertView.findViewById(R.id.textView2);
Button bt=(Button)convertView.findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
arr.remove(position);
ar.remove(position);
notifyDataSetChanged();
//don't use reference of the adapter itself from the activity
//a.take().notifyDataSetChanged();
//CustomAdapter.this.notifyDataSetChanged();}
}
});
// set them to list
tv1.setText(arr.get(position));
tv2.setText(ar.get(positon));
return convertView;
}
}
You convert the array to a list and remove the list item but you don't convert the list back to the array, so the remove is lost.
ArrayAdapter already has a remove() method, you should just use that.

Using getFragmentManager on a ListActivity

I just want to know if this is possible since first. I have created a custom listView based on the tutorial I read from Sai Geetha. Well it works perfectly on my app except that it needs to extend ListActivity instead of FragmentActivity. Now I'm having a hard time configuring and adding a dialog for this since I need to apply a fragment dialog and I can't use the getFragmentManager() since I'm not working with the FragmentActivity. Is there's another way I can do to work on this without sacrificing the ListActivity? Thanks!
Here's my code so far
XML:
conversation_list_view
<?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="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#id/android:list"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="20dp"/>
</LinearLayout>
group_screen
<?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="match_parent"
android:background="#color/white">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#drawable/action_bar_separator"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Group Name"
android:id="#+id/txt_group_name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#color/dark_gray"
android:shadowColor="#color/dark_shadow"
android:shadowRadius="1"
android:shadowDy="1"/>
<Button
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/btn_back"
android:background="#drawable/btn_navigate_back"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/btn_information"
android:background="#drawable/btn_information"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Conversations"
android:id="#+id/textView2"
android:textColor="#color/holo_light_blue"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="340dp"
>
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.mark.exercise.ListViewFragment"
android:id="#+id/fragment"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="42dp"
android:text="Ask something"
android:id="#+id/btn_ask_question"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
Java
package com.mark.exercise;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
/**
* Created by pc on 9/24/13.
*/
public class GroupActivity extends FragmentActivity {
Button information, back, new_topic;
ListView conversations;
TextView group_name;
String name, group_description, group_administrator,image_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_screen);
Intent intent = getIntent();
name = intent.getStringExtra("group_name");
group_description = intent.getStringExtra("group_description");
group_administrator = intent.getStringExtra("group_administrator");
image_id = intent.getStringExtra("image_id");
information = (Button)findViewById(R.id.btn_information);
back = (Button)findViewById(R.id.btn_back);
new_topic = (Button)findViewById(R.id.btn_ask_question);
group_name = (TextView)findViewById(R.id.txt_group_name);
group_name.setText(name);
information.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupActivity.this, GroupInformationActivity.class);
intent.putExtra("group_name",name);
intent.putExtra("group_description",group_description);
intent.putExtra("group_administrator",group_administrator);
intent.putExtra("image_id",image_id);
startActivity(intent);
GroupActivity.this.overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
});
new_topic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showCreateNewTopicDialog();
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
}
private void showCreateNewTopicDialog() {
FragmentManager fm = getSupportFragmentManager();
DialogFragmentCreateGroup createGroup = new DialogFragmentCreateGroup();
createGroup.show(fm, "create_group");
}
#Override
public void onBackPressed(){
super.onBackPressed();
overridePendingTransition(R.anim.in_from_right,R.anim.out_to_left);
}
}
List Fragment
package com.mark.exercise;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
/**
* Created by pc on 9/27/13.
*/
public class ListViewFragment extends ListFragment {
final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.conversations_list_view,
container, false);
setListView set_list = new setListView();
set_list.start();
return view;
}
public void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);
Intent intent = new Intent(getActivity(), ConversationActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
private class setListView extends Thread {
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
setConversations();
}
});
}
}
private void setConversations(){
list.clear();
SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
list,
R.layout.custom_list_main_conversations,
new String[] {"message","date", "reply_count", "stars_count"},
new int[] {R.id.txt_conversation_message,R.id.txt_topic_date, R.id.txt_no_of_reply, R.id.txt_no_of_stars}
);
for(int ctr=0;ctr<=5;ctr++){
Random randomGenerator = new Random();
HashMap<String,String> item_list = new HashMap<String,String>();
item_list.put("message", "This is the conversation number "+(ctr+1)+" and this topic is just a dummy data.");
item_list.put("date", "0"+(ctr+1)+"/0"+(ctr+2)+"/2013 "+(ctr+1)+":00:am");
item_list.put("reply_count", String.valueOf(ctr+randomGenerator.nextInt(10)));
item_list.put("stars_count", String.valueOf(ctr+randomGenerator.nextInt(10)));
list.add(item_list);
}
android.app.ListFragment lf = new android.app.ListFragment();
lf.setListAdapter(adapter);
}
}
You can use ListFragment inside FragmentActivity instead of using a ListActivity.

Listview not updating from fragment

When the user clicks a button, a new string is added to the list view, but nothing is appearing when i click it. How exactly would I do that?
MatchesList.java
The class that adds the strings to the list view
import java.util.ArrayList;
import com.actionbarsherlock.app.SherlockListActivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MatchesList extends SherlockListActivity{
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
int matchNum=0;
Button addMatch;
#Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.fragment_matches);
adapter= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
addMatch = (Button) findViewById(R.id.addMatchBtn);
addMatch.setOnClickListener(new OnClickListener(){
public void onClick(View v){
addMatch(v);
}
});
}
public void addMatch(View view){
matchNum += 1;
listItems.add("Match " + matchNum);
adapter.notifyDataSetChanged();
}
}
MatchesFragment.java
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.*;
public class MatchesFragment extends SherlockFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_matches, container, false);
}
}
fragment_matches.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MatchesFragment" >
<Button
android:id="#+id/addMatchBtn"
android:layout_width="50dp"
android:layout_height="40dp"
android:text="Add a Match"
/>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"></ListView>
</LinearLayout>
listAdapter.add(...) instead (and no need to call notifyDataSetChanged() then).
ArrayAdapter can use another list internally if you are using filter for example, besides not good practice as the ArrayAdapter could just do a defensive copy of that list passed in the constructor (it doesn't though). It is using an internal lock to protect that list though. So really isn't supposed to modify it outside.

Want to add item in a list in android at runtime

<?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"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/speed"
android:inputType="number"></EditText>"
<TextView
android:id="#+id/TextViewSpeed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="Speed"
android:layout_below="#+id/speed"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="AddValue"
android:id="#+id/AddValue"
>
</Button>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ListView"
android:layout_below="#+id/AddValue"
>
</ListView>
</RelativeLayout>
This is my layout code. I wanted to add a text data from EditText to the ListView which is on same page. How to write a code to add text to the list when I click on the AddValue Button.
Thanks in advance.
use this code
addbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String val = edittext.getText().toString();
list.add(val);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
}
});
Below snippet wil help you.
package org.sample;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
public class SampleActivity extends Activity
{
private Button add;
private EditText speedText;
private ArrayAdapter<String> adapter;
private ListView AddValue;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
AddValue=(ListView)findViewById(R.id.AddValue);
AddValue.setAdapter(adapter);
add = (Button) findViewById(R.id.add);
speedText = (EditText) findViewById(R.id.speed);
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
if (speedText.getText().toString().length() != 0)
{
adapter.add(speedText.getText().toString());
adapter.notifyDataSetChanged();
}
}
});
}
}

How do we create select (combo) box in android

I want integrate one combo filled with currency types. please tell me any way to create select box in android. i have created spinner but i have similar different views to show on page. and it is not looking attractive. please share any code for select.
use this dynamic_spinner_main.java
package example.sampleLocalization;
import java.util.ArrayList;
//import com.matthias.dynamicSpinnerT.R;
//import com.matthias.dynamicSpinnerT.R.id;
//import com.matthias.dynamicSpinnerT.R.layout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class dynamic_spinner_main extends Activity {
private Spinner m_myDynamicSpinner;
private EditText m_addItemText;
private ArrayAdapter<CharSequence> m_adapterForSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_spinner);
///////////////////////////////////////////////////////////////
//grab our UI elements so we can manipulate them (in the case of the Spinner)
// or add listeners to them (in the case of the buttons)
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
////////////////////////////////////////////////////////////////
//create an arrayAdapter an assign it to the spinner
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
m_adapterForSpinner.add("gr");
////////////////////////////////////////////////////////////////
//add listener for addButton
addButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
////////////////////////////////////////////////////////////////
//add listener for addButton
clearButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
private void addNewSpinnerItem() {
CharSequence textHolder = "" + m_addItemText.getText();
m_adapterForSpinner.add(textHolder);
}
private void clearSpinnerItems() {
m_adapterForSpinner.clear();
m_adapterForSpinner.add("dummy item");
}
}
main_spinner.xml
<?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">
<EditText android:layout_height="wrap_content"
android:layout_margin="4px"
android:id="#+id/newSpinnerItemText"
android:layout_width="fill_parent"></EditText>
<Button android:layout_height="wrap_content"
android:id="#+id/AddBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Add To Spinner"></Button>
<Button android:layout_height="wrap_content"
android:id="#+id/ClearBtn"
android:layout_margin="4px"
android:layout_width="fill_parent"
android:text="Clear Spinner Items"></Button>
<Spinner android:layout_height="wrap_content"
android:id="#+id/dynamicSpinner"
android:layout_margin="4px"
android:layout_width="fill_parent"></Spinner>
</LinearLayout>

Categories

Resources