Using few ListView's in one activity and layout - android

I'm trying to organize data recieved by SMS messages in specified ListView's.
I tried to create and activity which will contain 3 ListView's in one layout.
But, while running the activity it crashes.
Can someone help with this?
Here is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="#+id/idList"
android:layout_width="112dp"
android:layout_height="384dp"
android:layout_gravity="center" >
</ListView>
<ListView
android:id="#+id/namesList"
android:layout_width="96dp"
android:layout_height="387dp"
android:layout_gravity="center" >
</ListView>
<ListView
android:id="#+id/phonesList"
android:layout_width="wrap_content"
android:layout_height="382dp"
android:layout_gravity="center" >
</ListView>
</LinearLayout>
And here is the activity code:
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DataLists extends ListActivity implements OnClickListener {
ListView idList, namesList, phonesList;
MyReciever mr;
ArrayList<String>ids= new ArrayList<String>();
ArrayList<String>names=new ArrayList<String>();
ArrayList<String>phones=new ArrayList<String>();
ArrayAdapter<String> idAdapter, namesAdapter, phonesAdapter;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.details_lists);
idList=(ListView)findViewById(R.id.idList);
namesList=(ListView)findViewById(R.id.namesList);
phonesList=(ListView)findViewById(R.id.phonesList);
idAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ids );
idList.setAdapter(idAdapter);
namesAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
namesList.setAdapter(namesAdapter);
phonesAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, phones);
phonesList.setAdapter(phonesAdapter);
}
public void addItemToIdList(String st)
{
ids.add(st);
idAdapter.notifyDataSetChanged();
}
public void addItemToNamesList(String st)
{
names.add(st);
namesAdapter.notifyDataSetChanged();
}
public void addItemToPhonesList(String st)
{
phones.add(st);
phonesAdapter.notifyDataSetChanged();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
private class MyReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle=intent.getExtras();
SmsMessage[]msgs=null;
if(bundle!=null)
{
Object[]pdus=(Object[]) bundle.get("pdus");
msgs=new SmsMessage[pdus.length];
for(int i=0;i<msgs.length;i++)
{
int index=0, prev=0;
String msgBody=msgs[i].getMessageBody().toString();
index=msgBody.indexOf(';');
prev=index;
String name=msgBody.substring(0, index);
addItemToNamesList(name);
msgBody=msgBody.substring(index+1);
index=msgBody.indexOf(';');
String id=msgBody.substring(prev, index);
addItemToIdList(id);
msgBody=msgBody.substring(index+1);
String phone=msgBody;
addItemToPhonesList(phone);
}
}
}
}
}

You have extended ListActivity. This is a convenience class that can be used to display a single ListView. The crash is clearly described in your logcat/stacktrace:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
Since your layout contains 3 ListViews, you probably can't use ListActivity anyway. Just change your activity to extend Activity and go from there.

Related

Search keyword with values in arraylist in android

I am new to android development. I have created a search widget in ActionBar. Now I have to search my keyword with value stored in ArrayList. How can I get value of that searched keyword in a variable? Please give me some sample code for that.
You have to set an OnQueryTextListener to your searchView. Like this:
//listener to when the user changes the text
_searchView.setOnQueryTextListener(new OnQueryTextListener() {
boolean _first = true;
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String str) {
//do you arraylist filtering here
ArrayList<String> arrayList = new ArrayList<String>(); //your arraylist, that you will filter
for(String arrayItem:arrayList){
if(arrayItem.contains(str)){
//add this item to your new list
}
}
return true;
}
});
Then you just have to refresh your listAdapter with the new filtered array.
Try below code snippet
actionbar_serch.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/action_header"
android:gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/edit_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Search"
android:paddingLeft="15dip"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textCursorDrawable="#null" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.demosearch;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
Button btn_search;
ArrayList<String> item_array;
EditText edit_search;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
item_array=new ArrayList<String>();
for(int i=0;i<10;i++)
{
item_array.add("item"+i);
}
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_serch);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
btn_search=(Button)findViewById(R.id.btn_search);
edit_search = (EditText) findViewById(R.id.edit_search);
edit_search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String item=s.toString();
for(int i=0;i<item_array.size();i++)
{
if(item.equals(item_array.get(i)))
{
Log.e("Search item position is=", ""+i);
break;
}
}
}
});
}
}
Hope this helps you!!!!!
If it is not working please let me know i will try to help you more.

New Java class doesn't work. What am I doing wrong?

