我正在尝试在列表视图中显示来自收件箱的所有短信。
问题:
启动活动时,列表视图中的消息填充正确。但是当我向上或向下滚动中间和角落中部分消息的内容时,会自动更改位置。我无法追查列表异常行为的原因。能否请您查看适配器类或其他代码部分的问题。
代码:
我正在使用以下自定义适配器来填充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();
}
答案 0 :(得分:1)
尝试从add_to_list方法中删除 import subprocess
my_ip = subprocess.Popen(['ifconfig eth0 | awk "/inet /" | cut -d":" -f 2 | cut -d" " -f1'], stdout=subprocess.PIPE, shell=True)
(IP,errors) = my_ip.communicate()
my_ip.stdout.close()
print IP
并在adapter.notifyDataSetChanged();
内添加此行但是在/ outside之后执行While循环。
答案 1 :(得分:1)
//Copy and paste code below then see if there is change. This must be work for now. Hope this help. Setting final to all view/position to not change the value.
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(final int position, final View convertView, final ViewGroup parent) {
final 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();
}
}
答案 2 :(得分:1)
基本上listview会回收您的观点。您需要检查查看者的概念。 例如:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_entry, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Person person = getItem(position);
holder.nameTextView.setText(person.getName());
holder.surnameTextView.setText(person.getSurname());
//holder.personImageView.setImageBitmap(person.getImage());
return convertView;
}
答案 3 :(得分:0)
我已经更改了适配器类的代码,并将ArrayList与我的数据模型类'Message'一起使用。现在列表正在显示和滚动。
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<Message> {
private ArrayList<Message> dataItems;
Context context;
public CustomAdapter(Context context, ArrayList<Message> items) {
super(context, R.layout.messages_row, items);
this.context = context;
this.dataItems = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.messages_row, parent, false);
TextView _id = (TextView) convertView.findViewById(R.id.tvMsgId_msg);
TextView ctId = (TextView) convertView.findViewById(R.id.tvCtId_msg);
TextView ctName = (TextView) convertView.findViewById(R.id.tvCtName_msg);
TextView ctNo = (TextView) convertView.findViewById(R.id.tvCtNo_msg);
TextView msgDate = (TextView) convertView.findViewById(R.id.tvMsgDate_msg);
TextView msg = (TextView) convertView.findViewById(R.id.tvSms_msg);
Message obj = (Message) dataItems.get(position);
_id.setText(String.valueOf(obj.get_id()));
ctId.setText(String.valueOf(obj.getContact_id()));
ctName.setText(String.valueOf(obj.getContact_Name()));
ctNo.setText(String.valueOf(obj.getContact_No()));
msgDate.setText(String.valueOf(obj.getSms_date()));
msg.setText(String.valueOf(obj.getSms()));
return convertView;
}
}