我試圖在列表視圖中顯示來自收件箱的所有短信。更改列表滾動時查看內容
問題:
消息被正確填充在列表視圖中,當我開始練習。但是當我向上或向下滾動的一些內容消息中間和角落位置自動更改。我無法追查列表的這種不正常行爲的原因。您能否看到適配器類或代碼的其他部分有什麼問題。
代碼:
我使用下面的自定義適配器,以填補一個ListView。
import java.util.*;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ListViewAdapter extends BaseAdapter {
private String FIRST_COLUMN = "First";
private String SECOND_COLUMN = "Second";
private String THIRD_COLUMN = "Third";
private String FOURTH_COLUMN = "Fourth";
private String FIFTH_COLUMN = "Fifth";
private String SIXTH_COLUMN = "Sixth";
String class_name="";
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
TextView txtFifth;
TextView txtSixth;
public ListViewAdapter(Activity activity,
ArrayList<HashMap<String, String>> list) {
super();
this.activity = activity;
this.list = list;
class_name = activity.getClass().toString();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.messages_row, null);
txtFirst = (TextView) convertView.findViewById(R.id.tvMsgId_msg);
txtSecond = (TextView) convertView.findViewById(R.id.tvCtId_msg);
txtThird = (TextView) convertView.findViewById(R.id.tvCtName_msg);
txtFourth = (TextView) convertView.findViewById(R.id.tvCtNo_msg);
txtFifth = (TextView) convertView.findViewById(R.id.tvMsgDate_msg);
txtSixth = (TextView) convertView.findViewById(R.id.tvSms_msg);
}
HashMap<String, String> map = list.get(position);
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
txtThird.setText(map.get(THIRD_COLUMN));
txtFourth.setText(map.get(FOURTH_COLUMN));
txtFifth.setText(map.get(FIFTH_COLUMN));
txtSixth.setText(map.get(SIXTH_COLUMN));
return convertView;
}
public void clear() {
list.clear();
this.notifyDataSetChanged();
}
}
而且,這裏是填充列表的代碼部分。
private void showInboxMessages() {
String SORT_ORDER = "date DESC";
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = this.getContentResolver().query(uriSms, null, null, null, SORT_ORDER);
if (cursor != null) {
cursor.moveToLast();
if (cursor.getCount() > 0) {
HashMap<String, String> contacts = new HashMap<String, String>();
int _id = 1;
do {
//String _id = cursor.getString(cursor.getColumnIndex("_id"));
String person = cursor.getString(cursor.getColumnIndex("person"));
String address = cursor.getString(cursor.getColumnIndex("address"));
if (person == null || person.equals("")) {
if (contacts.containsKey(address))
person = (String) contacts.get(address);
else {
person = Methods.getContactName(this, address);
contacts.put(address,person);
}
}
String body = cursor.getString(cursor.getColumnIndex("body"));
Long date = cursor.getLong(cursor.getColumnIndex("date"));
Date dateVal=new Date(date);
SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.DATE_FORMAT2, Locale.getDefault());
String datetext = dateFormat.format(dateVal);
add_to_list(String.valueOf(_id), "", person, address, datetext, body);
_id ++;
} while (cursor.moveToPrevious());
}
}
}
private void add_to_list(String i, String n, String c, String d, String e, String f) {
HashMap<String, String> temp = new HashMap<String, String>();
temp.put(FIRST_COLUMN, i);
temp.put(SECOND_COLUMN, n);
temp.put(THIRD_COLUMN, c);
temp.put(FOURTH_COLUMN, d);
temp.put(FIFTH_COLUMN, e);
temp.put(SIXTH_COLUMN, f);
listarray.add(temp);
adapter.notifyDataSetChanged();
}