Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to add a button in a list View, I searched a lot on google but nothing was good enough for me.

Here's my code: I have 2 classes :

Menu.java

    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;

    public class Menu extends ListActivity implements OnItemClickListener {

String[] listaMeniu = { "1", "2", "3"};
Button butonNota;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ListAdapter(this, listaMeniu));

    ListView listView = getListView();
    listView.setOnItemClickListener (this);

    Button btnLoadMore = new Button(this);
    btnLoadMore.setText("show me");

}
}

Menu.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:padding="5dp">

    <ImageView
    android:id="@+id/1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
    android:src="@drawable/1" />

    <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize = "30dp"
    android:text="1" />


    </LinearLayout>

ListAdapter.java

     package com.example.a;

     import android.content.Context;
     import android.view.LayoutInflater;
     import android.view.View;
     import android.view.ViewGroup;
     import android.widget.ArrayAdapter;
     import android.widget.ImageView;
     import android.widget.TextView;

    public class ListAdapter extends ArrayAdapter {

private Context context;
private String[] values;

public ListAdapter(Context context, String[] values) {
    // TODO Auto-generated constructor stub

    super (context, R.layout.menu, values);
    this.context = context;
    this.values = values;
    }    

}

I already made the list view, but I don't know how to add the button above the list. I tried to add it in menu.xml but it's shows up a button for every item in the list. Hope you guys understand what I want. Thank you!

share|improve this question
1  
you have to use custom listview.. –  Erick Jun 12 '13 at 11:49
1  
learn base adapter.. instead of array adapter, –  Sandy09 Jun 12 '13 at 11:51
    
check this stackoverflow.com/questions/12637301/… –  sunil Jun 12 '13 at 11:54

3 Answers 3

The best way is to create your custom adapter. For example :

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.rowlayout, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    Button buttonView = (Button) rowView.findViewById(R.id.button);
    buttonView.setText(values[position]);

    return rowView;
  }
} 

And here is the xml from "rowlayout.xml". You have to put the layout file in the res/layout project folder.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px">
    </Button>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout> 

And then just update your Menu.java

public class Menu extends ListActivity implements OnItemClickListener {

    String[] listaMeniu = { "1", "2", "3"};
    Button butonNota;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new MySimpleArrayAdapter(this, listaMeniu));

        ...
    }
 }
share|improve this answer
    
Stil not working. I want something like this: | [picture] Item 1 [picture] Item 2 .... [picture] Item 9 [Button] | I made everything 'till the button and now i'm stuck. Can you explain me like for dummies :D ? Please! –  Dani Jun 12 '13 at 12:17

You need to override method getView in your adapter to return Button.

share|improve this answer

It appears that you want your 10th item in the list to be a button.

That means that when you overwrite your ArrayAdapter class, you need to modify GetView() so it returns the button instead of a picture. Create two different layout XML files, rowlayout_picture.xml and rowlayout_button.xml, and then:

public View getView(int position, View convertView, ViewGroup parent) 
{
     LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     View rowView = null;
     if (position < 10)
         rowView = inflater.inflate(R.layout.rowlayout_picture, parent, false);
     else
         rowView = inflater.inflate(R.layout.rowlayout_button, parent, false);

     return rowView;
 }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.