Jaro Kollár
Jaro Kollár

Reputation: 263

android - custom list view with one image at top

I have own custom list view, which has in one row TextView with EditText. Now I want to have one image above this list view which change image according to selected EditText in list view. So for example when user clicks on EditText in second row, the top image above the list view will change to img2. When user clisks on EditText in first row, it will be img1 and so on.

I have problem that I don't know how can I find out which EditText was selected in ListView.

Thanks

Upvotes: 0

Views: 831

Answers (3)

Ram Mandal
Ram Mandal

Reputation: 1959

You can add as many headers as you like by calling addHeaderView() multiple times. You have to do it before setting the adapter to the list view.

you can do this like this

LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.your_mage_layout, yourlistview, false);
yourlistview.addHeaderView(header, null, false);

and remember during onItemclick the position becomes

position=position-1

otherwise you gonna get error during onItem click since one of the row of listview is occupied by your custom header

Upvotes: 0

Gaurav Balbhadra
Gaurav Balbhadra

Reputation: 164

Your layout should be like below.

Demo.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_demo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

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

</LinearLayout>

Demo.java

public class Demo extends Activity {

    ArrayList<String> textViewList = new ArrayList<String>();
    ArrayList<String> imageList = new ArrayList<String>();
    ArrayList<String> editTextList = new ArrayList<String>();

    ListView list;
    ImageView ivDemo;

    ListDemoAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);

        list = (ListView) findViewById(R.id.list_demo);
        ivDemo = (ImageView) findViewById(R.id.iv_demo);    

        adapter = new ListDemoAdapter(this, textViewList, editTextList, imageList);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Picasso.with(this).load(imageList.get(i)).into(ivDemo);
            }
        });
    }
}

here you get the position of row which is clicked by user for edit text. using this position you can easily load image from that position in Arraylist and display it in Imageview which is above ListView.

Upvotes: 0

Eric B.
Eric B.

Reputation: 4702

You could change your layout like this:

<LinearLayout orientation="vertical">
   <ImageView/>
   <ListView/>
</LinearLayout>

Now in your java code, you can pass this ImageView as a parameter to your Adapter constructor. In the getView method, you could set a click listener to the EditText and based on that you could change the image in the ImageView.

Upvotes: 1

Related Questions