I have a ListView that gets TutorialTitels from the string file like so
public class tutorialActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial);
registerClickCallBack();
ListView listView = (ListView) findViewById(R.id.tutorialList);
String tutorialTitle1 = getResources().getString(R.string.tutorial1_title);
String tutorialTitle2 = getResources().getString(R.string.tutorial2_title);
String tutorialTitle3 = getResources().getString(R.string.tutorial3_title);
String tutorialTitle4 = getResources().getString(R.string.tutorial4_title);
String tutorialTitle5 = getResources().getString(R.string.tutorial5_title);
String tutorialTitle6 = getResources().getString(R.string.tutorial6_title);
String tutorialTitle7 = getResources().getString(R.string.tutorial7_title);
String tutorialTitle8 = getResources().getString(R.string.tutorial8_title);
String tutorialTitle9 = getResources().getString(R.string.tutorial9_title);
String tutorialTitle10 = getResources().getString(R.string.tutorial10_title);
String tutorialTitle11 = getResources().getString(R.string.tutorial11_title);
String tutorialTitle12 = getResources().getString(R.string.tutorial12_title);
String tutorialTitle13 = getResources().getString(R.string.tutorial13_title);
String tutorialTitle14 = getResources().getString(R.string.tutorial14_title);
String[] values = new String[] { tutorialTitle1, tutorialTitle2, tutorialTitle3, tutorialTitle4, tutorialTitle5, tutorialTitle6, tutorialTitle7, tutorialTitle8, tutorialTitle9, tutorialTitle10, tutorialTitle11, tutorialTitle12, tutorialTitle13, tutorialTitle14};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
}
private void registerClickCallBack() {
ListView list = (ListView) findViewById(R.id.tutorialList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {
}
});
}
}
When I click on a listViewItem then I'd like to open up an Activity that'll show the following things:
The clicked TutorialTitel
Tutorial content (This will also come from a string file, like so
String tutorialContent1 = getResources().getString(R.string.tutorial1_content);)
Tutorial example ((This will also come from a string file, like so
String tutorialExample1 = getResources().getString(R.string.tutorial1_example);))
I already have an XML file like so
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#FFFFFF"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:id="#+id/tutorialTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textSize="25sp" />
<LinearLayout
android:id="#+id/split"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginBottom="15dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="5dp"
android:background="#38b34a"
android:orientation="horizontal" />
<TextView
android:id="#+id/tutorialContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text=""/>
<TextView
android:id="#+id/tutorialExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text=""/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
The question: How do I pass the data to my activity corresponding to my clicked ListviewItem? should I do something like
if(position == 0){
//Send data through extra bundle
}
else if(position == 1){
//send data through extra bundle
}
But there should be a better way I think, but I don't know how exactly, because what if I'll getting 100 tutorials how should I manage a list long like that?
Can someone point me in the correct direction and what is the best approach to do this?
You can add a listener to ListView using setOnItemClickListener(); this listener callback will give three parameter as onItemClick (AdapterView<?> parent, View view, int position, long id) in which position will give the position of the list item clicked.
so using this position you can use as values[position] and same for tutorialContent and tutorialExample and pass it in intent and start Activity with this intent having the selected item data
This is a Quick solution for your problem. But i will not recommend because the way you have implemented is not in the object oriented standard.
I recommend you to create a model class for Tutorial with having member variable title, content, and example.
Class Tutorial{
private String title;
private String content;
private string example;
}
and pass this object in the intent.
First keep your strings in an array file (array.xml). Then read them to an String array;
String [] myresources=resources.obtainTypedArray(R.array.somelist);
for(int i=0;i<NumberOfTutorials;i++){
if(position==i){
//send myresources[i];
}
}
You can use enum. For example you can define Tutorials enum like this:
public enum Tutorials
{
TUTORIAL_1(R.string.tutorial1_title, R.string.tutorial1_content, R.string.tutorial1_example),
TUTORIAL_2(R.string.tutorial2_title, R.string.tutorial2_content, R.string.tutorial2_example),
TUTORIAL_3(R.string.tutorial3_title, R.string.tutorial3_content, R.string.tutorial3_example);
private int mTitleResourceId;
private int mContentResourceId;
private int mExampleResourceId;
private Tutorials(int titleResourceId, int contentResourceId, int exampleResourceId)
{
mTitleResourceId = titleResourceId;
mContentResourceId = contentResourceId;
mExampleResourceId = exampleResourceId;
}
public int getTitleResourceId()
{
return mTitleResourceId;
}
public int getContentResourceId()
{
return mContentResourceId;
}
public int getExampleResourceId()
{
return mExampleResourceId;
}
}
In this enum you can define 100 tutorials if you wish.
And then in your activity you can use it like this in generic way:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial);
ListView listView = (ListView) findViewById(R.id.tutorialList);
List<String> titlesList = new ArrayList<String>();
for (Tutorials tutorial: Tutorials.values())
{
titlesList.add(getResources().getString(tutorial.getTitleResourceId()));
}
String[] values = titlesList.toArray(new String[titlesList.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
}
And finally on click item on the list you can easily retrieve the Tutorial enum by its string title and then you will have the tutorial content and and tutorial example for free.
Once you set this code, you will add tutorials only to Tutorials enum and that's it.
Hope it helps. Of course, using sqlite is also an option.
Related
I am trying to create ListView with custom data set as follows:
String superType = "random1";
String superTypea = "random12";
String superType12 = "random2";
String superType_amount = "child1";
String childtype_calulated = "2323";
String superType_amount = "child2";
String childtype_calulated = "23223";
String superType_amount = "child2";
String childtype_calulated = "amount3";
Now I want to create ListView with this set of data how to do that?
Here is the list structure...
row1=superType |superType_amount |childtype_calulated
row2=superTypea |superType_amount |childtype_calulated
row3=superType12|superType_amount |childtype_calulated
Is there any solution of this?
It is absolutely possible to do this. First, I would recommend putting your data into a collection. It would be preferable to put them into an object and then a collection of those objects. From there you can add a ListView to your main layout, define a custom layout for your list items, and populate your ListView using an ArrayAdapter.
Here is a really good example of how you can do this well. It includes examples of loading data from an external source, which you don't need.
However, if you're getting into development now I would suggest you look into RecyclerView as well. RecyclerView is new and included in the AppCompat v7 library for use on pre-Lollipop Android. A RecyclerView will be a little more complicated to implement for a simple list but is significantly more scalable and efficient. I believe it is Google's intention to replace ListView with RecyclerView entirely in the future.
Here is a pretty simple introduction to making a list with RecyclerView.
EDIT
Using an ArrayAdapter with a ListView. First you need to create a model to store your data, some kind of class that you can put into a collection, for example:
public class Item {
public String title;
public String sub1;
public String sub2;
public void Item(String t, String s1, String s2) {
title = t;
sub1 = s1;
sub2 = s2;
}
}
Then you need to define the layout for the item in your list:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/sub1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/sub2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Then in you need to make your custom ArrayAdapter by extending the ArrayAdapter class:
public class ItemAdapter extends ArrayAdapter<Item> {
public ItemAdapter(Context context, ArrayList<Item> items) {
super(context, 0, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Item item = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout, parent, false);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView sub1 = (TextView) convertView.findViewById(R.id.sub1);
TextView sub2 = (TextView) convertView.findViewById(R.id.sub2);
title.setText(item.title);
sub1.setText(item.sub1);
sub2.setText(item.sub2);
return convertView;
}
}
Then all you need to do is create an instance of the adapter in your main class and attach your collection to it:
ArrayList<Item> data = new ArrayList<Item>();
ItemAdapter adapter = new ItemAdapter(this, data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
This should populate your ListView with all the items that you need in your list. I haven't run any of this code so there might be one or two small bugs for you to fix.
I have a listview class.listview opening and a user can click any listview item. I wanna set the text on a xml. I have strings like st1-st2-st3. If I clicked any listview item, how can I get it to open the xml and set Textview1 for st1/#String ?
my listview working like this ;
public class strateji extends Activity {
private static final String LOG_TAG = "Alert Dialog Activity";
private ListView lv1;
private TextView status;
private String lv_arr[]={"listview1-list2-list3-list4-list5-list6-etc..."};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ana);
// Look up the AdView as a resource and load a request.
Log.i(LOG_TAG, "Alert Dialog Activity Started");
lv1=(ListView)findViewById(R.id.listView1);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
setContentView(R.layout.texts); //--->opening texts.xml and setText
status.setText(String);--------->>> like this i want send a string for TextView
}
if(position == 1)
{
Intent myIntent = new Intent(strateji.this, mat2.class);
startActivityForResult(myIntent, 0);
}
}
}
Ok this is getting a bit confusing so let me just do this.
If you have a layout like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:packpurchase="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text_view_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="17sp"
android:padding="15dp" />
<View
android:id="#+id/divider_one"
android:layout_width="match_parent"
android:layout_height="1dp" />
<ListView
android:id="#+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="#drawable/list_divider_inset"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
In your activity, create the variables in order to have a reference to your views and adapter:
private ListView lv;
private ArrayAdapter<String> mAdapter;
private TextView tv;
And then in onCreate() once you are done calling super.onCreate() and setContentView(), set your adapter to your ListView while getting your references
lv = (ListView) findViewById(android.R.id.list);
mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr);
lv1.setAdapter(mAdapter);
tv = (TextView) findViewById(R.id.your_textview_id);
lv1.setOnItemClickListener(new OnitemClickListener(){
#Override
public void onItemClicked(...){
// GET YOUR STRING
String mString = mAdapter.getItem(position);
// APPLY YOUR STRING TO YOUR TEXT_VIEW
tv.setText(mString);
}
};
Again this is poorly explained and I am not sure if it is correct to what you need. It is just to get you started (or at least to help us understand better what you need).
Note that you don't have to keep a reference to your adapter as you can just call
lv1.getAdapter().getItem(position);
I just keep a reference to it and often see a reference to the adapter kept. Same with TextView tv. You don't have to keep a reference, but you can and once you have that reference you can do null checks to avoid doing to many findViewById() calls which can get heavy.
If I understand you correctly, you want a new activity to open and a textview in that activity to display text thats dependent on what list view item they clicked?
you would add an intent extra into your intent that you are using to launch the new activity and put the string you want displayed into it, and then in the new activity get that extra and then set the text view to it.
Intent i = new Intent(activity1, activity2);
i.putExtra(stringkey, stringvalue);
then in your oncreate of the activity you call
String str = getIntent.getStringExtra(stringkey);
then just set the textview to that string
I KNOW this is a repost, but I've been trying to get this to work for ages (hours) now and I really can't understand the existing answers.
All i want to know is: How can I edit this code so it works? I'm trying to populate both textviews from two different arrays.
Only the second adapter gets read and the first one's textview stays blank.
Any help at all would be appreciated.
public void run () {
if (t.getState() == Thread.State.TERMINATED) {
adapter2 = new ArrayAdapter<String>(getApplicationContext(), R.layout.listlook, R.id.txtl2, names);
setListAdapter(adapter2);
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.listlook, R.id.txtl1, comments);
setListAdapter(adapter);
return;
} else {
h.postDelayed(this, 1000);
}
}}
, 1000);
}
Thanks in advance.
Listview within Usercomments.xml
<ListView
android:id="#+android:id/list"
android:layout_width="match_parent"
android:layout_height="150dp" >
</ListView>
listlook.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/txtl1"
android:paddingLeft="2dp"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#0000FF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/txtl2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtl1"
android:gravity="bottom|right"
android:paddingLeft="5dp"
android:text="TextView"
android:textColor="#5C002E"
android:textSize="15sp" />
</RelativeLayout>
listlook is just the layout i use in the listview? Don't really know what I'm doing.
You're telling your list view that you want to use adapter2, and then telling it you want to use adapter - it can only read data from one adapter at a time.
If you want to display data from two lists, you could do something like this.
First, combine your two lists into a class, and build a List<Post> of these, instead of having two arrays:
public class Post {
String mName, mComment;
public Post(String name, String comment) {
mName = name;
mComment = comment;
}
public String getName() {
return mName;
}
public String getComment() {
return mComment;
}
}
Then, write an adapter that knows how to display Post items:
public class PostAdapter extends BaseAdapter {
private Activity mActivity;
private List<Post> mPosts;
public TweetstreamAdapter(Activity activity, List<Post> posts) {
mActivity = activity;
mPosts = posts;
}
#Override
public int getCount() {
return mPosts.size();
}
#Override
public Post getItem(int position) {
return mPosts.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Post post = getItem(position);
if (convertView == null) {
convertView = mActivity.getLayoutInflater().inflate(R.layout.listlook, parent, false);
}
((TextView)convertView.findViewById(R.id.txtl1)).setText(post.getName());
((TextView)convertView.findViewById(R.id.txtl2)).setText(post.getComment());
return convertView;
}
}
Then in your activity, you set the adapter like this:
setListAdapter(new PostAdapter(this, posts));
where posts is a List<Post> of posts.
Note: You'll need to ensure you have TextView entries in your listlook layout that have IDs that match those that the adapter is searching for. Update the code to match your IDs accordingly. Updated to match your id's. This should just drop in, provided you construct a list of Post objects correctly.
Update 2: You could build a list like this, assuming the two arrays (names/comments) are the same length:
List<Post> posts = new ArrayList<Post>();
for(int i = 0; i < names.length; i++) {
posts.add(new Post(names[i], comments[i]);
}
You have two lines with setListAdapter(). The second overwrite the first. If you have two ListViews then do this:
listView1.setListAdapter(adapter2);
listView2.setListAdapter(adapter);
I am filling a listview from names of a database. That is all working fine. After clicking on an Listitem with the name i would like to set the onListItemClickto fill out textviews in another activity. That is where i am stuck.
Here the code of my listview activity which is working:
public class DataListView extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private SQLiteDatabase newDB;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotelslist);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
ListView hotelslist = (ListView) findViewById(android.R.id.list);
hotelslist.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getBaseContext(), l.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
private void openAndQueryDatabase() {
try {
DataBaseHelper newDB = new DataBaseHelper(this, "mydatabase.sqlite");
SQLiteDatabase sdb = newDB.getReadableDatabase();
Cursor c = sdb.rawQuery("SELECT name FROM hotel ORDER BY name ASC", null);
if (c != null ) {
if (c.moveToFirst()) {
int i = 0;
do {
i++;
String name = c.getString(c.getColumnIndex("name"));
results.add(name);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (newDB != null)
newDB.close();
}
}
}
Besides the name the table also contains columns "address" and "telephone". This data i would like to be filled in textviews "addressfill" and "telephonefill" of another activity with following 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/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Address" />
<TextView
android:id="#+id/addressfill"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/telephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Telephone number" />
<TextView
android:id="#+id/telephonefill"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I tried to read into many tutorials out there and many posts here but didnt get around to found the right answer.
What code would i have to use for the onListItemClick and in my activity that uses the xml I presented. Help is very appreciated!
If you want to set up listeners on list items, you will need to extend array adapter and put the listener code in the adapter:
public class SettingsListAdapter extends ArrayAdapter<String> {
#Override
public View getView(int position, View convertView, ViewGroup parent){
// put your listener in here
mybutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do work here
}
}
}
In other words, the simplelistadapter is not sufficient to do want you want, you will need to create your own.
edit
Here's a good example of creating your own custom ArrayAdapter: Custom Array Adapter Tutorial
In onListItemClick() create an Intent and put the name in the Intent:
Intent intent = new Intent(DataListView.this, SecondActivity.class);
intent.putExtra("name", l.getItemAtPosition(position).toString());
startActivity(intent);
You must change SecondActivity to the class name of the Activity that will receive the name.
In this second Activity's onCreate(), read the String:
String name = getIntent().getStringExtra("name");
if(name != null) {
// Do something with the name, like selecting the address
// and telephone number from your database to display
}
However since you are working with a database you really should use a SimpleCursorAdapter. Working with a Cursor is more useful and efficient.
how can i do it for the other columns data of the table like address and telephone?
You have two options:
In your second Activity use a query like:
"SELECT address, telephone FROM Hotel WHERE name = '" + name + "'"
But if multiple hotels have the same name, you will get more than one address...
Change your query in your first Activity to:
"SELECT name, address, telephone FROM Hotel ORDER BY name"
Now in onListItemClick() you can pass the name, address, and telephone number all at once, but you will need to store the address and telephone number somewhere... This is where a SimpleCursorAdapter is extremely useful.
I am designing a simple app from tutorials that I have seen, and I am attempting to simply display two text views in each row of a listview, a name and a price. This worked and I could select the row and it would activate an intent. However I then changed my xml code so that the listview would be placed in a linearLayout in order for me to have a header at the top of the screen. Now when I click on any of the rows they are highlighted but nothing else happens. I have already tried to making the textviews set to clickable = false in the xml but still no luck. I am hoping I am just missing something simple in the onCreate method. `public class ViewMenuListing extends ListActivity {
public static final String ROW_ID = "row_id"; // Intent extra key
private ListView contactListView; // the ListActivity's ListView
private CursorAdapter contactAdapter; // adapter for ListView
private String tableName;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contactListView = getListView(); // get the built-in ListView
contactListView.setOnItemClickListener(viewContactListener);
setContentView(R.layout.viewmenu);
//Get table name of menu clicked.
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");
// map each contact's name to a TextView in the ListView layout
String[] from = new String[] { "name", "price" };
int[] to = new int[] { R.id.itemTextView, R.id.priceTextView };
//int[] to = new int[] { R.id.itemTextView};
contactAdapter = new SimpleCursorAdapter(
ViewMenuListing.this, R.layout.menu_list_item, null, from, to);
setListAdapter(contactAdapter); // set contactView's adapter
}`
The only thing I changed in this code was that I used the setContentView(R.layout.viewmenu) now when I didnt before and the list would just be the content view.
Here is my viewMenu file:
`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>`
and my menu_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemTextView"
android:padding="8dp"
android:clickable = "false"
android:textSize="20sp" android:textColor="#android:color/white"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/priceTextView"
android:clickable = "false"
android:textColor="#android:color/white"/>
</LinearLayout>
Thank you for your help!
Looks like you are explicitly setting an onItemClickListener for your ListView. This really isn't necessary since you are extending ListActivity and ListActivity has a method you can override called onListItemClick(). I would override the onListItemClick() method instead of explicitly setting an onItemClickListener. http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView, android.view.View, int, long)
Please correct following things
contactListView = getListView(); and setListAdapter(contactAdapter);
The above syntax can be only used if your class is extending ListActivity .In your case you are extending Activity,So above Approach will not work.
Your menu_list_item.xml looks perfectly fine to me.
in viewmenu.xml file make following correction while specifying id for listview.
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Please see to this corrected code.
public class SampleActivity extends ListActivity
{
public static final String ROW_ID = "row_id"; // Intent extra key
private ListView contactListView; // the ListActivity's ListView
private CursorAdapter contactAdapter; // adapter for ListView
private String tableName;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
contactListView = (ListView) findViewById(R.id.list); // get the
// built-in
// ListView
contactListView.setOnItemClickListener(this);
setContentView(R.layout.viewmenu);
Bundle extras = getIntent().getExtras();
tableName = extras.getString("table");
// map each contact's name to a TextView in the ListView layout
String[] from = new String[]
{ "name", "price" };
int[] to = new int[]
{ R.id.itemTextView, R.id.priceTextView };
contactAdapter = new SimpleCursorAdapter(SampleActivity.this,
R.layout.menu_list_item, null, from, to);
contactListView.setAdapter(contactAdapter);
}
#Override
public void onListItemClick(AdapterView<?> adapterView, View view,
int position, long arg3)
{
}
}
Now in onListItemClick method you can write whatever logic you want.
Hope this help.