How to do this list view

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to do this list view

Postby edwinkcw » Wed Feb 18, 2009 2:35 pm

I am implementing the following layout(in attachment)

I have already do the following xmls:

main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:layout_width="fill_parent" android:layout_height="fill_parent"
  7.  
  8.         android:orientation="vertical">
  9.  
  10.         <TextView android:layout_width="fill_parent"
  11.  
  12.                 android:layout_height="wrap_content" android:text="Please" />
  13.  
  14.         <ListView android:id="@+id/list" android:layout_width="fill_parent"
  15.  
  16.                 android:layout_height="wrap_content" />
  17.  
  18. </LinearLayout>
  19.  
  20.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


row.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:layout_width="fill_parent" android:layout_height="wrap_content"
  7.  
  8.         android:orientation="horizontal">
  9.  
  10.         <ImageView android:id="@+id/icon" android:layout_width="50px"
  11.  
  12.                 android:layout_height="50px" android:layout_centerHorizontal="true" />
  13.  
  14.         <TextView android:id="@+id/text1" android:textSize="16px"
  15.  
  16.                 android:textStyle="bold" android:layout_width="fill_parent"
  17.  
  18.                 android:layout_height="wrap_content"
  19.  
  20.                 android:gravity="center_vertical" />
  21.  
  22. </LinearLayout>
  23.  
  24.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Also, I have a data.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <data>
  3.  
  4.    <entry>
  5.  
  6.        <name>Abc</name>
  7.  
  8.        <image>http://www.comput.com/abc.jpg</image>
  9.  
  10.        <id>123</id>
  11.  
  12.   </entry>
  13.  
  14.    <entry>
  15.  
  16.        <name>Def</name>
  17.  
  18.        <image>http://www.comput.com/def.jpg</image>
  19.  
  20.        <id>321</id>
  21.  
  22.   </entry>
  23.  
  24. </data>
  25.  
  26.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


I know that I need to XML parser and already know how to parse an xml in android.
1) The problem is how to assign a web image to the list view.
2) The the item in list is click, how can I get the id. For example, when I click "Abc", I need the program return "123" for me
3) I already add android:gravity="center_vertical", why the text still align with top?

Thanks
Attachments
Screenshot.png
Screenshot.png (51.43 KiB) Viewed 432 times
edwinkcw
Developer
Developer
 
Posts: 38
Joined: Wed Feb 18, 2009 2:04 pm

Top

Postby dragoff » Thu Feb 19, 2009 9:11 am

1. com.example.android.apis.view -> List14.java
2. By clicking on ListItem, you cant get a View. Cast it to the ViewHolder (view.getTag()) and you can get all of you need from this Item (in your example is ImageView and TextView). So you can put to ViewHolder structure all additional information, e.g. Id.
3. Gravity top|center.
dragoff
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Feb 04, 2009 2:34 pm
Location: Odessa, Ukraine

Postby edwinkcw » Thu Feb 19, 2009 1:32 pm

