更高的API中ListView项为空

时间:2016-05-06 15:22:35

标签: android listview android-studio

我是Android新手。

我的问题是我的ListView项目视图没有出现在高于19的API上。

在创建项目时,我将目标API设置为KitKat(API 19),但现在当我查看build.app时,targetSdkVersion为23,minSdkVersion为14。

然后我将手机升级到Marshmellow,现在我想在手机上调试项目时,它工作得很好,但我的ListView项目是空的,应该有TextViews ...另一方面一切正常,甚至ListView项目在使用KitKat的模拟手机上。

我使用ArrayAdapter和Cursor从SQLite db。

获取数据

有没有其他方法可以期望从一开始就创建这个项目,使其在更高的API上正常工作?

build.app

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.asd.sqlite1"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'

}

ListView类

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

 public class RefuelListActivity extends AppCompatActivity {

     ListView RefuelListView;
     SQLiteDatabase sqLiteDatabase;
     DBHelper dbHelper;
     Cursor cursor;
     RefuelListAdapter refuelListAdapter;

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

        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        if (getSupportActionBar() != null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }

        RefuelListView = (ListView)findViewById(R.id.refuel_tab_list_view);


        refuelListAdapter = new RefuelListAdapter(getApplicationContext(),R.layout.refuel_list_item);
         RefuelListView.setAdapter(refuelListAdapter);

        dbHelper = new DBHelper(getApplicationContext());
        sqLiteDatabase = dbHelper.getWritableDatabase();


        cursor = dbHelper.getAllInformations(sqLiteDatabase);


        if (cursor.moveToFirst()){ 
            do{
                int fuel_id;
                float fuel_amount, fuel_price, cash_spend, km_counter;
                String fuel_date;

                // (0.id, 1.fuel_amount, 2.fuel_price, 3.cash_spend, 4.km_counter, 5.fuel_date)

                fuel_id = cursor.getInt(0);
                fuel_amount = cursor.getFloat(1);
                fuel_price = cursor.getFloat(2);
                cash_spend = cursor.getFloat(3);
                km_counter = cursor.getFloat(4);
                fuel_date = cursor.getString(5);


                RefuelListDataProvider refuelListDataProvider = new RefuelListDataProvider(fuel_id, fuel_amount,fuel_price,cash_spend,km_counter,fuel_date);
                refuelListAdapter.add(refuelListDataProvider);


            }while (cursor.moveToNext()); 
        }
    }
 }

适配器类

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class RefuelListAdapter extends ArrayAdapter {
    List list = new ArrayList();
    public RefuelListAdapter(Context context, int resource) {
        super(context, resource);
    }

    static class LayoutHandler{

        TextView KM_COUNTER, FUEL_AMOUNT, FUEL_PRICE, CASH_SPEND, FUEL_DATE;
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        LayoutHandler layoutHandler;
        if (row == null){
            //No idea what this is doing...
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            row = layoutInflater.inflate(R.layout.refuel_list_item,parent,false);
            layoutHandler = new LayoutHandler();
            layoutHandler.KM_COUNTER = (TextView)row.findViewById(R.id.lv_refuel_km_counter_text);
            layoutHandler.FUEL_AMOUNT = (TextView)row.findViewById(R.id.lv_refuel_fuel_amount_text);
            layoutHandler.FUEL_PRICE = (TextView)row.findViewById(R.id.lv_refuel_fuel_price_text);
            layoutHandler.CASH_SPEND = (TextView)row.findViewById(R.id.lv_refuel_cost_text);
            layoutHandler.FUEL_DATE = (TextView)row.findViewById(R.id.lv_refuel_data_text);

            row.setTag(layoutHandler);
        }
        else { layoutHandler = (LayoutHandler)row.getTag(); }

        RefuelListDataProvider refuelListDataProvider = (RefuelListDataProvider)this.getItem(position);

        layoutHandler.KM_COUNTER.setText(String.valueOf(refuelListDataProvider.getKm_counter()));
        layoutHandler.FUEL_AMOUNT.setText(String.valueOf(refuelListDataProvider.getFuel_amount()));
        layoutHandler.FUEL_PRICE.setText(String.valueOf(refuelListDataProvider.getFuel_price()));
        layoutHandler.CASH_SPEND.setText(String.valueOf(refuelListDataProvider.getCash_spend()));
        layoutHandler.FUEL_DATE.setText(refuelListDataProvider.getFuel_date());

        return row;
    }
}

我也使用Data Provider类。

public class RefuelListDataProvider {

    private int fuel_id;
    private float fuel_amount, fuel_price, cash_spend, km_counter;
    private String fuel_date;

    public RefuelListDataProvider(Integer fuel_id, Float fuel_amount, Float fuel_price, Float cash_spend, Float km_counter, String fuel_date){

        this.fuel_id = fuel_id;
        this.fuel_amount = fuel_amount;
        this.fuel_price = fuel_price;
        this.cash_spend = cash_spend;
        this.km_counter = km_counter;
        this.fuel_date = fuel_date;
    }

    public int getFuel_id() {
        return fuel_id;
    }

    public void setFuel_id(int fuel_id) {
        this.fuel_id = fuel_id;
    }

    public float getCash_spend() {
        return cash_spend;
    }

    public void setCash_spend(float cash_spend) {
        this.cash_spend = cash_spend;
    }

    public float getFuel_amount() {
        return fuel_amount;
    }

    public void setFuel_amount(float fuel_amount) {
        this.fuel_amount = fuel_amount;
    }

    public String getFuel_date() {
        return fuel_date;
    }

    public void setFuel_date(String fuel_date) {
        this.fuel_date = fuel_date;
    }

    public float getFuel_price() {
        return fuel_price;
    }

    public void setFuel_price(float fuel_price) {
        this.fuel_price = fuel_price;
    }

    public float getKm_counter() {
        return (km_counter);
    }

    public void setKm_counter(float km_counter) {
        this.km_counter = km_counter;
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。为了解决这个问题,我在RefuelListActivity中更改了一个通道:

refuelListAdapter = new RefuelListAdapter(getApplicationContext(),R.layout.refuel_list_item);

refuelListAdapter = new RefuelListAdapter(this,R.layout.refuel_list_item);

现在工作正常。 谢谢你的帮助我猜。