2

I have a class that extends Activity and inflates another xml layout in the main layout.

Example:

public class PrivateHistory extends Activity{

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.main_layout);  

    viewBundleMessage();

    LinearLayout item = (LinearLayout)findViewById(R.id.menu_linear_layout);
    View child = getLayoutInflater().inflate(R.layout.menu_list, null);
    item.addView(child);

    getHistoryInfo();

    }
}

Example (menu_list layout):

<?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">

<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listViewMenu">

</ListView>

And there is a getHistoryInfo method that I'm using to retrieve some history information from the db so I can put every record in a ListItem.

Example:

public void getHistoryInfo(){

//removed historyItem + database information

    ListView lv = (ListView) findViewById(R.id.listViewMenu);
    lv.setAdapter(new ArrayAdapter<String>(this,R.layout.history_list_view, historyItem));

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            showToast(historyItem.get(position).toString());
        }
    });

}

Example (history_list_view):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/noteText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="14dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="@drawable/bg_white_grey"
android:gravity="center_vertical"
android:typeface="serif"
android:layout_marginBottom="10dp"/>

So everything here works, but only if you want to add text into every ListItem. I would like to add images to every ListItems, but how? If this class had extended a ListActivity it wouldn't be that hard, but it extends an Activity. So can anyone help me here?

For anyone that would like to use my app, please go to: http://www.4shared.com/android/tWfbNqZ6/Free_Wallet.html

Aerial
  • 1,185
  • 4
  • 20
  • 42

4 Answers4

1

To have customized rows for the list (i.e. anything other than a textview) you should create your own adapter-implementation. Here is another answer (by me) explaining the general principle:
Two views in each list item (the example is about two textviews rather than an image, but just replace the layout with two textview for a layout with an imageview and change the corresponding lines in the getView() method)

Community
  • 1
  • 1
Jave
  • 31,598
  • 14
  • 77
  • 90
0

You can define a custom BaseAdapter where in the getView() method you can inflate your custom View for a list item. For more information you can check here: http://developer.android.com/reference/android/widget/BaseAdapter.html or here: http://thinkandroid.wordpress.com/2010/01/13/custom-baseadapters/

Hope this helps for now! In case you have any specific problem, please shoot it!

Dimitris Makris
  • 5,183
  • 2
  • 34
  • 54
0

You have write a custom adapter for the list view. Do one thing read something about BASE ADAPTER.

Here is the link

Rocker
  • 669
  • 5
  • 15
0

If you want the image to depend on the historyItem, you must use your own adapter implementation as outlined by Jave.

However, if you simply want a bullet or similar in the list item, you can change the history_list_view to a more complex layout including different Layout objects, ImageViews and the TextView which is mandatory when using ArrayAdapter. With the enhanced history_list_view, you can use ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects), which you will call with

lv.setAdapter(new ArrayAdapter<String>(this,R.layout.history_list_view,
    R.id.noteText, historyItem));
David Burström
  • 1,592
  • 14
  • 14
  • Well, I tried to change the history_list_view.xml file by adding the TextView and ImageView in a RelativeLayout and got error message (didn't take the time to read the error log). Maybe if you write a small example, it will work, idk... – Aerial Dec 15 '11 at 15:50