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

2
  • if you google you will get a lot. Commented Dec 15, 2011 at 14:58
  • see here they will post only links not more than you expected. Commented Dec 15, 2011 at 15:04

4 Answers 4

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)

Sign up to request clarification or add additional context in comments.

Comments

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!

2 Comments

Thanx, I'll take the time to understand how BaseAdapter works. I appreciate your and the others effort for posting helpful information here.
Your welcome! In case you need anything, we all be glad to help you!
0

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

Here is the link

Comments

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));

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.