2017-09-03 08:04:18 +02:00
|
|
|
package org.schabi.newpipe.fragments.list.search;
|
2016-08-02 15:06:02 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.MatrixCursor;
|
2017-01-11 11:06:24 +01:00
|
|
|
import android.support.v4.widget.ResourceCursorAdapter;
|
2016-08-02 15:06:02 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*
|
2016-09-12 00:33:11 +02:00
|
|
|
* Created by Christian Schabesberger on 02.08.16.
|
|
|
|
*
|
|
|
|
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
|
|
|
|
* SuggestionListAdapter.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
2016-08-02 15:06:02 +02:00
|
|
|
*/
|
|
|
|
|
2017-01-11 11:06:24 +01:00
|
|
|
/**
|
|
|
|
* {@link ResourceCursorAdapter} to display suggestions.
|
|
|
|
*/
|
|
|
|
public class SuggestionListAdapter extends ResourceCursorAdapter {
|
2016-08-02 15:06:02 +02:00
|
|
|
|
2017-01-11 11:06:24 +01:00
|
|
|
private static final String[] columns = new String[]{"_id", "title"};
|
|
|
|
private static final int INDEX_ID = 0;
|
|
|
|
private static final int INDEX_TITLE = 1;
|
2016-08-02 15:06:02 +02:00
|
|
|
|
|
|
|
|
2017-01-11 11:06:24 +01:00
|
|
|
public SuggestionListAdapter(Context context) {
|
|
|
|
super(context, android.R.layout.simple_list_item_1, null, 0);
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
2017-01-11 11:06:24 +01:00
|
|
|
ViewHolder viewHolder = new ViewHolder(view);
|
|
|
|
viewHolder.suggestionTitle.setText(cursor.getString(INDEX_TITLE));
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-11 11:06:24 +01:00
|
|
|
/**
|
|
|
|
* Update the suggestion list
|
|
|
|
* @param suggestions the list of suggestions
|
|
|
|
*/
|
2016-08-02 15:06:02 +02:00
|
|
|
public void updateAdapter(List<String> suggestions) {
|
2017-01-11 11:06:24 +01:00
|
|
|
MatrixCursor cursor = new MatrixCursor(columns, suggestions.size());
|
2016-08-02 15:06:02 +02:00
|
|
|
int i = 0;
|
2017-01-11 11:06:24 +01:00
|
|
|
for (String suggestion : suggestions) {
|
|
|
|
String[] columnValues = new String[columns.length];
|
|
|
|
columnValues[INDEX_TITLE] = suggestion;
|
|
|
|
columnValues[INDEX_ID] = Integer.toString(i);
|
|
|
|
cursor.addRow(columnValues);
|
2016-08-02 15:06:02 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
changeCursor(cursor);
|
|
|
|
}
|
|
|
|
|
2017-01-11 11:06:24 +01:00
|
|
|
/**
|
|
|
|
* Get the suggestion for a position
|
|
|
|
* @param position the position of the suggestion
|
|
|
|
* @return the suggestion
|
|
|
|
*/
|
2016-08-02 15:06:02 +02:00
|
|
|
public String getSuggestion(int position) {
|
2017-01-11 11:06:24 +01:00
|
|
|
return ((Cursor) getItem(position)).getString(INDEX_TITLE);
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 21:32:04 +02:00
|
|
|
@Override
|
|
|
|
public CharSequence convertToString(Cursor cursor) {
|
|
|
|
return cursor.getString(INDEX_TITLE);
|
|
|
|
}
|
|
|
|
|
2016-08-02 15:06:02 +02:00
|
|
|
private class ViewHolder {
|
2017-01-11 11:06:24 +01:00
|
|
|
private final TextView suggestionTitle;
|
|
|
|
private ViewHolder(View view) {
|
2017-09-03 08:04:18 +02:00
|
|
|
this.suggestionTitle = view.findViewById(android.R.id.text1);
|
2017-01-11 11:06:24 +01:00
|
|
|
}
|
2016-08-02 15:06:02 +02:00
|
|
|
}
|
|
|
|
}
|