How to Obtain a Reference to an Off-Screen Item in Android Listview

Asked 2 years ago, Updated 2 years ago, 86 views

Suppose there are 10 listView items on the screen and the 11th item is off the screen.I want to get all 11 pieces of data, but I don't know how to refer to the 11th piece of data.
If you execute the following code, you will get a null pointer exception when you try to get the 11th reference.

public void saveData(){
    StringBuffer buffer = new StringBuffer();
    int count = listView.getCount();
    for(inti=0;i<count;i++){
        Viewchild=listView.getChildAt(i);
        TextView setText1=(TextView)child.findViewById (R.id.TextView1);
        EditText=(EditText)child.findViewById(R.id.setText1);
        String content1 = setText1.getText().toString().trim();
        String content2 = text.getText().toString().trim();
        buffer.append(content1);
        buffer.append(",");
        buffer.append(content2);
        buffer.append(",");
    }
}
public class CustomAdapter extensions ArrayAdapter <String>{

    publicCustomAdapter(Context context, List<String>num_set){
        super(context, R.layout.custom_row, num_set);
    }

    @ Override
    publicView getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=LayoutInflater.from(getContext());
        View CustomView=inflater.inflate(R.layout.custom_row, parent, false);


        String[] num=getItem(position).split(",");
        TextView setText1=(TextView)CustomView.findViewById (R.id.TextView1);
        EditText setText2=(EditText)CustomView.findViewById (R.id.setText2);

        setText1.setText(num[0]);
        setText2.setText(num[1]);
        return CustomView;
    }
}

android

2022-09-30 21:16

1 Answers

Instead of retrieving data from ListView, use the Adapter.Assume you are using CustomAdapter.

public void saveData(){
    StringBuffer buffer = new StringBuffer();

    CustomAdapter=(CustomAdapter)listView.getAdapter();
    for (inti=0, length=adapter.getCount(); i<length;i++){
        String [ ] rowData=adapter.getItem(i).split(",");
        buffer.append(String.format("%s,%s,", rowData[0].trim(), rowData[1].trim()));
    }
}


2022-09-30 21:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.