尝试使用CursorAdapter使ListView显示新信息

时间:2014-05-10 01:36:22

标签: java android sqlite listview

我是一名新的Android程序员。我将数据输入到SQLite数据库中,然后我希望我的ListView刷新并显示我刚刚添加到数据库中的信息。 ListView位于添加按钮下方。 logcat错误显示NullPointerException。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.34"
            android:text="Registration No:"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/vehicle_reg"
            android:layout_width="175dp"
            android:layout_height="wrap_content"
            android:ems="10"
            />
          <requestFocus />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.43"
            android:text="Mileage:"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/vehicle_mileage"
            android:layout_width="114dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.29"
            android:ems="10" >


        </EditText>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_weight="0.51"
            android:text="Budget Amount:"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/vehicle_budget"
            android:layout_width="167dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.13"
            android:ems="10"
            android:inputType="numberDecimal" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.37"
            android:text="Tank Capacity: "
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/vehicle_capacity"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:ems="10" />
    </LinearLayout>

    <LinearLayout
           android:id="@+id/LinearLayout10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/add_button_vehicle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add Vehicle" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.43"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/vehicleList_reg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>

    </LinearLayout>

</LinearLayout>

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.budgetfuel.databasemodel.Car;
import com.example.budgetfuel.databasemodel.DBhelper;
import com.example.budgetfuel.databasemodel.DatabaseHelper;
import com.example.budgetfuel.databasemodel.SQLController;

public class VehicleSetup extends FragmentActivity {

    Button addvehicle;
    ListView lv;
    SQLController dbcon;
    Button btnAdd;
    EditText reg, milge, cap, budgt;

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

        // get action bar
        ActionBar actionBar = getActionBar();

        // Enabling Up / Back navigation
        actionBar.setDisplayHomeAsUpEnabled(true);
        dbcon = new SQLController(this);
        dbcon.open();
        addvehicle = (Button) findViewById(R.id.add_button_vehicle);
        lv = (ListView) findViewById(R.id.listViewVehicles);
        reg = (EditText) findViewById(R.id.vehicle_reg);
        milge = (EditText) findViewById(R.id.vehicle_mileage);
        cap = (EditText) findViewById(R.id.vehicle_budget);
        budgt = (EditText) findViewById(R.id.vehicle_capacity);



        // Attach The Data From DataBase Into ListView Using Crusor Adapter
        Cursor cursor = dbcon.readData();
        String[] from = new String[] {DBhelper.VEHICLE_ID,DBhelper.VEHICLE_REG, DBhelper.VEHICLE_BUDGET, DBhelper.VEHICLE_CAPACITY };

        int[] to = new int[] { R.id.vehicle_id, R.id.vehicle_reg, R.id.vehicle_mileage, R.id.vehicle_budget,
                R.id.vehicle_capacity };
        @SuppressWarnings("deprecation")

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                VehicleSetup.this, R.layout.view_car_entry, cursor, from, to);
        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);
//      
        addvehicle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String registration = reg.getText().toString();
                int mileage = Integer.parseInt(milge.getText().toString());
                int capacity = Integer.parseInt(cap.getText().toString());
                double budget = Double.parseDouble(budgt.getText().toString());
                dbcon.insertData(registration, mileage, budget, capacity);

                reg.setText("");
                milge.setText("");
                cap.setText("");
                budgt.setText("");


            }
        });



    }

1 个答案:

答案 0 :(得分:0)

尝试将adapter.notifyDataSetChanged();放在lv.setAdapter(adapter);

之后