Mam pewien problem z widoku listy niestandardowych, widok niestandardowy widoku listy nie pokazuje żadnych danych, które je dałem. Staram się umieścić adapter.notifydatasanchanged (); Ale wydaje się, że nie ma różnicy. Dane pobierane z bazy danych nie są wyświetlane

To moja aktywność


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");
    }

To jest moja klasa adapterów


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;
    }


}

Oto logkcat podczas awarii pętli


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) 
0
Sacrivar 27 marzec 2020, 12:26

1 odpowiedź

Najlepsza odpowiedź
  1. adapter.notifyDataSetChanged(); powinien być po LISTVIEW.setAdapter(adapter);, a nie przed nim. Zaproponuj przeniesienie new CursorAdaptor1 do po translist = new ArrayList<>();. Następnie możesz użyć .notifyDataSetChange() po 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

  1. W CursorAdaptor1 klasa, newView() nie zostanie wywołana. Musisz dodać funkcję nadpisania getView(), aby zadzwonić do newView(). Dodano również czek 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);
// ...

0
RandomThoughts2468 27 marzec 2020, 12:18