**嗨,我想在历史页面(fragment_historic)上显示sqlite表的元素。我很好地恢复了BD中的元素,但这些元素没有在
中恢复 ViewHolder.trxId.setText (trx.getTransaction_id ());
ViewHolder.amount.setText (trx.getTransaction_amount (). ToString ()); ViewHolder.dateTime.setText (trx.getTransaction_DateTime ()); ViewHolder.cardNumber.setText (trx.getTransaction_cardNumber ());
** TransactionAdapter类**
public class TransactionAdapter extends ArrayAdapter<Transaction>
{
ImageView downloadImageButton;
public TransactionAdapter(Context context, List<Transaction> trx)
{
super(context, 0, trx);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_historic, parent, false);
}
TransactionViewHolder viewHolder = (TransactionViewHolder) convertView.getTag();
if (viewHolder == null)
{
viewHolder = new TransactionViewHolder();
viewHolder.dateTime = (TextView) convertView.findViewById(R.id.tv_trx2_dt);
viewHolder.amount = (TextView) convertView.findViewById(R.id.tv_trx2_amount);
viewHolder.trxId = (TextView) convertView.findViewById(R.id.tv_trx2_id);
viewHolder.cardNumber = (TextView) convertView.findViewById(R.id.tv_trx2_cn);
viewHolder.statusImgView = (ImageView) convertView.findViewById(R.id.tv_trx2_status);
convertView.setTag(viewHolder);
}
//getItem(position) va récupérer l'item [position] de la List<Tweet> tweets
Transaction trx = getItem(position);
System.out.println("TEST..............................position " + position + "amount : " + trx.getTransaction_amount().toString() + "dateTime : " + trx.getTransaction_DateTime() + "cardNumber" + trx.getTransaction_cardNumber());
//il ne reste plus qu'à remplir notre vue
viewHolder.trxId.setText(trx.getTransaction_id());
viewHolder.amount.setText(trx.getTransaction_amount().toString());
viewHolder.dateTime.setText(trx.getTransaction_DateTime());
viewHolder.cardNumber.setText(trx.getTransaction_cardNumber());
if (trx.getTransaction_status() == "APPROVED")
{
downloadImageButton.setImageResource(R.drawable.trx_status_ok);
} else
{
downloadImageButton.setImageResource(R.drawable.trx_status_nok);
}
viewHolder.statusImgView=downloadImageButton;
return convertView;
}
class TransactionViewHolder
{
public TextView dateTime;
public TextView trxId;
public TextView amount;
public TextView cardNumber;
public ImageView statusImgView;
public TextView status;
}
}
public class HistoricFragment extends Fragment
{
PaymentAppDbHelper db;
ListView mListView;
List<Transaction> trx;
// View cellule = new View(getContext());
// Button btn_back_diagonistics;
private FragmentActivity fragmentActivity;
@Override
public void onAttach(Activity activity) {
fragmentActivity=(FragmentActivity) activity;
super.onAttach(activity);
}
public HistoricFragment() {
// Required empty public constructor
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_historic, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
mListView = (ListView) getView().findViewById(R.id.listViewTest);
db= new PaymentAppDbHelper(getContext());
try
{
trx = db.SelectTransactions();
} catch (ParseException e)
{
e.printStackTrace();
}
TransactionAdapter adapter = new TransactionAdapter(getContext(), trx);
mListView.setAdapter(adapter);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
setRetainInstance(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
//Restore the fragment's state here
}
}
@Override`enter code here`
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}
}
<!--fragment_historic-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:background="@color/noir"
tools:context="com.alcineo.demo.androiddemo.fragments.HistoricFragment">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50px"
android:id="@+id/ll_cel_title"
android:layout_centerHorizontal="true"
android:background="@drawable/tablerow_white_bg">
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_dateTime"
android:layout_gravity="center"
android:id="@+id/tv_dateTime"
android:textColor="@color/noir"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="23px"
android:scrollHorizontally="false"
android:textAlignment="center"/>
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_trxID"
android:layout_gravity="center"
android:id="@+id/tv_trxID"
android:layout_marginRight="10dp"
android:textColor="@color/noir"
android:textSize="23px"
android:scrollHorizontally="false"
android:textAlignment="center"/>
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_cardNum"
android:layout_gravity="center"
android:id="@+id/tv_cardNum"
android:textColor="@color/noir"
android:layout_marginRight="20dp"
android:textSize="23px"
android:textAlignment="center"/>
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_amount"
android:layout_gravity="center"
android:id="@+id/tv_amount"
android:layout_marginRight="9dp"
android:textColor="@color/noir"
android:textSize="23px"
android:textAlignment="center"/>
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STATUS"
android:layout_gravity="center"
android:id="@+id/tv_status"
android:layout_marginRight="9dp"
android:textColor="@color/noir"
android:textSize="23px"
android:textAlignment="center"/>
</LinearLayout>
<!--_______________________-->
<!--transactions à afficher-->
<!--_______________________-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_cel_one"
android:tag="ll_row_entries"
android:background="@drawable/tablerow_grey_bg"
android:weightSum="1"
android:layout_below="@+id/ll_cel_title"
android:layout_alignParentStart="true">
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12:04 AM\nNov 16"
android:layout_gravity="center"
android:id="@+id/tv_trx2_dt"
android:textColor="@color/noir"
android:layout_marginLeft="10dp"
android:layout_marginRight="40dp"
android:textSize="20px"
android:textAlignment="center"/>
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123456"
android:layout_gravity="center"
android:id="@+id/tv_trx2_id"
android:layout_marginRight="60dp"
android:textColor="@color/noir"
android:textSize="20px"
android:textAlignment="center"
/>
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="425698******1234"
android:layout_gravity="center"
android:id="@+id/tv_trx2_cn"
android:textColor="@color/noir"
android:layout_marginRight="20dp"
android:textSize="20px"
android:textAlignment="center"
/>
<com.alcineo.demo.androiddemo.utils.fonts.SourceSansProBold
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15.75"
android:layout_gravity="center"
android:id="@+id/tv_trx2_amount"
android:layout_marginRight="30dp"
android:textColor="@color/noir"
android:textSize="20px"
android:textAlignment="center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_trx2_status"
android:src="@drawable/trx_status_ok"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp">
<Button
android:id="@+id/Modifier_historic"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/buttonback_new" />
</LinearLayout>
</RelativeLayout>
<!--activity_main-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/mainLayout"
android:stretchColumns="1"
android:padding="0dp">
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/noir"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/displayedTextView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:visibility="invisible"/>
<ListView
android:id="@+id/listViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
//Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alcineo.demo.androiddemo, PID: 10395
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.alcineo.demo.androiddemo.fragments.HistoricFragment.onViewCreated(HistoricFragment.java:121)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5683)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
答案 0 :(得分:0)
您正在尝试将trx
的数据绑定到您的适配器,但trx
没有数据,这就是您获得空指针异常的原因。我想,你应该检查PaymentAppDbHelper
课程。由于您没有提供有关PaymentAppDbHelper
课程的信息,我无法告诉您究竟是什么问题。
答案 1 :(得分:0)
嗨我稍微修改了你的代码试试
希望它的帮助!
@Override
public View getView(int position, View convertView, ViewGroup parent){
TransactionViewHolder viewHolder = null;
// check View is null or not
if (convertView == null){
//inflating view in convertView object
convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_historic, parent, false);
//creating instance of TransactionViewHolder
viewHolder = new TransactionViewHolder();
//binding ids to views
viewHolder.dateTime = (TextView) convertView.findViewById(R.id.tv_trx2_dt);
viewHolder.amount = (TextView) convertView.findViewById(R.id.tv_trx2_amount);
viewHolder.trxId = (TextView) convertView.findViewById(R.id.tv_trx2_id);
viewHolder.cardNumber = (TextView) convertView.findViewById(R.id.tv_trx2_cn);
viewHolder.statusImgView = (ImageView) convertView.findViewById(R.id.tv_trx2_status);
convertView.setTag(viewHolder);
}else{
viewHolder = (TransactionViewHolder) convertView.getTag();
}
//getItem(position) va récupérer l'item [position] de la List<Tweet> tweets
Transaction trx = getItem(position);
System.out.println("TEST..............................position " + position + "amount : " + trx.getTransaction_amount().toString() + "dateTime : " + trx.getTransaction_DateTime() + "cardNumber" + trx.getTransaction_cardNumber());
//il ne reste plus qu'à remplir notre vue
viewHolder.trxId.setText(trx.getTransaction_id());
viewHolder.amount.setText(trx.getTransaction_amount().toString());
viewHolder.dateTime.setText(trx.getTransaction_DateTime());
viewHolder.cardNumber.setText(trx.getTransaction_cardNumber());
if (trx.getTransaction_status() == "APPROVED"){
downloadImageButton.setImageResource(R.drawable.trx_status_ok);
} else{
downloadImageButton.setImageResource(R.drawable.trx_status_nok);
}
viewHolder.statusImgView=downloadImageButton;
return convertView;
}