Android - 如何在项目中使用固定部分ListView库?

时间:2015-09-15 11:04:04

标签: android android-listview android-adapter android-library

如何在我的项目中使用此库?

Pinned Section ListView

我从sqlite获取数据并在适配器中设置。

StartActivity.java:

public class StartActivity extends AppCompatActivity {

    public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
    public static final String DIR_DATABASE = DIR_SDCARD + "/Android/data/";
    public ArrayList<StructNote> notes = new ArrayList<StructNote>();
    public ArrayAdapter adapter;
    public static String PACKAGE_NAME;
    DB db = new DB(StartActivity.this);
    public Cursor cursor;
    public SQLiteDatabase sql;


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

        PACKAGE_NAME = getApplicationContext().getPackageName();
        File file = new File(DIR_DATABASE + PACKAGE_NAME + "/newTest");
        file.mkdirs();
        db.GetPackageName(PACKAGE_NAME);
        db.CreateFile();
        try {
            db.CreateandOpenDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        sql = db.openDataBase();

        final ListView lstContent = (ListView) findViewById(R.id.lstContent);
        adapter = new AdapterNote(notes);
        lstContent.setAdapter(adapter);

        populateListView();

    }
    public void populateListView() {

            try {
                cursor = sql.rawQuery("SELECT ContentID as CONID," +
                        " Pav as Pav," +
                        " Title as WTitle," +
                        " Flag as Flag" +
                        " FROM BookPage" +
                        " WHERE ArticleID = '" + 61799 + "'order by PageID", null);
                if (cursor != null) {
                    if (cursor.moveToFirst()) {
                        do {

                            StructNote note = new StructNote();
                            note.CONID = cursor.getInt(cursor.getColumnIndex("CONID"));
                            note.WTitle = cursor.getString(cursor.getColumnIndex("WTitle"));
                            note.Flag = cursor.getInt(cursor.getColumnIndex("Flag"));
                            notes.add(note);
                        } while (cursor.moveToNext());
                    }

                    adapter.notifyDataSetChanged();
                }
            } catch (Exception e) {
                Log.i("xxx", "You have an error");
            } finally {
                cursor.close();
            }
    }
}

AdapterNote.java

public class AdapterNote extends ArrayAdapter<StructNote> {

    public AdapterNote(ArrayList<StructNote> array) {
        super(G.context, R.layout.dapter_notes, array);
    }

    public static class ViewHolder {

        public TextView txtTitle;

        public ViewHolder(View view) {
            txtTitle = (TextView) view.findViewById(R.id.txtTitle);
        }

        public void fill(ArrayAdapter<StructNote> adapter, StructNote item, int position) {
            txtTitle.setText(item.WTitle);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        StructNote item = getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.dapter_notes, parent,
                    false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.fill(this, item, position);
        return convertView;
    }
}

activity_start.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/LayoutTest">

        <ListView
            android:id="@+id/lstContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@null"
            tools:listitem="@layout/dapter_notes"></ListView>
    </RelativeLayout>


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

dapter_notes.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="match_parent"
    android:orientation="vertical"
    android:padding="8dip"
    android:id="@+id/mainLayout">

    <LinearLayout
        android:id="@+id/LayoutOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_marginBottom="8dip">

        <LinearLayout
            android:id="@+id/LayoutTwo"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1">

            <TextView
                android:id="@+id/txtTitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Title"
                android:maxLength="100"
                android:singleLine="true"
                android:ellipsize="end"
                android:gravity="right"
                android:textColor="#000000"/>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

StructNote.java

public class StructNote {
    int CONID;
    String WTitle;
    int Flag;
}

我阅读了这个图书馆的指南,但我无法使用它。

某些行的 note.Flag 的值为0,其他某些1我需要固定为0。

0 个答案:

没有答案