【问题标题】:Loading data from SQLite Database into ListView将 SQLite 数据库中的数据加载到 ListView
【发布时间】:2017-03-21 01:29:21
【问题描述】:

我正在尝试从 Android 中的 SQLite DB 加载数据并将其值设置为 ListView。问题是,当我在 OnCreate 中加载数据时,它不显示任何内容,但是当我刷新应用程序时,它会显示数据。

请帮我解决这个问题!

加载任务代码:

public void loadTasks() {

    Cursor c = notesDb.rawQuery("SELECT * FROM notes", null);
    int titleIndex = c.getColumnIndex("title");
    int contentIndex = c.getColumnIndex("content");
    int dateIndex = c.getColumnIndex("date");
    int idIndex = c.getColumnIndex("object");
    if (c.moveToFirst()) {
        if (!titles.isEmpty()) {
            titles.clear();
        }
        if (!content.isEmpty()) {
            content.clear();
        }
        if (!dates.isEmpty()) {
            dates.clear();
        }
        if (!objectId.isEmpty()) {
            objectId.clear();
        }

        do {
            titles.add(c.getString(titleIndex));
            content.add(c.getString(contentIndex));
            dates.add(c.getString(dateIndex));
            objectId.add(c.getString(idIndex));

            Log.i("Title", c.getString(titleIndex));
            Log.i("Content", c.getString(contentIndex));
            Log.i("Date", c.getString(dateIndex));
            Log.i("Id", c.getString(idIndex));

        } while (c.moveToNext());

        tasksList.setAdapter(customAdapter);
        loadingBar.setVisibility(View.INVISIBLE);

    } else {

        if (titles.size() > 0 || content.size() > 0) {
            tasksList.setAdapter(customAdapter);
            loadingBar.setVisibility(View.INVISIBLE);
        } else {
            titles.add("Welcome To App !");
            content.add("Start taking notes by clicking the add button below !");

            final Calendar c1 = Calendar.getInstance();
            final String monthName = new SimpleDateFormat("MMMM").format(c1.getTime());
            dates.add(String.valueOf(monthName + " " + c1.get(Calendar.DAY_OF_MONTH)) + " " + c1.get(Calendar.YEAR));
            objectId.add("randomId");
            tasksList.setAdapter(customAdapter);
            loadingBar.setVisibility(View.INVISIBLE);
        }
    }
}

OnCreate 代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dolt);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    loadTasks();

在 OnCreate 中将数据传递给适配器:

customAdapter = new CustomAdapter(DoltActivity.this, titles, content, dates);

自定义适配器代码:

public class CustomAdapter extends BaseAdapter{

ArrayList<String> title;
ArrayList<String> contents;
ArrayList<String> dates;
Context context;
private static LayoutInflater inflater=null;
public CustomAdapter(DoltActivity mainActivity, ArrayList<String> titles, ArrayList<String> content, ArrayList<String> date) {
    // TODO Auto-generated constructor stub
    title=titles;
    contents=content;
    dates=date;
    context=mainActivity;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return title.size();
}

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

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

@Override
public int getItemViewType(int position) {
    return CustomAdapter.IGNORE_ITEM_VIEW_TYPE;
}

public static class Holder
{
    TextView tv;
    TextView content;
    TextView date;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder = new Holder();
    View rowView = null;

    rowView = inflater.inflate(R.layout.custom_row, null);

    holder.tv = (TextView) rowView.findViewById(R.id.textView10);
    holder.content = (TextView) rowView.findViewById(R.id.textView3);
    holder.date = (TextView) rowView.findViewById(R.id.textView4);

    holder.tv.setText(title.get(position));
    holder.content.setText(contents.get(position));
    holder.date.setText(dates.get(position));

    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            DoltActivity.tapped = true;
            DoltActivity.id = position;
            Log.i("Position", String.valueOf(position));

            }
        });

    rowView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {

            DoltActivity.longTapped = true;
            DoltActivity.id = position;

            Log.i("Long ", "Tapped");

            return true;
        }
    });


    return rowView;
}

}

刷新代码:

swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
                loadTasks();               
            swipeRefresh.setRefreshing(false);
        }
    });

提前致谢!

【问题讨论】:

  • customAdapter 代码在哪里,以及如何将光标传递给 customAdapter。
  • @shanyour 我也添加了这些代码!谢谢你通知我!!!

标签: java android sql sqlite listview


【解决方案1】:

试试这个,

1- 在将 loadTask() 设置为 ListView 之前(即之前),在 loadTask() 内初始化您的 customAdapter

customAdapter = new CustomAdapter(getActivity(), titles, content, dates);
tasksList.setAdapter(customAdapter);

您的代码中似乎存在逻辑数据流错误,您在调用 loadTask() 后初始化了 customAdapter,但在 loadTask() 内部,您正在使用该对象,而没有使用更新的 标题 列表对其进行初始化、内容日期

【讨论】:

    【解决方案2】:

    更新阵列后创建适配器。移动你的线路:

     customAdapter = new CustomAdapter(DoltActivity.this, titles, content, dates);
    

    在之前:

    tasksList.setAdapter(customAdapter);
    

    另外,您可以将下面的代码移到您的 if 之外,只是为了消除代码重复。你的 loadTask 函数如下:

    public void loadTasks() {
    
    Cursor c = notesDb.rawQuery("SELECT * FROM notes", null);
    int titleIndex = c.getColumnIndex("title");
    int contentIndex = c.getColumnIndex("content");
    int dateIndex = c.getColumnIndex("date");
    int idIndex = c.getColumnIndex("object");
    if (c.moveToFirst()) {
        if (!titles.isEmpty()) {
            titles.clear();
        }
        if (!content.isEmpty()) {
            content.clear();
        }
        if (!dates.isEmpty()) {
            dates.clear();
        }
        if (!objectId.isEmpty()) {
            objectId.clear();
        }
    
        do {
            titles.add(c.getString(titleIndex));
            content.add(c.getString(contentIndex));
            dates.add(c.getString(dateIndex));
            objectId.add(c.getString(idIndex));
    
            Log.i("Title", c.getString(titleIndex));
            Log.i("Content", c.getString(contentIndex));
            Log.i("Date", c.getString(dateIndex));
            Log.i("Id", c.getString(idIndex));
    
        } while (c.moveToNext());
    
    } else {
    
        if (titles.size() > 0 || content.size() > 0) {
    
        } else {
            titles.add("Welcome To App !");
            content.add("Start taking notes by clicking the add button below !");
    
            final Calendar c1 = Calendar.getInstance();
            final String monthName = new SimpleDateFormat("MMMM").format(c1.getTime());
            dates.add(String.valueOf(monthName + " " + c1.get(Calendar.DAY_OF_MONTH)) + " " + c1.get(Calendar.YEAR));
            objectId.add("randomId");
    
        }
    }
    
    customAdapter = new CustomAdapter(DoltActivity.this, titles, content, dates);
    tasksList.setAdapter(customAdapter);
    loadingBar.setVisibility(View.INVISIBLE);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 2019-07-19
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多