I just wanted to click on a button to see a text. Do I Need to import the public boolean onCreateOptionsMenu(Menu menu)? Or what am I doing wrong? I tried to Import some things, but it didn't help. Java-Code:
package com.example.tsd453;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Blume extends Activity implements OnClickListener {
public Button btn;
public TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.blume);
btn = (Button)findViewById(R.id.BtnKlick);
tw = (TextView)findViewById(R.id.Text);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw.setText("Hallo");
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" >
<TextView
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="70dp"
android:layout_marginTop="50dp"
android:textSize="70sp" />
<Button
android:id="#+id/BtnKlick"
android:layout_width="100dp"
android:layout_height="70dp"
android:layout_centerInParent="true"
android:layout_marginTop="31dp"
android:text="Button" />
</RelativeLayout>
you could try this:
instead of
btn.setOnClickListener(this);
and
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw.setText("Hallo");
try this in your onCreate:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tw.setText("Hallo, Welt!");
}
});
and you don't need to "implements onClickListener" in class blume
and e.g. use android:layout_below="#id/tw" in the xml file of the button to set the button below the textview
EDIT:
i just tried it on my nexus 5, and this code (i think it is the same like yours) is working
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
public Button btn;
public TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.BtnKlick);
tw = (TextView)findViewById(R.id.Text);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
tw.setText("Hallo");
}
}
i removed the options menu, but it also worked with it.
screenshot:
https://copy.com/3TX47IAK9gya
screenshot of the code in android studio:
https://copy.com/hbwnH0vZkzMn
It seems that the Button is hiding the TextView, try setting margin like:
<TextView
android:layout_marginTop="50dp" />
<Button
android:layout_marginTop="150dp"
/>
and remove center align (at least for one of them).

Make individual onClick() methods for children of expandablelistview

I have an expandablelistview as shown below. I've made it so that a toast message will pop up when each child is clicked. I need each of the children to start their own activity/fragment, which required individual onClick() methods. Does anybody know how this can be achieved? Thanks. NOTE: I am using SimonVT's slidingmenu library and I'm pretty new to android programming.
MainActivity.java:
package press.linx.expandablelistdemo;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.xml.sax.SAXException;
import net.simonvt.menudrawer.MenuDrawer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
ExpandableListView exv;
MenuDrawer mDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDrawer = MenuDrawer.attach(this);
mDrawer.setContentView(R.layout.activity_main);
mDrawer.setMenuView(R.layout.leftmenu);
exv=(ExpandableListView)findViewById(R.id.expandableListView1);
exv.setAdapter(new MyAdapter(this));
exv.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
String itemclicked = MyAdapter.childList[groupPosition][childPosition];
Toast.makeText(getApplicationContext(), "you clicked " + itemclicked, Toast.LENGTH_SHORT).show();
return false;
}
});
}
private void setListAdapter(SimpleAdapter adapter) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyAdapter.java
package press.linx.expandablelistdemo;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
Typeface typeface;
String []parentList = {"Tech", "Best Of", "Art & Design", "Other"};
static String [][] childList = {
{
"All Tech", "Reviews", "Gaming", "Gadgets"
},
{
"Android"
},
{
"Architecture"
},
{
"Infographics"
}
};
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}
#Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
//typeface = Typeface.createFromAsset(context.getAssets(), "font/robotochild.ttf");
TextView tv = new TextView(context);
tv.setText(childList[groupPosition][childPosition]);
tv.setPadding(30, 10, 0, 10);
tv.setTextSize(15);
//tv.setTypeface(typeface);
tv.setTextColor(Color.WHITE);
return tv;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childList[groupPosition].length;
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return parentList.length;
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv = new TextView(context);
typeface = Typeface.createFromAsset(context.getAssets(), "font/roboto.ttf");
tv.setText(parentList[groupPosition]);
tv.setPadding(50, 10, 0, 10);
tv.setTextSize(20);
tv.setTextColor(Color.WHITE);
tv.setTypeface(typeface);
return tv;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
menulistview.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=".MainActivity"
android:background="#drawable/geowall">
<ExpandableListView
android:id="#+id/expandableListView1"
android:groupIndicator="#null"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="3dp" >
</ExpandableListView>
!
If you want differing functionalities per button, consider storing them as the buttons' tag property (in the XML layout). The View v parameter in your onChildClick listener is the View of the child being clicked; it can then be used to retrieve the tag, like so:
v.getTag();
You could either have a switch-case block to call the correct activity based on the tag, or store the exact name of the activity in the tag and pass that in via reflection to retrieve the class for the activity. Alternatively, you could store a HashMap mapping the tag-names to Activity classes/variables.
This might be you are looking for.
Let’s get start by creating a project in Eclipse IDE.
Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as AndroidListViewActivity.
Once the project is created open your main activity java file (in this case AndroidListViewActivity.java) and extend the class from ListActivity.
public class AndroidListViewActivity extends ListActivity {
3. Now we need a string resources file to store all list item labels. So create an XML file under values folder and name it as list_data.xml and paste the following code.
( Right Click on res/values ⇒ New ⇒ Android XML File)
list_data.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="adobe_products">
<item>Adobe After Effects</item>
<item>Adobe Bridge</item>
<item>Adobe Dreamweaver</item>
<item>Adobe Edge</item>
<item>Adobe Fireworks</item>
<item>Adobe Flash</item>
<item>Adobe Photoshop</item>
<item>Adobe Premiere</item>
<item>Adobe Reader</item>
<item>Adobe Illustrator</item>
</string-array>
</resources>
In ListView each list item will be an xml layout, so we can customize each list item. Create an XML file under res/layout folder and name it as list_item.xml and type the following code. This xml layout will be single list item row.
( Right Click on res/layout ⇒ New ⇒ Android XML File)
Now open your main activity java file (AndroidListViewActivity.java) and type the following code. In the following code i am importing all xml resources data and storing them in an Array. On the next step i am binding array to ListAdapter.
AndroidListViewActivity.java
package com.androidhive.androidlistview;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class AndroidListViewActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, adobe_products));
}
}
Now run your project you can see listview with list of array items. But on clicking single list item you can see no action. So we need to start new activity on selecting single list item.
Launching new Activity on selecting single list item
In my previous article i had explained how to switch between screens. Here i am going to show single list item details in new screen.
Now create new activity class under src folder. Right Click on src/package folder ⇒ New ⇒ Class and name it as SingleListItem. (SingleListItem.java)
Open your AndroidListViewActivity.java and modify the code to following. In the following code i am getting the selected list item string(product name) and sending it to new Activity.
AndroidListViewActivity.java
package com.androidhive.androidlistview;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class AndroidListViewActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, adobe_products));
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) {
// selected item
String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
}
}
Now in new activity we need to display the received from listview activity.
Create a new xml file under res/layout and name it as single_list_item_view.xml and type the following code. This XML file will be layout for SingleListItem.java
single_list_item_view.xml
<?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">
<TextView android:id="#+id/product_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:padding="10dip"
android:textColor="#ffffff"/>
</LinearLayout>
Now open your second activity file i.e SingleListItem.java and paste the following code.
SingleListItem.java
package com.androidhive.androidlistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SingleListItem extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_list_item_view);
TextView txtProduct = (TextView) findViewById(R.id.product_label);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);
}
}
The final step is to add an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.androidlistview"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AndroidListViewActivity"
android:label="Android List View">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SingleListItem"
android:label="Single Item Selected"></activity>
</application>
</manifest>
Finally run your project by right clicking on your project folder ⇒ Run As ⇒ 1 Android Application.

