Android Sqlite和Multicolumns问题

时间:2017-04-12 12:18:22

标签: android android-layout android-sqlite

我有sqlite的简单项目(3列的发送和反向信息)。它运作良好。实际上我尝试将这些信息放到带有3列适配器的listView中 - >它看起来像这样: enter image description here

但是当我运行我的应用程序时,我的适配器重复相同的三次,之后它将进入下一列并进行相同的操作。 enter image description here

我想要做的是填充第nr 1列的textView2信息,第2列的textView3 ...

这是用于构建ArrayList并调用Listview和适配器的代码:

private void populateListView() {
    Cursor data = mDatabaseHelper.getData();
    ArrayList<String> listData = new ArrayList<>();
    while (data.moveToNext()) {
        listData.add(data.getString(1));
        listData.add(data.getString(2));
        listData.add(data.getString(3));
    }

    ListDataAdapter adapter = new ListDataAdapter(this, R.layout.adapter_view_layout, listData);
    mListView.setAdapter(adapter);

和适配器:

import android.content.Context;
import android.support.annotation.LayoutRes;
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.TextView;

import java.util.ArrayList;



class ListDataAdapter extends ArrayAdapter<String> {

private static final String TAG = "ListDataAdapter";
    private Context mContext;
    int mResource;

    public ListDataAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<String> objects) {
        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String name = getItem(position).toString();
        String sex = getItem(position).toString();
        String date = getItem(position).toString();


        LayoutInflater inflater =  LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource,parent, false);


        TextView tvName = (TextView) convertView.findViewById(R.id.textView1);
        TextView tvSex = (TextView) convertView.findViewById(R.id.textView2);
        TextView tvDate = (TextView) convertView.findViewById(R.id.textView3);

        tvName.setText(name);
        tvDate.setText(date);
        tvSex.setText(sex);

        return convertView;
    }
}

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

嗯,您在三个String上设置相同的TextView,这是预期的。 因此,要实现您的目标,您必须创建一个包含这3列的新类。我们将其命名为RowModel。因此,您不必使用String,而是使用新课程。像这样:

private void populateListView() { 
    Cursor data = mDatabaseHelper.getData();
    ArrayList<RowModel> listData = new ArrayList<>();
    while (data.moveToNext()) {
        listData.add(new RowModel(data.getString(1), data.getString(2), data.getString(3));
    } 

    ListDataAdapter adapter = new ListDataAdapter(this, R.layout.adapter_view_layout, listData);
    mListView.setAdapter(adapter);
}

然后您的适配器将是这样的:

class ListDataAdapter extends ArrayAdapter<RowModel> {

    @NonNull 
    @Override 
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        RowModel model = getItem(position);
        String name = model.getName();
        String sex = model.getSex();
        String date = model.getDate();

        // populate TextViews
    }
}