自定义列表视图重复最后一行

时间:2017-10-19 16:06:28

标签: android listview baseadapter

嗨我想从数据库中获取值并在listview中显示

我从数据库中获取6个值并将其发送到适配器,但所有6行都是要发送到适配器的最后一项

当我添加

ViewHolder holder;

android studio后alt +输入更改为

RecyclerView.ViewHolder holder;

我阅读了这篇文章,但无法解决问题

Rows being repeated in ListView

Duplicated entries in ListView

Android Listview row repeating item

List item repeating in android customized listview

我知道这个问题是重复但无法修复 提前致谢

我的适配器:

public class lim_sms_adapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private List<lim_sms> lim_smsItems;



public lim_sms_adapter (Activity activity, List<lim_sms> lim_smsItems) {
    this.activity = activity;
    this.lim_smsItems = lim_smsItems;
}
@Override
public int getCount() {
    return lim_smsItems.size();
}
@Override
public Object getItem(int location) {
    return lim_smsItems.get(location);
}
@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getViewTypeCount() {

    return getCount();
}

@Override
public int getItemViewType(int position) {

    return position;
}

@SuppressLint("SetTextI18n")
@Override
public View getView(int position, View convertView, ViewGroup parent) {


    if (inflater == null)
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.row_lim_sms, null);

    TextView number = (TextView) convertView.findViewById(R.id.row_lim_sms_txt_number);

    lim_sms m =  lim_smsItems.get(position);

     number.setText(m.getSms_number());

    return convertView;
}

}

我的班级seter和geter:

public class lim_sms {
String sms_number;

public lim_sms(String sms_number) {
    this.sms_number = sms_number;
}

public lim_sms() {
}

public String getSms_number() {
    return sms_number;
}

public void setSms_number(String sms_number) {
    this.sms_number = sms_number;
}
}

我的活动:

public class list_limit_sms extends AppCompatActivity {

private List<lim_sms> limit_sms = new ArrayList<lim_sms>();
private lim_sms_adapter adapter;
private sms_database sms_db = new sms_database(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_limit_sms);

    ListView listView = (ListView) findViewById(R.id.list_lim_sms);

    adapter = new lim_sms_adapter(this, limit_sms);
    listView.setAdapter(adapter);

    lim_sms cy = new lim_sms();
    List<sms_class> sms_list = sms_db.getAllContacts();

    for (sms_class ss : sms_list) {
        //String log = "Id: "+ss.getId()+" ,num: " + ss.getNum() + " ,type: " + ss.getType();
        //Toast.makeText(this, ""+log, Toast.LENGTH_LONG).show();
        cy.setSms_number(ss.getNum());
        Toast.makeText(this, ""+ss.getNum(), Toast.LENGTH_SHORT).show();
        limit_sms.add(cy);
        adapter.notifyDataSetChanged();
    }


}
}

0 个答案:

没有答案