gpt4 book ai didi

java - 自定义ListView不显示任何数据?

转载 作者:行者123 更新时间:2023-12-01 17:44:38 25 4
gpt4 key购买 nike

我的自定义 ListView 有一些问题,自定义 ListView 不显示我给他们的任何数据。我尝试将adapter.notifyDataSetChanged();但似乎没有什么区别。从数据库获取的数据未显示

这是我的 Activity


import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class Transaksi extends AppCompatActivity {

ListView LISTVIEW;
ArrayList<Transaction> translist;
Transaction transaction;
DatabaseHelper handler;
CursorAdaptor1 adapter;
public static int okko;
FloatingActionButton fab;

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

handler = new DatabaseHelper(this);
Cursor cursor = handler.viewTrans(getIntent().getIntExtra("EXTRA_ID",0));

fab = (FloatingActionButton) findViewById(R.id.fab);

okko = getIntent().getIntExtra("EXTRA_ID",0);

translist = new ArrayList<>();
adapter = new CursorAdaptor1(Transaksi.this, R.layout.adapter_view_layout,translist);
LISTVIEW = (ListView) findViewById(R.id.LISTVIEW);
LISTVIEW.setAdapter(adapter);

int a = cursor.getCount();

cursor.moveToFirst();

Toast.makeText(this, ""+cursor.getString(1)+" "+cursor.getString(2)+" "+cursor.getString(3)+" "+cursor.getDouble(4)+" "+cursor.getDouble(5)+" "+cursor.getDouble(6), Toast.LENGTH_SHORT).show();


if(cursor.moveToFirst() && a >= 1){
while (cursor.moveToNext()){
transaction = new Transaction(cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getDouble(4),cursor.getDouble(5),cursor.getDouble(6));
adapter.notifyDataSetChanged();
translist.add(transaction);
}
adapter.notifyDataSetChanged();

}else{
Toast.makeText(this, "There is not transaction yet!!", Toast.LENGTH_SHORT).show();
}

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});


}

public void openDialog(){
ExampleDialog exampleDialog = new ExampleDialog();
exampleDialog.show(getSupportFragmentManager(),"Example Dialog");
}

这是我的适配器类


import android.content.Context;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.example.werren.testapp.R;

import java.util.ArrayList;

public class CursorAdaptor1 extends ArrayAdapter<Transaction> {

private LayoutInflater mInflater;
private ArrayList<Transaction> transaction;
private int mViewResourceId;

public CursorAdaptor1(Context context, int textViewResourceId, ArrayList<Transaction> transactions){
super(context,textViewResourceId,transactions);
this.transaction = transactions;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
}

@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){
return newView(position,convertView,parent);
}

public View newView(int position, View convertView, ViewGroup parent) {

if(convertView == null){
convertView = mInflater.inflate(mViewResourceId,null);
}

Transaction trans = transaction.get(position);

if(trans != null){
TextView tanggal = (TextView) convertView.findViewById(R.id.FirstText);
TextView cur_beli = (TextView) convertView.findViewById(R.id.SecondText);
TextView cur_jual = (TextView) convertView.findViewById(R.id.ThirdText);
TextView jumlah = (TextView) convertView.findViewById(R.id.FourthText);
TextView kurs = (TextView) convertView.findViewById(R.id.FifthText);
TextView total = (TextView) convertView.findViewById(R.id.SixthText);

String a = String.valueOf(trans.getJumlah1());
String b = String.valueOf(trans.getKurs1());
String c = String.valueOf(trans.getTotal1());

tanggal.setText(trans.getTanggal1());
cur_beli.setText(trans.getCur_beli1());
cur_jual.setText(trans.getCur_jual1());
jumlah.setText(a);
kurs.setText(b);
total.setText(c);
}
return convertView;
}


}

这是while循环崩溃的Logcat


2020-03-29 03:33:50.235 11124-11124/com.example.werren.testapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.werren.testapp, PID: 11124
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.werren.testapp/com.example.werren.testapp.Transaksi}: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.werren.testapp.Transaksi.onCreate(Transaksi.java:67)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

最佳答案

  1. adapter.notifyDataSetChanged();应该在LISTVIEW.setAdapter(adapter);之后而不是在它之前。建议搬迁new CursorAdaptor1translist = new ArrayList<>(); 之后。然后您可以使用.notifyDataSetChange()之后while (cursor.moveToNext()){ ... } .
    translist = new ArrayList<>();
//fixme-add-start
adapter = new CursorAdaptor1(Transaksi.this, R.layout.adapter_view_layout,translist);
// adapter.notifyDataSetChanged(); //fixme:bug found
LISTVIEW = (ListView) findViewById(R.id.LISTVIEW);
LISTVIEW.setAdapter(adapter);
//fixme-add-end

    if(cursor.moveToFirst() && a >= 1){
while (cursor.moveToNext()){
//...
}
// adapter = new CursorAdaptor1(Transaksi.this, R.layout.adapter_view_layout,translist); //fixme:del
adapter.notifyDataSetChanged();
// LISTVIEW = (ListView) findViewById(R.id.LISTVIEW); //fixme:del
// LISTVIEW.setAdapter(adapter); //fixme:del

  • CursorAdaptor1类,newView()不被调用。需要添加覆盖getView()函数调用 newView() 。还添加了if (convertView == null) {检查一下。
  •   @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    return newView(position, convertView, parent);
    }

    public View newView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) { //fixme:add
    convertView = mInflater.inflate(mViewResourceId, null);
    } //fixme:add

    Transaction trans = transaction.get(position);
    // ...

    关于java - 自定义ListView不显示任何数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60883201/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com