user2545733
user2545733

Reputation:

Set value and Get the values of all the items in the listview Android

I have a ListView which is :

<ListView
        android:id="@+id/list"
        android:layout_width="290dp"
        android:layout_height="166dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:visibility="gone"
        android:choiceMode="singleChoice" />

And there is ImageView

<ImageView
        android:id="@+id/cont"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/img" />  

How I am adding data in my listview.

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, list);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {


                }
            });

I am trying to update the listview value :

View v=listView.getChildAt(position);
                    TextView tv= (TextView) v.findViewById(android.R.id.text1);
                    tv.setText(firstNameDialog.getText().toString().trim().toString()+" "+lastNameDialog.getText().toString().trim().toString());

But this is not changing the value

I want that when the image view is clicked then I can get the values of the listview item to get stored in the form of array. How could i get all the items values of a listview and how could set the values again on the clicked item

Upvotes: 3

Views: 13173

Answers (5)

Pouya Danesh
Pouya Danesh

Reputation: 1627

for changing values of a list item you should do it in the adapter class. so if your item has a TextViewyou should write a method in adapter like this

public void changeText(String newValue) {
this.myTextView.setText(newValue);
}

this is also the case when using view holder. for getting all the items values, basically you should do what first answer said.

imageView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { int count = listView.getAdapter().getCount(); String[] listData = new String[count]; for (int i = 0; i < count; i++) { listData[i] = listView.getAdapter().getItem(i).toString(); } } }); but except getCount you could use size(). but at least for me these relations between adapter and list is a little confusing. hope it helps.

Upvotes: 0

Meher
Meher

Reputation: 318

int wantedPosition = position; // Whatever position you're looking for
int firstPosition = mList.getFirstVisiblePosition() -mList.getHeaderViewsCount();

int wantedChild = wantedPosition - firstPosition;
if (wantedChild < 0 || wantedChild >= mList.getChildCount()) {
    return;
}

Button btnEdit = (Button)wantedView.findViewById(R.id.btnEdit);
btnEdit.setText("my new message");

Have you tried something like this

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33248

imageView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                int count = listView.getAdapter().getCount();
                String[] listData = new String[count];
                for (int i = 0; i < count; i++) {
                    listData[i] = listView.getAdapter().getItem(i).toString();
                }
            }
        });

Upvotes: 1

user2558337
user2558337

Reputation:

Use the adapter. In cycle get the values from adapter :

StringBuilder b = new StringBuilder();
for(int i=0;i>adapter.getCount();i++)
    b.append(adapter.getItem(i));//May be toString() should be used.

Upvotes: 0

kittu88
kittu88

Reputation: 2461

The process is quite simple, what you can do is, on clicking the image view, iterate through the array adapter which you are using to populate the listview. while iterating you can declare an array and keep inserting the iterated values inside it. Hope it helps!

Upvotes: 0

Related Questions