我的问题是如何通过数组适配器将数据从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)带有listview和进度条的smslist.xml
和
2)带有textviews的listitem.xml
你能帮忙找到这个问题的解决方法吗?
答案 0 :(得分:0)
我找到了解决方案。 首先,我必须从switch-case子句中删除默认选项。它运行两个线程。 其次,我必须将用户ArrayList中的用户记录添加到for循环中以读取所有记录(现在我只读了一个)。