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

5

3

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

Gosse answered 19/8, 2013 at 7:11 Comment(4)
simply use ImageView onClick and you have already ListView data in list.Tulley
actually after the listview is getting set by adapter values .then i am setting up new data on click on each item.Or i can say that i am changing the data of the items on clickGosse
on click of the item i am opening a custom dialog .in that there is a edit text in that the user is entering a name.them i am setting that name on the item clicked by the user from the listviewGosse
May be you store your data anywhere after changing. so use that..Tulley
T
1
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();
                }
            }
        });
Tulley answered 19/8, 2013 at 7:27 Comment(5)
how about setting the item values i have the positionGosse
@User not getting you...now what an issue?Tulley
i am asking if i have the postion of the item .den how could i settext on that item of the listviewGosse
you are using list for display data so change in list like as list[posistion]="your value" and set adapater.notifyDataSetChanged()Tulley
if i am doing this it is showin this error "The type of the expression must be an array type but it resolved to ListView"Gosse
G
0

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!

Gee answered 19/8, 2013 at 7:19 Comment(0)
H
0
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

Heer answered 19/8, 2013 at 7:19 Comment(0)
E
0

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.
Erumpent answered 19/8, 2013 at 7:19 Comment(2)
This should be i < adapter.getCount();. As it currently stands, this is an infinite loop.Jaddo
It's not an infinite loop - it will simply never reach the append statement because the i integer (starting at 0) will never be greater than adapter.getCount() unless the adapter somehow had a count of -1.Theoretical
M
0

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.

Morning answered 26/4, 2015 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.