ORMLite 和 ListView

ORMLite and ListView

我正在使用 ORMLite,我想在 listView 中显示数据,例如,我有一个 SMS 列表(id、号码、消息),所以当我查询所有内容时,我就有了所有数据现在我想做一个视图来显示每个数据,所以我找到了如何做一个 listView 但我不知道如何在每一行上显示数字和消息...

public class SMSActivity extends Activity {

    ListView lview;
    SMSAdapter lviewAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms);



        DatabaseHandler db = new DatabaseHandler(this);

        Dao<SMS, Integer> SMSDao = null;

        try {
            SMSDao = db.getSMSDao();
            final List<SMS> users = SMSDao.queryForAll();
            Log.d("list", users.toString());


            lview = (ListView) findViewById(R.id.mainListView);

            lviewAdapter = new SMSAdapter(this, ?, ?);

            System.out.println("adapter => "+lviewAdapter.getCount());

            lview.setAdapter(lviewAdapter);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

短信适配器:

public class SMSAdapter extends BaseAdapter {
    Activity context;
    String title[];
    String description[];

    public SMSAdapter(Activity context, String[] title, String[] description) {
        super();
        this.context = context;
        this.title = title;
        this.description = description;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return title.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    private class ViewHolder {
        TextView txtViewTitle;
        TextView txtViewDescription;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        ViewHolder holder;
        LayoutInflater inflater =  context.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.row_sms, null);
            holder = new ViewHolder();
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.firstLine);
            holder.txtViewDescription = (TextView) convertView.findViewById(R.id.secondLine);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtViewTitle.setText(title[position]);
        holder.txtViewDescription.setText(description[position]);

        return convertView;
    }

}

首先,我建议您使用 RecyclerView 而不是 ListView。其次,我的建议也是在适配器中传递整个用户列表。 我想你想这样做:

//...
lview = (ListView) findViewById(R.id.mainListView);
List<String> messages = new ArrayList<String>();
List<String> numbers = new ArrayList<String>();

for(int i = 0; i < users.size(); i++){
    SMS sms = users.get(i);
    messages.add(sms.getMessage());
    numbers.add(sms.getNumber());
}
lviewAdapter = new SMSAdapter(this, numbers, messages);
System.out.println("adapter => "+lviewAdapter.getCount());

lview.setAdapter(lviewAdapter);
//...

您的适配器需要两个字符串数组,您可以从用户列表中获取它们。