0

My problem is about loading data from csv file into list view by array adapter. When i fill data from SMS Inbox it works perfectly, but when I read them from csv file, first item appears, and then after few seconds appears other elements without symbol of progress bar.

public class SMSReader extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
(...)

            switch (loadCSVPrefs()) {
    case 0: {
        threadOfFillingsDataBySMSInbox.start();
        setCSVPrefs(1);
    }
        break;
    case 1: {
        threadOfFillingsDataByCSVFile.start();
    }
    default:
        threadOfFillingsDataBySMSInbox.start();
    }

}

Thread threadOfFillingsDataBySMSInbox = new Thread(new Runnable() {

    public void run() {

        final ArrayList<UserRecord> users = new ArrayList<UserRecord>();


        //(read from SMS INBOX CODE)(...)

                UserRecord user1 = new UserRecord(name, smsMessage,
                        geocodePosition(latGPS, longGPS));
                users.add(user1);



        generateCsvFile(csvFilePath, stringtocsv);

                    LV.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View child,
                    int position, long arg3) {

                // (Sets values of read SMS)(...)

                setValuesFromSMS(nameFromSMS, textFromSMS,
                        localizationFromSMS);



            }
        });
        runOnUiThread(new Runnable() { // Run on the UI Thread to set
                                        // the adapter to the list

            public void run() {

                LV.setAdapter(new UserItemAdapter(getApplicationContext(),
                        android.R.layout.simple_list_item_1, users));
            }
        });

    }
});

Thread threadOfFillingsDataByCSVFile = new Thread(new Runnable() {

    public void run() {

        String cos2 = readCsvFile(csvFilePath);


        final ArrayList<UserRecord> users = new ArrayList<UserRecord>();

        // (read from csv File)

        UserRecord user1 = new UserRecord(nameFromCSV, messageFromCSV,
                geocodePosition(latGPS, longGPS));
        users.add(user1);



        LV.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View child,
                    int position, long arg3) {

                UserRecord user = users.get(position);

                String nameFromCSVFile = user.username;
                String textFromCSVFile = user.message;
                String localizationFromCSVFile = user.address;

                setValuesFromSMS(nameFromCSVFile, textFromCSVFile,
                        localizationFromCSVFile);



            }
        });
        runOnUiThread(new Runnable() { // Run on the UI Thread to set
                                        // the adapter to the list

            public void run() {

                LV.setAdapter(new UserItemAdapter(getApplicationContext(),
                        android.R.layout.simple_list_item_1, users));
            }
        });

    }
});

public class UserItemAdapter extends ArrayAdapter<UserRecord> {
    private ArrayList<UserRecord> users;

    public UserItemAdapter(Context context, int textViewResourceId,
            ArrayList<UserRecord> users) {
        super(context, textViewResourceId, users);
        this.users = users;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (v == null) {
            v = vi.inflate(R.layout.listitem, null);
        }

        UserRecord user = users.get(position);
        if (user != null) {
            TextView username = (TextView) v.findViewById(R.id.smsFrom);
            TextView localization = (TextView) v
                    .findViewById(R.id.localization);
            TextView userMessage = (TextView) v
                    .findViewById(R.id.userMessage);

            if (username != null) {
                username.setText(" " + user.username);
            }

            if (localization != null) {
                localization.setText(user.address);
            }

            if (userMessage != null) {
                userMessage.setText(user.message);
            }

        }
        return v;
    }
}
(...)

And layouts:

1)smslist.xml with listview and progress bar

and

2)listitem.xml with textviews

Could you help find the soltion of this problem, please?

dudeck
  • 393
  • 5
  • 13

1 Answers1

0

I found solution. First, I have to delete default option from switch-case clause. It runs both threads. Secondly I have to put adding user record from users ArrayList into for loop to read all records (now i read only one-last).

dudeck
  • 393
  • 5
  • 13