ListView中的数据来自SQLite //适配器错误

时间:2018-03-31 10:29:38

标签: android sqlite android-layout listview android-sqlite

TachesFragment.java

package com.example.johnwalls.projet;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class TachesFragment extends Fragment {
    private DB base;
    private ListView sp ;

    public TachesFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_taches, container, false);

        TextView text2 =  view.findViewById(R.id.ta);//Find textview Id
        TextView t1 =     view.findViewById(R.id.t1);
        TextView de1=     view.findViewById(R.id.de1);
        TextView em1=     view.findViewById(R.id.em1);
        String getArgument = getArguments().getString("main");
        text2.setText(getArgument);


        base=new DB(getActivity());

        text2.setText(getArgument);

        sp=(ListView) view.findViewById(R.id.ta);
        ArrayList<Mission> empList=base.MissionList();
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,android.R.id.text1,empList);
        sp.setAdapter(adapter);





        return view;
    }

}

fragment_taches

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"


    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:gravity="bottom"
    android:orientation="vertical"
    android:background="@drawable/ee63"

    tools:context="com.example.johnwalls.projet.TachesFragment">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"

        android:id="@+id/ta"

        />


</android.support.v4.widget.DrawerLayout>

simple_list_item_1管理

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"

    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/t1"
        android:textSize="20dp"
        android:text=""
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/de1"
        android:textSize="10dp"
        android:text=""
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/em1"
        android:textSize="10dp"
        android:text=""
        />


</LinearLayout>

你想要实现什么目标你想要什么?你得到了什么(包括错误信息)你还尝试了什么?您认为是什么造成的?为什么你需要为它提出一个新问题?为什么你的问题与其他类似的问题不同?

2 个答案:

答案 0 :(得分:1)

我相信您的问题是您告诉ArrayAdapter期望一个String对象数组(ArrayAdapter<String>),但您提供了一系列 Mission 对象每ArrayList<Mission> emplist=base.MissionList();

即。你有

    ArrayList<Mission> empList=base.MissionList(); 
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,android.R.id.text1,empList);

相反,我认为你需要: -

    ArrayList<Mission> empList=base.MissionList(); //<<<< NOT CHANGED
    ArrayAdapter<Mission> adapter=new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,android.R.id.text1,empList);
  • 请注意,任务的toString方法将用于提供列表的数据,如果您没有覆盖toString方法,结果可能不是您所期望的。例如
  

com.example.johnwalls.projet.Mission@546789

在这种情况下,您应该创建一个toString方法来返回一个合适的字符串。

作为示例(不能使用),这是一个带有重写toString方法的TaskData类: -

public class TaskData {

    private long id;
    private String type, name, owner, expiration;

    public TaskData(long id, String type, String name, String owner, String expiration) {
        this.id = id;
        this.type = type;
        this.name = name;
        this.owner = owner;
        this.expiration = expiration;
    }

    //<<<< Oreridden toString Method Starts here >>>>
    public String toString() {
        return " Typ: " + type + " Name: " + name + " Owner: " + owner + " Until: " + expiration;
    }
    //<<<< End >>>>

    public long getId(){return id;}
    public String getType() {return this.type;}
    public String getName() {return this.name;}
    public String getOwner() {return this.owner;}
    public String getExpiration() {return  this.expiration;}
}
  • 重写的toString方法返回一个字符串,该字符串结合了类型名称,所有者和到期日期。例如
      

    类型:测试名称: Fred 所有者: Bert 直到: 31/12/2018

答案 1 :(得分:0)

    LoveData beanClass = arrayList.get(position);
    Log.e("DisplayPhotolistAdapter","beanClass:"+beanClass);

    tv_date.setText(beanClass.getLOVE_DATE());
    tv_total_percent.setText(beanClass.getPrcentage()+"%");
    tv_hername.setText(beanClass.getHername());
    tv_hisname.setText(beanClass.getHisname());
    tv_Actvity.setText(beanClass.getActvity()+":");
相关问题