basic listview

hwazgwia  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(133)

我是安卓、java和安卓studio的新手,试图向一个运行正常的应用程序添加一些基本报告。我用示例代码找到了十几种可能的解决方案,但每次都是由于我缺乏基本知识。昨天,我在答案1中复制了莱昂纳多·维洛佐的代码。这看起来很简单,我决定再试一次。然而,我得到了一些错误,对于一个有经验的程序员来说,修复这些错误可能非常简单。
我们的目标是显示一些不同距离的比赛中没有完成的选手的列表。非常感谢你的帮助!我是作为一个非营利的越野比赛组织的志愿者来做这件事的。
我收到的一些错误消息是“方法未从其超类重写方法”、“无法解析符号'dnfs'”、无法解析'dnfsadapter'中的方法'getitem'。
报告_dnfs.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/listview_dnfs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:dividerHeight="1dp" />
</LinearLayout>

row_dnf.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/textview_epreuve"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Epreuve"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@+id/textview_dossard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Dossard"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

添加到dbhelper.java:

public ArrayList<DNFs> getDNFs(){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor =  db.rawQuery( "SELECT Epreuve, Dossard FROM r3_DNFs", null);
    ArrayList<DNFs> list_dnfs = new ArrayList<DNFs>();
    while(cursor.moveToNext()){
        DNFs racer = new DNFs();
        racer.set_epreuve(cursor.getString(0));
        racer.set_dossard(cursor.getInt(1));
        list_dnfs.add(racer);
    }
    cursor.close();
    db.close();
    return list_dnfs;
}

已创建dnfsadapter.java

public class DNFsAdapter extends ArrayAdapter<DNFs> {
    // View lookup cache
    private static class ViewHolder {
        TextView epreuve;
        TextView dossard;
    }
    public DNFsAdapter(Context context, ArrayList<DNFs> list_dnfs) {
        super(context, R.layout.row_dnf, list_dnfs);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        DNFs racer = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag
        if (convertView == null) {
            // If there's no view to re-use, inflate a brand new view for row
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.row_dnf, parent, false);
            viewHolder.epreuve = (TextView) convertView.findViewById(R.id.textview_epreuve);
            viewHolder.dossard = (TextView) convertView.findViewById(R.id.textview_dossard);
            // Cache the viewHolder object inside the fresh view
            convertView.setTag(viewHolder);
        } else {
            // View is being recycled, retrieve the viewHolder object from tag
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // Populate the data from the data object via the viewHolder object
        // into the template view.
        viewHolder.epreuve.setText("id:" + racer.get_epreuve());
        viewHolder.dossard.setText("name: " + racer.get_dossard());
        // Return the completed view to render on screen
        return convertView;
    }
}

viewdnfs.java

public class ViewDNFs extends BaseActivity {
    DNFsAdapter adapter;
    private DBHelper my_db;

    @Override
    private void initView(){
        // get database
        my_db = new DBHelper(context);
        // retrieve DNF racers from database
        ArrayList<DNFs> racersList = my_db.getDNFs();
        // create the adapter
        DNFsAdapter adapter = new DNFsAdapter(this, racersList);
        // Attach the adapter to a ListView
        ListView listView = (ListView) findViewById(R.id.listview_dnfs);
        //set adapter
        listView.setAdapter(adapter);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题