How to link listView to another class when list item is selected?

Hi I am trying to make an app that shows a list of items using listView and when the user select one of the items from the list. the app will call the specific class that is link to the item selected however i encountered an error at the second #override, saying that the override must override a super class. Here are my codes for class and 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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/botanicgate" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
package com.fyp.gulliver;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class HotSpot extends ListActivity{
/** Called when the activity is first created. */
String places[] = {"BotanicGarden", "Sentosa"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotspot);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter
(HotSpot.this, android.R.layout.simple_list_item_1,
places));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Class ourClass;
String item = ((TextView)view).getText().toString();
try {
ourClass = Class.forName
("com.fyp.gulliver." + item);
Intent ourIntent = new Intent(HotSpot.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
As i am still new to android, thus i do not know what mistakes i have made, I will be grateful if another one can help me to solve my error thank you :D
Try changing this:
listView.setOnItemClickListener(new OnItemClickListener();
to
listView.setOnItemClickListener(new AdapterView.OnItemClickListener();
and remove the 2nd #Override

buttons in listview help?

I need to put button into list view to go to another view.
my button: AddTime = (Button) findViewById(R.id.Add_Time);
i tryd to get it working using:
public class myclass extends ListActivity implements OnClickListener {
and i have
public void onClick(View arg0) {
}
and i need it to go to another place i think using this
Intent intent = new Intent(this, myclass.class);
startActivity(intent);
how to i get this all working in listview?
use this
package example.buttonWithList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class buttonWithList extends ListActivity {
/** Called when the activity is first created. */
String[] items={"azhar","j"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
// TODO Auto-generated constructor stub
this.cntx=context;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return items.length;
}
#Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return items[position];
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return items.length;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
// TODO Auto-generated method stub
/*if(convertView==null)
{
}*/
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.row, null);
TextView tv=(TextView)row.findViewById(R.id.txtRow);
Button btn=(Button)row.findViewById(R.id.btnRow);
tv.setText(items[position]);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
onListItemClick(this, this, position, null);
}
});
return row;
}
protected void onListItemClick(OnClickListener onClickListener,
OnClickListener onClickListener2, int position, Object object) {
// TODO Auto-generated method stub
//Toast.makeText(this, items[position], 3000);
System.out.println(items[position]);
}
}
}
main.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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<ListView android:id="#android:id/list" android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
row.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">
<TextView android:text="#+id/TextView01" android:id="#+id/txtRow"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:id="#+id/btnRow" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Click "></Button>
</LinearLayout>
I suggest you to use a list view with strings. Make a buttons array and when you click on one, via onClick get the button in the array with the position parameter

Categories

Resources