从SQLite数据库中输出到列应显示值,但只显示默认的“TextView”。

huangapple go评论61阅读模式
英文:

Output from SQLite database into columns should display values, but just shows default "TextView"

问题

我正试图从我的SQLite数据库中输出三列,并在我的Android应用程序中将它们显示在一个“表格”中。

虽然我可以运行查询并获得正确数量的单元格来填充,但由于某种原因,我的代码没有将查询的值放入其中,因此它始终显示一个在每一行中都只写着“TextView”的表格。

为了做到这一点,我首先在包含所有查询的类中运行查询,并称呼了这个方法getListContents()

然后,我创建了两个布局:

布局1被称为view_player_history.xml,只是一个线性布局中的列表视图。

布局2被称为list_adapter_view.xml,再次是一个线性布局,但这次有三个文本视图来提供我的数据的三列。

然后我创建了三个不同的类:

User类: 在这里,我定义了所有的列并返回相关数据。

public class User {
    private String Year;
    private String Club;
    private String Apps;

    public User(String year, String club, String apps) {
        Year = year;
        Club = club;
        Apps = apps;
    }

    public String getYear() {
        return Year;
    }

    public String getClub() {
        return Club;
    }

    public String getApps() {
        return Apps;
    }
}

ViewPlayerHistoryContents类: 在这里,我访问我的SQL数据库来提取数据并将其添加到要显示的列表中。

public class ViewPlayerHistoryContents extends AppCompatActivity {

    DatabaseAccess myDB;
    ArrayList<User> userList;
    ListView listView;
    User user;

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

        myDB = new DatabaseAccess(this);

        userList = new ArrayList<>();
        Cursor data = myDB.getListContents();
        int numRows = data.getCount();

        while (data.moveToNext()){
            user = new User(data.getString(0), data.getString(1), data.getString(2));
            userList.add(user);
        }

        com.example.transfergame.ThreeClass adapter = new com.example.transfergame.ThreeClass(this, R.layout.list_adapter_view, userList);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }
}

ThreeClass类: 在这里,我试图将其分配给不同的文本视图,以便在我的列中显示。

public class ThreeClass extends ArrayAdapter<User> {

    private LayoutInflater mInflator;
    private ArrayList<User> users;
    private int mViewResourceId;

    public ThreeClass (Context context, int textViewResourceId, ArrayList<User> users){
        super(context, textViewResourceId, users);
        this.users = users;
        mInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mViewResourceId = textViewResourceId;
    }

    public View getView(int position, View convertView, ViewGroup parents) {
        convertView = mInflator.inflate(mViewResourceId,null);

        User user = users.get(position);

        TextView Year = (TextView) convertView.findViewById(R.id.textYear);
        TextView Club = (TextView) convertView.findViewById(R.id.textClub);
        TextView Apps = (TextView) convertView.findViewById(R.id.textApps);

        return convertView;
    }
}

但是由于某种原因,似乎没有将值分配给它们。我知道其余部分都工作正常,因为正如我提到的,它总是在我的ListView中输出正确数量的行。它只是不更新文本。因此,也没有错误消息,因为没有什么失败了。

是否有拼写错误阻止了将值分配给每个TextView?

英文:

I'm trying to output three columns from my SQLite database and have them show on a "table" in my Android app.

While I can run my query and get the correct number of cells to populate, for some reason my code doesn't put the values from the query in them and so it keeps displaying a table that just says "TextView" in every row.

To do this I first run a query in my class with all my queries and have called this method getListContents().

I am then creating two layouts:

Layout 1 is called view_player_history.xml and is just a list view in a linear layout.

Layout 2 is called list_adapter_view.xml and is again a linear layout, but this time there are three text views's to give the three columns for my data.

I then create three different classes:

User Class: Here I am defining all my columns and returning the relevant data.

public class User {
private String Year;
private String Club;
private String Apps;

public User(String year, String club, String apps) {
    Year = year;
    Club = club;
    Apps = apps;
}

public String getYear() {
    return Year;
}

public String getClub() {
    return Club;
}

public String getApps() {
    return Apps;
}
}

ViewPlayerHistoryContents class: Here I'm accessing my SQL database to pull the data and add it to the list to display

public class ViewPlayerHistoryContents extends AppCompatActivity {

DatabaseAccess myDB;
ArrayList&lt;User&gt; userList;
ListView listView;
User user;

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

    myDB = new DatabaseAccess(this);

    userList = new ArrayList&lt;&gt;();
    Cursor data = myDB.getListContents();
    int numRows = data.getCount();

    while (data.moveToNext()){
        user = new User(data.getString(0), data.getString(1), data.getString(2));
        userList.add(user);
    }

    com.example.transfergame.ThreeClass adapter = new com.example.transfergame.ThreeClass(this, R.layout.list_adapter_view, userList);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);


}
}

Three Class: Here I am trying to assign it to the different text views so it displays in my columns

public class ThreeClass extends ArrayAdapter&lt;User&gt; {

private LayoutInflater mInflator;
private ArrayList&lt;User&gt; users;
private int mViewResourceId;

public ThreeClass (Context context, int textViewResourceId, ArrayList&lt;User&gt; users){
    super(context, textViewResourceId, users);
    this.users = users;
    mInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mViewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parents) {
    convertView = mInflator.inflate(mViewResourceId,null);

    User user = users.get(position);

    TextView Year = (TextView) convertView.findViewById(R.id.textYear);
    TextView Club = (TextView) convertView.findViewById(R.id.textClub);
    TextView Apps = (TextView) convertView.findViewById(R.id.textApps);

    return convertView;
}
}

But for some reason, it doesn't seem to be assigning the values. I know the rest of it is working fine because as I mentioned it always outputs the correct number of rows in my ListView. It just doesn't update the text. Because of this, there is also no error message, as nothing fails.

Is there a typo that is stopping the values being assigned to each TextView?

答案1

得分: 1

这是因为您实际上还没有将这些值分配给每个 TextView。您只是初始化了这些变量。

您需要添加以下代码:

Year.setText(user.getYear());
Club.setText(user.getClub());
Apps.setText(user.getApps());

这样您就可以为 Year、Club 和 Apps 设置文本。

英文:

That's because you haven't actually assigned the values to each of your TextViews. You've just initialized the variables.

You need to add

Year.setText(user.getYear());
Club.setText(user.getClub());
Apps.setText(user.getApps());

so you set the text for Year, Club and Apps.

huangapple
  • 本文由 发表于 2020年6月5日 21:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/62215976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定