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

可紊 提交于 2019-12-10 21:54:15

问题


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


回答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();
                }
            }
        });



回答2:


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!




回答3:


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.



回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/18308228/set-value-and-get-the-values-of-all-the-items-in-the-listview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!