0

我的问题是关于通过数组适配器将数据从 csv 文件加载到列表视图中。当我从 SMS 收件箱填充数据时,它工作得很好,但是当我从 csv 文件中读取它们时,出现第一个项目,然后几秒钟后出现其他没有进度条符号的元素。

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

和布局:

1) 带有列表视图和进度条的 smslist.xml

2)listitem.xml 与 textviews

你能帮忙找到这个问题的解决方案吗?

4

1 回答 1

0

我找到了解决方案。首先,我必须从 switch-case 子句中删除默认选项。它运行两个线程。其次,我必须将用户 ArrayList 中的用户记录添加到 for 循环中以读取所有记录(现在我只读取最后一个)。

于 2013-07-24T09:09:03.210 回答