当我从用户获取数据并将其保存到 SQLite 数据库中时,我应该怎么做才能使列表视图在片段中工作

Posted

技术标签:

【中文标题】当我从用户获取数据并将其保存到 SQLite 数据库中时,我应该怎么做才能使列表视图在片段中工作【英文标题】:What should I do to make the listview work in the fragment when I am getting data from the user and saving it into a SQLite database 【发布时间】:2018-01-27 00:33:55 【问题描述】:

这显示了其中包含列表视图代码的片段之一,该代码在它自己的布局中工作但在片段中不起作用。我应该怎么做才能在片段中工作?我对android应用程序开发相当陌生,所以我不知道该怎么做。如果您不明白我要做什么,我让用户将数据输入表单,然后我希望该数据作为列表显示在片段中,以便他们可以看到从头到尾输入的所有数据。我一直在寻找解决方案,但找不到适合我需要做的解决方案。我看到的所有信息都是针对预先保存的 sq lite 数据库。

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.database.Cursor;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.Toast;

import java.util.ArrayList;

import static android.R.attr.button;

public class tab3expense extends Fragment 

    private static final String TAG = "tab3expense";
    DatabaseHelper mDatabaseHelper;
    private ListView mListView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.tab3expense, container, false);
        return rootView;

        mListView = (ListView) findViewById(R.id.listView);
        mDatabaseHelper = new DatabaseHelper(this);

        populateListView();

    

    private void populateListView() 
        Log.d(TAG, "populateListView: Displaying data in the ListView.");

        //get the data and append to a list
        Cursor data = mDatabaseHelper.getData();
        ArrayList<String> listData = new ArrayList<>();
        while(data.moveToNext())
            //get the value from the database in column 1
            //then add it to the ArrayList
            listData.add(data.getString(1));
            listData.add(data.getString(2));
            listData.add(data.getString(3));
        
        //create the list adapter and set the adapter
        ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
        mListView.setAdapter(adapter);

        //set an onItemClickListener to the ListView
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                String name = adapterView.getItemAtPosition(i).toString();
                Log.d(TAG, "onItemClick: You Clicked on " + name);

                Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
                int itemID = -1;
                while(data.moveToNext())
                    itemID = data.getInt(0);
                
                if(itemID > -1)
                    Log.d(TAG, "onItemClick: The ID is: " + itemID);
                    Intent editScreenIntent = new Intent(tab3expense.this, add_expense.class);
                    editScreenIntent.putExtra("id",itemID);
                    editScreenIntent.putExtra("name",name);
                    startActivity(editScreenIntent);
                
                else
                    toastMessage("No ID associated with that name");
                
            
        );
    

    /**
     * customizable toast
     * @param message
     */
    private void toastMessage(String message)
        Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
    

这是布局文件

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.dharquissandas.budget.MainActivity$PlaceholderFragment">

<ListView
    android:layout_
    android:layout_
    android:id="@+id/listView"/>
</LinearLayout>    

【问题讨论】:

只是为了澄清......告诉我你在 onCreateView 中的 return rootView; 行之后没有收到任何编译错误? 这一行出现错误:Intent editScreenIntent = new Intent(tab3expense.this, add_expense.class); 【参考方案1】:

Fragment 类上下文将通过以下方法访问 getActivity()。在这里,我对您的代码进行了一些更改。您必须在 onCreateView() 中扩充您的布局视图,然后在 onActivityCreated() 中投射您的列表视图组件。请看下面的代码。提前致谢。

public class Tab3expense extends Fragment 


    private static final String TAG = "tab3expense";
    DatabaseHelper mDatabaseHelper;
    private ListView mListView;
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
         rootView = inflater.inflate(R.layout.tab3expense, container, false);
        return rootView;



    

    @Override
    public void onActivityCreated( Bundle savedInstanceState) 
        super.onActivityCreated(savedInstanceState);
        mListView = (ListView) rootView.findViewById(R.id.listView);
        mDatabaseHelper = new DatabaseHelper(getActivity());

        populateListView();
    

    private void populateListView() 
        Log.d(TAG, "populateListView: Displaying data in the ListView.");

        //get the data and append to a list
        Cursor data = mDatabaseHelper.getData();
        ArrayList<String> listData = new ArrayList<>();
        while(data.moveToNext())
            //get the value from the database in column 1
            //then add it to the ArrayList
            listData.add(data.getString(1));
            listData.add(data.getString(2));
            listData.add(data.getString(3));
        
        //create the list adapter and set the adapter
        ListAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listData);
        mListView.setAdapter(adapter);

        //set an onItemClickListener to the ListView
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 
                String name = adapterView.getItemAtPosition(i).toString();
                Log.d(TAG, "onItemClick: You Clicked on " + name);

                Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
                int itemID = -1;
                while(data.moveToNext())
                    itemID = data.getInt(0);
                
                if(itemID > -1)
                    Log.d(TAG, "onItemClick: The ID is: " + itemID);
                    Intent editScreenIntent = new Intent(getActivity(), add_expense.class);
                    editScreenIntent.putExtra("id",itemID);
                    editScreenIntent.putExtra("name",name);
                    startActivity(editScreenIntent);
                
                else
                    toastMessage("No ID associated with that name");
                
            
        );
    

    /**
     * customizable toast
     * @param message
     */
    private void toastMessage(String message)
        Toast.makeText(getActivity(),message, Toast.LENGTH_SHORT).show();
    

【讨论】:

谢谢,这行得通,但它不显示我从表单中添加的任何新数据,它只显示旧数据。有没有办法解决这个问题? @DeepHarquissandas 您创建了三个固定大小的 ArrayList,尝试更改动态 ArrayList 将有助于显示新数据。 我该怎么做呢,我在 android 编程方面还很新,我正在学习。【参考方案2】:

重新排列你的代码

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View rootView = inflater.inflate(R.layout.tab3expense, container, false);


        mListView = (ListView)rootView. findViewById(R.id.listView);
        mDatabaseHelper = new DatabaseHelper(getContext());

        populateListView();
 return rootView;

    

【讨论】:

以上是关于当我从用户获取数据并将其保存到 SQLite 数据库中时,我应该怎么做才能使列表视图在片段中工作的主要内容,如果未能解决你的问题,请参考以下文章

将图像保存到核心数据中并将其提取到 tableview 单元格中

使用SQLite保存数据并将其添加到ListView

如何从API加载图像并将其保存到sqlite数据库中?

viewContext 未保存在数据库中(app.sqlite)

将数据从列表视图(从 Rest Api 生成)传递到另一个活动并将其保存到 SQLite 数据库中

当我从实时数据库读取数据时,它只获取用户名并将 imageurl 获取为“null”但是我在实时数据库中有 url