lundi 30 novembre 2015

RecylerView won't populate

I'm trying to populate a RecyclerView from an SQLite database however I'm not seeing anything being displayed when the code runs. There are no error messages, so there's no logcat to show. I initially thought it had something to do with List<> and ArrayList<> differences, but I had no luck with that idea, so I've come here to hopefully get some help from more experienced developers.

I know there's a lot of code down there, but I wanted to post everything that's related to the one RecyclerView, so we don't miss anything at all. I tried to think of everything you'd need, but if you need more, please don't hesitate to speak up.

Thank you all for your time.

MainActivity.java:

import android.content.Intent;
import android.database.Cursor;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.example.michael.budgetapp.Database.CategoryLoad;
import com.example.michael.budgetapp.Database.TransactionLoad;
import com.example.michael.budgetapp.Tabs.SlidingTabLayout;
import com.example.michael.budgetapp.Tabs.ViewPagerAdapter;

public class MainActivity extends AppCompatActivity
    implements CategoryLoad.categoryLoadComplete,
    TransactionLoad.LoadComplete {

private android.support.v7.widget.Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
ViewPagerAdapter mAdapter;
CharSequence Titles[]={"Budgets", "Transactions"};
int Numboftabs = 2;
private Cursor data = null;

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

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

    mAdapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
    mTabs.setDistributeEvenly(true);
    mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.accent);
        }
    });
    mTabs.setViewPager(mPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, AddTransaction.class));
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main_activity, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        startActivity(new Intent(this, Settings.class));
        return true;
    }

    if (id == R.id.category) {
        startActivity(new Intent(this, AddCategory.class));
        return true;
    }
    if (id == R.id.search) {
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void categoryLoadComplete(Cursor cursor) {
    data = cursor;
}

@Override
public void transactionLoadComplete(Cursor cursor) {

}
}

AdapterTransactions.java:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.michael.budgetapp.R;
import java.util.Collections;
import java.util.List;

public class AdapterTransactions extends RecyclerView.Adapter<AdapterTransactions.MyViewHolder> {

private LayoutInflater inflater;
List<TransactionInfo> transactionData = Collections.emptyList();

public AdapterTransactions(Context context, List<TransactionInfo> transactionData) {
    inflater = LayoutInflater.from(context);
    this.transactionData = transactionData;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.transaction_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    TransactionInfo current = transactionData.get(position);
    holder.itemName.setText(current.itemName);
    holder.itemCat.setText(current.itemCat);
    holder.itemPrice.setText(current.itemPrice);
    holder.itemDate.setText(current.date);
    holder.itemCurrency.setText(current.itemCurrency);
}

@Override
public int getItemCount() {
    return transactionData.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {
    TextView itemName, itemCat, itemPrice, itemDate, itemCurrency;

    public MyViewHolder(View itemView) {
        super(itemView);
        itemName = (TextView) itemView.findViewById(R.id.itemName);
        itemCat = (TextView) itemView.findViewById(R.id.itemCat);
        itemPrice = (TextView) itemView.findViewById(R.id.itemPrice);
        itemDate = (TextView) itemView.findViewById(R.id.itemDate);
        itemCurrency = (TextView) itemView.findViewById(R.id.itemCurrency);
    }
}
}

TransactionsTab.java:

import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.michael.budgetapp.Database.DatabaseHelper;
import com.example.michael.budgetapp.Database.TransactionLoad;
import com.example.michael.budgetapp.R;
import java.util.ArrayList;
import java.util.List;

public class TransactionsTab extends Fragment
    implements TransactionLoad.LoadComplete {

RecyclerView transactionsRecycler;
AdapterTransactions adapter;
TransactionLoad load = null;
List<TransactionInfo> adapterData = new ArrayList<>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (load == null) {
        load = new TransactionLoad(getActivity());
        load.execute();
    }
}

@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab_transactions,container,false);
    transactionsRecycler = (RecyclerView) view.findViewById(R.id.transactionRecycler);
    adapter = new AdapterTransactions(getActivity(), adapterData);
    transactionsRecycler.setAdapter(adapter);
    transactionsRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
    return view;
}