I have tried the following code, but it throws an exception.
I am newbie of android programming, thanks


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.util.ArrayList;
  3.  
  4. import java.util.HashMap;
  5.  
  6. import java.util.List;
  7.  
  8. import java.util.Map;
  9.  
  10.  
  11.  
  12. import android.app.Activity;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.View;
  17.  
  18. import android.widget.AdapterView;
  19.  
  20. import android.widget.ImageView;
  21.  
  22. import android.widget.ListView;
  23.  
  24. import android.widget.SimpleAdapter;
  25.  
  26. import android.widget.TextView;
  27.  
  28. import android.widget.Toast;
  29.  
  30. import android.widget.AdapterView.OnItemClickListener;
  31.  
  32.  
  33.  
  34. public class PersonalVideoList extends Activity {
  35.  
  36.   ListView lv = null;
  37.  
  38.   OnItemClickListener listener = null;
  39.  
  40.  
  41.  
  42.   @Override
  43.  
  44.   protected void onCreate(Bundle icicle) {
  45.  
  46.     super.onCreate(icicle);
  47.  
  48.     setContentView(R.layout.main_option_list);
  49.  
  50.     lv = (ListView) findViewById(R.id.list);
  51.  
  52.  
  53.  
  54.     List<Map<String, Object>> list = buildListForSimpleAdapter();
  55.  
  56.  
  57.  
  58.     SimpleAdapter adapter = new SimpleAdapter(this, list,
  59.  
  60.         R.layout.main_option_list_row, new String[] { "name", "icon" },
  61.  
  62.         new int[] { R.id.text1, R.id.icon });
  63.  
  64.  
  65.  
  66.     listener = new AdapterView.OnItemClickListener() {
  67.  
  68.       public void onItemClick(AdapterView parent, View v, int position, long id) {
  69.  
  70.         ViewHolder holder;
  71.  
  72.         switch (position) {
  73.  
  74.         case 0:
  75.  
  76.           holder = (ViewHolder) v.getTag();
  77.  
  78.           Toast.makeText(PersonalVideoList.this, holder.text.getText(),
  79.  
  80.               Toast.LENGTH_SHORT).show();
  81.  
  82.           break;
  83.  
  84.         case 1:
  85.  
  86.           break;
  87.  
  88.         case 2:
  89.  
  90.           break;
  91.  
  92.         case 3:
  93.  
  94.           finish();
  95.  
  96.           break;
  97.  
  98.         }
  99.  
  100.         // Toast.makeText(MainOptionList.this, String.valueOf(position),
  101.  
  102.         // Toast.LENGTH_SHORT).show();
  103.  
  104.       }
  105.  
  106.     };
  107.  
  108.  
  109.  
  110.     lv.setAdapter(adapter);
  111.  
  112.     lv.setOnItemClickListener(listener);
  113.  
  114.   }
  115.  
  116.  
  117.  
  118.   static class ViewHolder {
  119.  
  120.     TextView text;
  121.  
  122.     ImageView icon;
  123.  
  124.   }
  125.  
  126.  
  127.  
  128.   private List<Map<String, Object>> buildListForSimpleAdapter() {
  129.  
  130.  
  131.  
  132.     List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(3);
  133.  
  134.  
  135.  
  136.     // Build a map for the attributes
  137.  
  138.     Map<String, Object> map = new HashMap<String, Object>();
  139.  
  140.     map.put("name", "Abc");
  141.  
  142.     map.put("icon", R.drawable.temp_icon1);
  143.  
  144.     list.add(map);
  145.  
  146.  
  147.  
  148.     map = new HashMap<String, Object>();
  149.  
  150.     map.put("name", "Def");
  151.  
  152.     map.put("icon", R.drawable.temp_icon2);
  153.  
  154.     list.add(map);
  155.  
  156.  
  157.  
  158.     return list;
  159.  
  160.   }
  161.  
  162. }
  163.  
  164.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
edwinkcw
Developer
Developer
 
Posts: 38
Joined: Wed Feb 18, 2009 2:04 pm

Postby dragoff » Thu Feb 19, 2009 2:34 pm

Do you have Android ApiDemos? As I said before, make your own Activity as List14.java. You can find it in com.example.android.apis.view.
dragoff
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Feb 04, 2009 2:34 pm
Location: Odessa, Ukraine

Postby edwinkcw » Fri Feb 20, 2009 9:40 am

Thanks for your helpful reply.
I have already read the example code.
Mine has some little different.

Here is my some code about the listener and the rest are similar to the example one.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.    listener = new AdapterView.OnItemClickListener() {
  4.  
  5.          
  6.  
  7.       public void onItemClick(AdapterView parent, View v, int position, long id) {
  8.  
  9.           ViewHolder holder;
  10.  
  11.           holder = (ViewHolder) parent.getSelectedView().getTag();
  12.  
  13.           Toast.makeText(MainOptionList.this, holder.text.getText(),
  14.  
  15.               Toast.LENGTH_SHORT).show();
  16.  
  17.           Toast.makeText(MainOptionList.this, "Hello",
  18.  
  19.               Toast.LENGTH_SHORT).show();
  20.  
  21.       }
  22.  
  23.     };
  24.  
  25.  
  26.  
  27.     lv.setAdapter(new EfficientAdapter(this));
  28.  
  29.     lv.setOnItemClickListener(listener);
  30.  
  31.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


