【问题标题】:How can I add to my list from SQLite如何从 SQLite 添加到我的列表
【发布时间】:2012-05-09 01:24:38
【问题描述】:

我的应用程序将接收短信,将其保存在 SQLite 中,然后将其显示在我的列表中。 其余的都完成了,但我无法将数据放入我的列表中;我该怎么做?

这是我正在尝试的:

list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long index) {
            // here send Recent case Object  OR or Recent case info.  to show case information

        }
    });

这是我的适配器:

 public class RecentCaseListAdapter extends BaseAdapter {
private RecentCases myPage;
static public List<RecentCaseClass> listOfCases;

RecentCaseClass entry;


public RecentCaseListAdapter(RecentCases R, List<RecentCaseClass> listOfCaseParameter) {
    this.myPage = R;
    this.listOfCases = listOfCaseParameter;

}

public int getCount() {
    return listOfCases.size();
}

public Object getItem(int position) {
    return listOfCases.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup viewGroup) {

     entry = listOfCases.get(position);

     if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) myPage.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.recent_cases_row, null);
      }

    // this is row items..
    // Set the onClick Listener on this button

    TextView tvCase = (TextView) convertView.findViewById(R.id.tvrecentName);
    tvCase.setText(entry.getName());
    TextView tvCaseInfo = (TextView) convertView.findViewById(R.id.recent_info);
    tvCaseInfo.setText(entry.getAge());

    // To be a clickable button



    return convertView;
}



public void add(RecentCaseClass rCaseClass) {
    // TODO Auto-generated method stub
    listOfCases.add(rCaseClass);
}


}

这是我的清单

    public static List<RecentCaseClass> RecentCaseList = new ArrayList<RecentCaseClass>();

public static List<RecentCaseClass> getRecentCaseList() {
    return RecentCaseList;
}

public static void setRecentCaseList(List<RecentCaseClass> recentCaseList) {
    RecentCaseList = recentCaseList;
}

我应该在哪里添加列表

if (SMSReceived.startsWith("Hi")) {
            Toast.makeText(context, "Iam in hi", Toast.LENGTH_LONG)
                    .show();

            msg = SMSReceived;

            RecentCaseClass rCase = run();
            dbAdapter.add(rCase);

            All_Static.RecentCaseList.add(recent1);
        }

【问题讨论】:

  • 您是否想了解如何从 SQLite 表中获取数据到 ListView 中?我不清楚你在哪个方面遇到了麻烦。您是否尝试过使用 SimpleCursorAdapter?
  • 您需要使用适配器才能将数据与 ListView 绑定。
  • @happydave 请检查我的更新
  • @ShashankKadne 你能检查我的编辑吗

标签: java android sqlite list


【解决方案1】:

我还是不太确定你在问什么,所以我不知道这是否有帮助。我认为你想大致做到以下几点:

1) 通过查询要放入列表中的数据来创建一个 android.database.Cursor 对象 2) 使用要显示的查询中的列创建一个字符串数组。例如。 String[] from_list = new String[] { "title", "case" }

3) 创建一个 int 数组,其中包含要填充的列表行中的 TextViews 的 ID。例如int[] to_list = new int[] {R.id.title, R.id.case}

4) 创建一个 SimpleCursorAdapter 将光标映射到您的列表:

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter (this, R.layout.list_row, mCursor, from_list, to_list);

5) `setListAdapter(mAdapter);

这有帮助吗?

【讨论】:

  • 不,我做了所有的数据库和适配器以及 bla bla,但现在我有我的列表的 baseadapter 和我的数据库的光标,问题是:我如何向用户显示 baseadapter 中的信息,你能检查我的代码吗?我都做到了,我认为我的问题是非常小的线,但我无法得到它
猜你喜欢
  • 2021-11-21
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
相关资源
最近更新 更多