private static ArrayList<TransactionInfo> convertCursorToBudgetInfoList(Cursor cursor) {

    if (cursor == null) {
        return new ArrayList<TransactionInfo>();
    }
    TransactionInfo returnListValue = new TransactionInfo();
    ArrayList<TransactionInfo> returnList = new ArrayList<>(cursor.getCount());

    if (cursor.moveToFirst()) {
        for (int i = 10; i < cursor.getCount(); i++) {
            returnListValue.itemName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRANSACTION_NAME));
            returnListValue.itemCat = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRANSACTION_CATEGORY));
            returnListValue.itemCurrency = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRANSACTION_CURRENCY));
            returnListValue.itemPrice = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRANSACTION_PRICE));
            returnListValue.date = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TRANSACTION_DATE));
            returnList.add(returnListValue);
        }
    }

    return returnList;

}

@Override
public void transactionLoadComplete(Cursor cursor) {
    adapterData = convertCursorToBudgetInfoList(cursor);
    adapter.notifyDataSetChanged();
    adapter.transactionData.notifyAll();
}
}

TransactionInfo.java:

public class TransactionInfo {
public String itemName, itemPrice, itemCurrency, itemCat, date;
}

transaction_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Lunch"
            android:layout_gravity="center_vertical"
            android:id="@+id/itemName"
            android:textColor="@color/primary_dark"
            android:textStyle="bold"
            android:padding="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Food"
            android:textStyle="bold"
            android:layout_gravity="center_vertical"
            android:id="@+id/itemCat"
            android:textColor="@color/primary_text"
            android:padding="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="87"
            android:layout_gravity="center_vertical"
            android:id="@+id/itemPrice"
            android:textColor="@color/primary_text"
            android:padding="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="USD"
            android:layout_gravity="center_vertical"
            android:id="@+id/itemCurrency"
            android:textColor="@color/primary_text"
            android:padding="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="2015/11/25; 20:34"
            android:layout_gravity="center_vertical"
            android:id="@+id/itemDate"
            android:textColor="@color/primary_text"
            android:padding="5dp"/>

    </TableRow>

</TableLayout>

tab_transactions.xml:

<RelativeLayout
xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tabs.Transactions.TransactionsTab">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/transactionRecycler">
</android.support.v7.widget.RecyclerView>

TransactionLoad.java:

import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.util.Log;
import java.lang.ref.WeakReference;

public class TransactionLoad extends AsyncTask<Void, Void, Cursor> {

private static final String TAG = "LoadTask";
private LoadComplete loadComplete;
private WeakReference<Context> transactionWeakReference;
private DatabaseHelper tt;

public interface LoadComplete {
    void transactionLoadComplete(Cursor cursor);
}

public TransactionLoad(Context context) {
    transactionWeakReference = new WeakReference<>(context);
    tt = DatabaseHelper.getInstance(transactionWeakReference.get());
    try {
        loadComplete = (LoadComplete) transactionWeakReference.get();
    } catch (ClassCastException e) {
        Log.e(TAG, context.toString() + " must implement LoadComplete");
    }
}

@Override
protected Cursor doInBackground(Void... params) {
    Cursor result = tt.getReadableDatabase().query(
            DatabaseHelper.TRANSACTIONS_TABLE,  // table name
            null, // columns
            null, // where
            null, // where arguments
            null, // group by
            null, // having
            DatabaseHelper.TRANSACTION_KEY_ID); // order by
    result.getCount();
    return result;
}

@Override
protected void onPreExecute() {
}

@Override
protected void onPostExecute(Cursor cursor) {
    loadComplete.transactionLoadComplete(cursor);
}
}

Aucun commentaire:

Enregistrer un commentaire