However, it throws an exception again. Could anyone tell me why? Thanks
edwinkcw
Developer
Developer
 
Posts: 38
Joined: Wed Feb 18, 2009 2:04 pm

Postby dragoff » Fri Feb 20, 2009 10:31 am

In your previous post you cast View v (holder = (ViewHolder) v.getTag(); ), not AdapterView as now. Why do you try to get something that doesn't exist from AdapterView? Don't forget set tag for this view in your ListAdapter. Otherwise a NullPointErexeption will be raised again.
dragoff
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Feb 04, 2009 2:34 pm
Location: Odessa, Ukraine

Top

Postby edwinkcw » Fri Feb 20, 2009 12:56 pm

The reason I access AdapterView is that I have tried (ViewHolder)v.getTag() already, but the exception still throws. So I tried to make some trials.
edwinkcw
Developer
Developer
 
Posts: 38
Joined: Wed Feb 18, 2009 2:04 pm

Postby dragoff » Fri Feb 20, 2009 1:43 pm

If I make no mistakes, thats because you didn't set tag for this View in your extended class which overrides
public View getView(int position, View convertView, ViewGroup parent). (if you use your own class ListAdapter as extended from BaseAdapter). In this function, as in List14.java, you must set tag for convertView. This View will be returned to you in onItemClick listener. If you didn't call setTag(), (ViewHolder)v.getTag() throws an NullPointerEception.
If you want to keep your previous code, as in Thu Feb 19, 2009, probably you shouldn't use ViewHolder and try another way to cache ids or something else for your own purposes. You have List of Maps, so use it.
But as for me, your buildListForSimpleAdapter is unacceptable.
dragoff
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Feb 04, 2009 2:34 pm
Location: Odessa, Ukraine

Postby edwinkcw » Fri Feb 20, 2009 4:57 pm

Oh, I see. I am new to write such progam. In general, if writing a list, mostly programmer will use ListActivity instead of Activity and set the ListView, isn't it?
If then, I think I need to re-design my code.
edwinkcw
Developer
Developer
 
Posts: 38
Joined: Wed Feb 18, 2009 2:04 pm

Postby dragoff » Fri Feb 20, 2009 7:24 pm

:) sorry)) just now clap eyes on "public class List14 extends ListActivity" )))) When I work with similar problem, my class extends LinearLayout, not ListActivity and nothing to say about any problems. Maybe in the future... ) I don't know for this moment.
What about your last comment, probably you should try)
dragoff
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Feb 04, 2009 2:34 pm
Location: Odessa, Ukraine

Postby dragoff » Fri Feb 20, 2009 7:37 pm

TabActivity is Activity that contains TabHost. ListActivity is Activity that contains ListView and ListAdapter. You can use both of them. When you use (..)Activity its sometimes really helpful because they hide setups of TabHost, ListView etc. from you.
dragoff
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Feb 04, 2009 2:34 pm
Location: Odessa, Ukraine

Postby edwinkcw » Sat Feb 21, 2009 7:01 am

Thanks for your help. Now I can make such list view right now.
I think I don't understand the use of adapter well, is there any tutorial can teach me well and clear my misunderstanding.
Also, this maybe a silly question. I extend to ListActivity and the java code will find the id:list. However, what if I have two listview in the layout. How can I implement the java code?
edwinkcw
Developer
Developer
 
Posts: 38
Joined: Wed Feb 18, 2009 2:04 pm

Postby edwinkcw » Sat Feb 21, 2009 7:19 am

I also find that I actually don't need to add the id in the view.
When I finsh parsing the xml, I should put the id into an array, then when the item is on click. i can using the position to get back the id.
edwinkcw
Developer
Developer
 
Posts: 38
Joined: Wed Feb 18, 2009 2:04 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests