So I am trying to use a custom layout for my notepad part of my app, but I just can't seem to be able to make it appear, below I'll display the notepad activity itself, the layout file and the adapter class that I made to inflate it in the hopes that I can get help to solve it.
The Notepad Activity:
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class Notepad extends AppCompatActivity {
Adapter adapter;
List<Note> notes;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notepad);
Toolbar toolbar2 = findViewById(R.id.toolbar2);
setSupportActionBar(toolbar2);
DataBaseHelper db = new DataBaseHelper(this);
notes = db.getNotes();
recyclerView = findViewById(R.id.listOfnotes);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new Adapter(this, notes);
recyclerView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId() == R.id.add){
startActivity(new Intent(this, AddNote.class));
}
return super.onOptionsItemSelected(item);
}
}
The layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAFAFA"
android:padding="5dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
app:cardElevation="0dp"
app:contentPadding="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="83dp"
android:layout_height="61dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/constraintLayout"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/nTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Sample Note Title"
android:textAlignment="viewStart"
android:textAllCaps="false"
android:textColor="#android:color/background_dark"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/nDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="created : 7/31/2019"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/nTitle" />
<TextView
android:id="#+id/nTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="08:02"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/nDate"
app:layout_constraintTop_toBottomOf="#+id/nTitle" />
<TextView
android:id="#+id/listId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="TextView"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/nTitle"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
The adapter class:
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
LayoutInflater inflater;
List<Note> notes;
Adapter(Context context, List<Note> notes){
this.inflater = LayoutInflater.from(context);
this.notes = notes;
}
#NonNull
#Override
public Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View view = inflater.inflate(R.layout.custom_list_view, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Adapter.ViewHolder viewHolder, int i) {
String title = notes.get(i).getTitle();
String date = notes.get(i).getDate();
String time = notes.get(i).getTime();
Log.d("Title", "onBindViewHolder: Title -> " + title);
viewHolder.nTitle.setText(title);
viewHolder.nDate.setText(date);
viewHolder.nTime.setText(time);
}
#Override
public int getItemCount() {
return notes.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
TextView nTitle, nDate, nTime;
public ViewHolder(#NonNull View itemView) {
super(itemView);
nTitle = itemView.findViewById(R.id.nTitle);
nDate = itemView.findViewById(R.id.nDate);
nTime = itemView.findViewById(R.id.nTime);
}
}
}
Notepad Activity layout file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Notepad">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#color/colorPrimary" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listOfnotes"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#cd0190"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar2"
app:layout_constraintVertical_bias="0.0" />
</androidx.appcompat.widget.LinearLayoutCompat>
Related
I fetch data from database and put it to RecyclerView. My database arrives 4 items I see it with log debug then I send it to my adapter. However I see only first item on my screen.
my adapter ListArticleAdapter
package uz.sherdevs.alishernavoiy.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import uz.sherdevs.alishernavoiy.R;
import uz.sherdevs.alishernavoiy.model.Article;
public class ListArticleAdapter extends RecyclerView.Adapter<ListArticleAdapter.MyViewHolder> {
private Context context;
private List<Article> articleList;
public ListArticleAdapter(Context context, List<Article> articleList) {
this.context = context;
this.articleList = articleList;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_list, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.tv_title.setText(articleList.get(position).getTitle());
}
#Override
public int getItemCount() {
// Log.d("list", "" + articleList.size());
return articleList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_title;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
tv_title = itemView.findViewById(R.id.article_title);
}
}
}
MainActivity
package uz.sherdevs.alishernavoiy;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import uz.sherdevs.alishernavoiy.adapter.ListArticleAdapter;
import uz.sherdevs.alishernavoiy.database.SQLiteDBHelper;
import uz.sherdevs.alishernavoiy.model.Article;
public class MainActivity extends AppCompatActivity {
private RecyclerView articleRecyclerView;
private ListArticleAdapter adapter;
private List<Article> articleList;
private SQLiteDBHelper dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
articleRecyclerView = (RecyclerView)findViewById(R.id.articles_recyclerview);
dbHelper = new SQLiteDBHelper(this);
articleList = dbHelper.getArticles();
// for (Article article : articleList) {
// Log.d("list", article.getTitle());
// }
articleRecyclerView.setHasFixedSize(true);
adapter = new ListArticleAdapter(this, articleList);
// Log.d("list", articleList.toString());
articleRecyclerView.setAdapter(adapter);
articleRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/background_icon"
>
<ImageView
android:src="#drawable/background_regtangle_green"
android:scaleType="centerCrop"
android:id="#+id/circle_background"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintWidth_percent="1"
app:layout_constraintVertical_bias="0"
/>
<TextView
android:text="Dostonlar"
android:textColor="#FFDF6A"
android:textSize="60sp"
android:textStyle="bold"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#id/circle_background"
app:layout_constraintLeft_toLeftOf="#id/circle_background"
app:layout_constraintRight_toRightOf="#id/circle_background"
app:layout_constraintBottom_toBottomOf="#id/circle_background"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintVertical_bias="0.3"
android:fontFamily="#font/myfont"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/articles_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="?attr/actionBarSize">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
item_list.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"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardview1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.15"
app:layout_constraintWidth_percent="0.8"
app:cardCornerRadius="20dp"
app:layout_constraintVertical_bias="0.23"
app:cardElevation="20dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/article_title"
android:textSize="45sp"
android:textStyle="bold"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.6"
android:gravity="center"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
I got 4 Objects from my database with command
articleList = dbHelper.getArticles();
However I see only first one on the screen.
Moreover as you see I loged in my adapter
#Override
public int getItemCount() {
// Log.d("list", "" + articleList.size());
return articleList.size();
}
That size also shows 4.
In item_list.xml, change android:layout_height="match_parent" with android:layout_height="wrap_content"
I have created a listview that has many info in one list. I tired to implement the search view in the listview adapter but the list do not get filtered.
Below is my source code for that Activity.
DictionaryAdapter.java
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.CursorAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.personaldictionary.R;
import com.example.personaldictionary.database.DictionaryContract;
public class DictionaryAdapter extends CursorAdapter {
private static final String LOG_TAG = "DictionaryAdapter";
public DictionaryAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(LOG_TAG,"newView called cursor: "+cursor+" context: "+context+" parent: "+parent);
return LayoutInflater.from(context).inflate(R.layout.word_list, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(LOG_TAG,"bindView called cursor: "+cursor+" context: "+context+" view: "+view);
view.findViewById(R.id.container).setAnimation(AnimationUtils.loadAnimation(context,R.anim.fade_scale_animation));
TextView wordTextView = (TextView) view.findViewById(R.id.word_text_list);
TextView wordDefTextView = (TextView) view.findViewById(R.id.word_definition_list);
TextView wordEtyloTextView = (TextView) view.findViewById(R.id.word_etymologies_list);
int wordColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD);
int defColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION);
int etyloColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES);
String word = cursor.getString(wordColumnIndex);
String wordDef = cursor.getString(defColumnIndex);
String wordEtylo = cursor.getString(etyloColumnIndex);
wordTextView.setText(word);
wordDefTextView.setText(wordDef);
wordEtyloTextView.setText(wordEtylo);
}
}
WordListActivity.java
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuItemCompat;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import androidx.appcompat.widget.SearchView;
import android.widget.Toast;
import com.example.personaldictionary.adapter.DictionaryAdapter;
import com.example.personaldictionary.database.DictionaryContract;
import com.example.personaldictionary.database.DictionaryDbHelper;
public class WordListActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String LOG_TAG = "WordListActivity";
DictionaryAdapter mAdapter;
DictionaryDbHelper mDbHelper;
private static final int WORD_LOADER = 0;
private String wordString;
private String wordDefString;
private String wordEtyloString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_list);
ListView wordListView = (ListView) findViewById(R.id.word_list);
View emptyView = findViewById(R.id.empty_view);
wordListView.setEmptyView(emptyView);
Log.d(LOG_TAG,"productListView.setEmptyView(emptyView)");
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras!=null){
wordString = intent.getStringExtra("word");
wordDefString = intent.getStringExtra("wordDefinition");
wordEtyloString = intent.getStringExtra("wordEtylo");
Log.d("WordListActivity","wordString: "+wordString+" wordDefString: "+wordDefString+" wordEtyloString: "+wordEtyloString);
insertWord(wordString,wordDefString,wordEtyloString);
}
mAdapter = new DictionaryAdapter(this,null);
wordListView.setAdapter(mAdapter);
getSupportLoaderManager().initLoader(WORD_LOADER, null,this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.search,menu);
MenuItem search_item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) search_item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
WordListActivity.this.mAdapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
public void insertWord(String word, String word_def, String word_etylo){
Uri newUri = null;
Log.d(LOG_TAG,"insertWord() word: "+word+" word_def: "+word_def+" word_et: "+word_etylo);
ContentValues values = new ContentValues();
values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD,word);
values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,word_def);
values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES,word_etylo);
Log.d(LOG_TAG,"DictionaryContract.DictionaryEntry.CONTENT_URI: "+DictionaryContract.DictionaryEntry.CONTENT_URI);
Log.d(LOG_TAG,"values: "+values);
newUri = getContentResolver().insert(DictionaryContract.DictionaryEntry.CONTENT_URI, values);
//mAdapter.swapCursor(getAllItems());
//mAdapter.swapCursor(getAllItems());
if (newUri == null) {
Toast.makeText(WordListActivity.this, "Word insert error ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(WordListActivity.this, "Word insert Success", Toast.LENGTH_SHORT).show();
}
}
private Cursor getAllItems(){
Log.d(LOG_TAG,"getAllItems inside");
String[] projection = {DictionaryContract.DictionaryEntry._ID,
DictionaryContract.DictionaryEntry.COLUMN_WORD,
DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,
DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES};
Cursor cursor = getContentResolver().query(DictionaryContract.DictionaryEntry.CONTENT_URI,projection,null,null,null);
Log.d(LOG_TAG,"getAllItems: "+cursor);
return cursor;
}
#NonNull
#Override
public Loader<Cursor> onCreateLoader(int id, #Nullable Bundle args) {
Log.d(LOG_TAG,"onCreateLoader called");
String[] projection = {DictionaryContract.DictionaryEntry._ID,
DictionaryContract.DictionaryEntry.COLUMN_WORD,
DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,
DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES,};
return new CursorLoader(this, DictionaryContract.DictionaryEntry.CONTENT_URI,projection,null,null,null);
}
#Override
public void onLoadFinished(#NonNull Loader<Cursor> loader, Cursor data) {
Log.d(LOG_TAG,"onLoadFinished called data: "+data);
mAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(#NonNull Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
activity_word_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WordListActivity"
android:background="#color/white"
android:id="#+id/root_layout">
<ListView
android:id="#+id/word_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_input" />
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/empty_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-medium"
android:paddingTop="16dp"
android:text="#string/empty_view_title_text"
android:textAppearance="?android:textAppearanceMedium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="#+id/empty_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/empty_title_text"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:paddingTop="8dp"
android:text="#string/empty_view_subtitle_text"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#A2AAB0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.528"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.548" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
word_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#fff">
<RelativeLayout
android:id="#+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:background="#drawable/card_bg"
android:paddingBottom="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:id="#+id/word_text_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:textColor="#color/title_text_color"
android:textStyle="bold" />
<TextView
android:id="#+id/word_definition_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/word_text_list"
android:layout_alignStart="#+id/word_text_list"
android:layout_alignLeft="#id/word_text_list"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:textColor="#color/content_text_color" />
<TextView
android:id="#+id/word_etymologies_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/word_definition_list"
android:layout_alignStart="#+id/word_definition_list"
android:layout_alignLeft="#id/word_definition_list"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:textColor="#color/content_text_color" />
</RelativeLayout>
<ImageView
android:id="#+id/word_audio"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/ic_audio"
app:layout_constraintEnd_toStartOf="#+id/container"
app:layout_constraintStart_toStartOf="#+id/container"
app:layout_constraintTop_toTopOf="#+id/container"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search_gray_24dp"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always"/>
</menu>
This is how the view is. I want to filter the text in blue through searchview. Could anyone suggest how to do it?
You should filter list and call notifyDataSetChanged in adapter.
Example you have list ListA which contains ListA = {A,B,C}
You filter listA ( you search for item B ) FilteredList = {B}
AdapterSource = FilteredList
adapter.notifyDataSetChanged()
If you clear search view
AdapterSource = ListA
I am currently trying to implement a custom listView for my Fragment, but when i try to add the item to the ListView it doesn't show up, and i get no exception or anything.
What i'm trying to do is to create an object with a name and a number by typing, both of these things work, but it seems like i did a mistake with the listView
Here is the Fragment with the ListView
package com.example.myapplicationteeeeeeeeeest.ui.BShoppingRecipe;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.example.myapplicationteeeeeeeeeest.R;
import java.util.ArrayList;
import java.util.List;
public class ShoppingListTab extends ShoppingRecipe {
private EditText inputIngredient;
private EditText inputQuantity;
private Button addButton;
private ListView listView;
private ShoppingListAdapter adapterListView;
private ArrayList<ShoppingList> ShoppingList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate( R.layout.tab_fragment_one, container,false );
listView = (ListView) view.findViewById( R.id.shoppingList_listView );
addButton = (Button) view.findViewById( R.id.shoppingTab_button);
inputQuantity = (EditText) view.findViewById( R.id.quantity_editText );
inputIngredient = (EditText) view.findViewById( R.id.ingredient_editText );
ShoppingList = new ArrayList<>();
adapterListView = new ShoppingListAdapter( getActivity(),ShoppingList );
listView.setAdapter( adapterListView );
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ShoppingList.remove(position);
adapterListView.notifyDataSetChanged();
return true;
}
});
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ingredient = inputIngredient.getText().toString().trim();
String quantitiy = inputQuantity.getText().toString().trim();
if (!ingredient.isEmpty() && !quantitiy.isEmpty()) {
ShoppingList ShoppingListItem = new ShoppingList(ingredient, quantitiy);
ShoppingList.add(ShoppingListItem);
adapterListView.notifyDataSetChanged();
inputIngredient.setText("");
inputQuantity.setText("");
}
}
});
return view;
}
}
The Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/ingredient_editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="#string/shopping_list_hint_artikel"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/quantity_editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="#string/shopping_list_hint_stückzahl"
android:inputType="number"
app:layout_constraintEnd_toStartOf="#+id/shoppingTab_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ingredient_editText"
app:layout_constraintHorizontal_bias="0.0"/>
<Button
android:id="#+id/shoppingTab_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/shopping_list_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/ingredient_editText" />
<ListView
android:id="#+id/shoppingList_listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/quantity_editText"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The Costume ListAdapter
package com.example.myapplicationteeeeeeeeeest.ui.BShoppingRecipe;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.myapplicationteeeeeeeeeest.R;
import java.util.ArrayList;
public class ShoppingListAdapter extends ArrayAdapter<ShoppingList> {
private ArrayList<ShoppingList> shoppingList;
private Context context;
public ShoppingListAdapter(Context context, ArrayList<ShoppingList> taskList) {
super(context, R.layout.shopping_list_item);
this.shoppingList = taskList;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflater.inflate(R.layout.shopping_list_item, null);
}
ShoppingList taskItem = shoppingList.get(position);
if(taskItem != null){
TextView taskItemText = v.findViewById(R.id.shoppingList_Ingredient);
TextView taskItemDate = v.findViewById(R.id.shoppingList_Quantity);
taskItemText.setText(taskItem.getTask());
taskItemDate.setText(taskItem.getNumber());
}
return v;
}
}
And the Layout for the Item
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/shoppingList_Ingredient"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#+id/shoppingList_Quantity"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/shoppingList_Quantity"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
Try adding this method in your adapter class:
#Override
public int getCount() {
return shoppingList.size();
}
And also try not to use same name for your list variable and model class.
Hope this helps!
Hello I am developing an app that fetches data from the server the code is shown below
XML:-
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ImageQuiz">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="8dp"
app:title="QUIZ"
app:titleTextColor="#fff" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e2e2e2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="8dp"
app:title="QUIZ"
app:titleTextColor="#fff"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer these questions"
android:textSize="10dp"
android:textAlignment="center"
android:padding="5dp"
android:textColor="#fff"
android:background="#05af43"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recylcerViewImage"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Submit"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textColor="#585858"
android:background="#drawable/rounded_corner"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
ImageQuizAdapter.java
package com.accolade.eventify;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import com.bumptech.glide.Glide;
import java.util.List;
public class ImageQuizAdapter extends RecyclerView.Adapter<ImageQuizAdapter.ImageQuizViewHolder> {
private Context mCtx;
private List<ImageQuizModel> quizList;
public ImageQuizAdapter (Context mCtx, List<ImageQuizModel> quizList) {
this.mCtx = mCtx;
this.quizList = quizList;
}
#Override
public ImageQuizViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.layout_recycler_image_quiz, null);
return new ImageQuizViewHolder(view);
}
#Override
public void onBindViewHolder(ImageQuizViewHolder holder, int position) {
ImageQuizModel imageQuizModel=quizList.get(position);
Glide.with(mCtx)
.load(imageQuizModel.getImage())
.into(holder.imageView);
//here i used only image
}
#Override
public int getItemCount() {
return quizList.size();
}
public class ImageQuizViewHolder extends RecyclerView.ViewHolder {
RadioButton r1,r2,r3,r4;
ImageView imageView;
public ImageQuizViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView2);
r1 = itemView.findViewById(R.id.radio_button1);
r2 = itemView.findViewById(R.id.radio_button2);
r3 = itemView.findViewById(R.id.radio_button3);
r4 = itemView.findViewById(R.id.radio_button4);
}
}
}
ImagaQuiz.java
package com.accolade.eventify;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class ImageQuiz extends AppCompatActivity {
private static final String URL_PRODUCTS = "http://accoladetest.cf/MyApi/MyApiQuizPic.php";
RecyclerView recyclerView;
private Toolbar mTopToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_quiz);
mTopToolbar = (Toolbar) findViewById(R.id.my_toolbar1);
setSupportActionBar(mTopToolbar);
recyclerView = findViewById(R.id.recylcerViewImage);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
loadProducts();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quiz, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_favorite) {
Toast.makeText(this, "Add Feature", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadProducts() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
List<ImageQuizModel> data=new ArrayList<>();
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for(int i=0;i< array .length();i++){
JSONObject json_data = array .getJSONObject(i);
ImageQuizModel product = new ImageQuizModel();
product.id= json_data.getInt("id");
product.image= json_data.getString("image");
product.op1= json_data.getString("op1");
product.op2= json_data.getString("op2");
product.op3= json_data.getString("op3");
product.op4= json_data.getString("op4");
data.add(product);
}
//creating adapter object and setting it to recyclerview
ImageQuizAdapter adapter = new ImageQuizAdapter(ImageQuiz.this, data);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
ImageQuizModel.java
package com.accolade.eventify;
public class ImageQuizModel {
public int id;
public String image;
public String op1;
public String op2;
public String op3;
public String op4;
public ImageQuizModel(){
}
public String getImage() {
return image;
}
public int getId(){
return id;
}
public String getOp1() {
return op1;
}
public String getOp2() {
return op2;
}
public String getOp3() {
return op3;
}
public String getOp4() {
return op4;
}
}
layout_recycler_image_guiz.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
app:cardCornerRadius="10dp"
android:elevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/img" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button1"
android:text=" Option 1"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"
/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button2"
android:text="Option 2"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button3"
android:text="Option 3"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radio_button4"
android:text="Option 4"
android:textSize="20dp"
android:buttonTint="#color/colorPrimary"/>
</LinearLayout>
</RadioGroup>
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
But here the problem is recycler view is displaying in half screen as the image below, I want its width to matchparent
enter image description here
I removed ConstraintLayout and instead I used RelativeLayout as a parent and it is working, Thank you guys for responding.
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/img" />
</LinearLayout>
</RelativeLayout>
Go to your imageview which you are using and make sure attributes layout height and width both are set to match_parent. Hope this will help.
You should not use match_parent for widgets within a ConstraintLayout. From the documentation:
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
If you want a widget to span 100% across its parent, the way to do that in ConstraintLayout is to set the width of the widget to 0dp and specify a start and end constraint as follows for the RelativeLayout which is a child of the ConstraintLayout:
<RelativeLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#e2e2e2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
In short, all children of ConstraintLayout should be constrained vertically and horizontally and match_parent should not be used.
I have a ListView created inside a fragment, and it has a search filter, the problem is XML layout showing fine in android studio but when running in the emulator or phone it's showing differently (not properly as I aligned) and also when I click the SearchView it goes under the tab navigation. Can anyone tell how to fix this?
This is the fragment:
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Individuals extends android.support.v4.app.ListFragment
implements FindCallback<ParseObject> {
private List<ParseObject> mOrganization = new ArrayList<ParseObject>();
SearchView sv;
IndividualsAdaptor adaptor;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.individuals, container, false);
}
#Override
public void onViewCreated(View view, Bundle b) {
super.onViewCreated(view, b);
sv = (SearchView) view.findViewById(R.id.ser1);
adaptor = new IndividualsAdaptor(getActivity(), mOrganization);
setListAdapter(adaptor);
ParseQuery.getQuery("_User").findInBackground(this);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adaptor.getFilter().filter(text);
return true;
}
});
}
#Override
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + scoreList.size() + " _User");
mOrganization.clear();
mOrganization.addAll(scoreList);
((IndividualsAdaptor) getListAdapter()).updateBackupList(mOrganization);
((IndividualsAdaptor) getListAdapter()).notifyDataSetChanged();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
}
This is the adapter:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
import com.parse.ParseObject;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class IndividualsAdaptor extends ArrayAdapter {
protected Context mContext;
// Code for Custom Filter.
protected List mBackupList = new ArrayList();
public IndividualsAdaptor(Context context, List status) {
super(context, R.layout.t3, status);
mContext = context;
// Code for Custom Filter.
mBackupList.addAll(status);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.t3, null);
holder = new ViewHolder();
holder.usernameHomepage = (TextView) convertView.findViewById(R.id.fname);
holder.statusHomepage = (TextView) convertView.findViewById(R.id.lname);
holder.pposition = (TextView) convertView.findViewById(R.id.idposition);
holder.orgName = (TextView) convertView.findViewById(R.id.organizationname);
holder.logo = (ImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject statusObject = (ParseObject) getItem(position);
// title
String username = statusObject.getString("firstname");
holder.usernameHomepage.setText(username);
// content
String status = statusObject.getString("lastname");
holder.statusHomepage.setText(status);
// content
String positions = statusObject.getString("position");
holder.pposition.setText(positions);
// content
String org = statusObject.getString("organizationName");
holder.orgName.setText(org);
// logo
URL url = null;
Bitmap bmp = null;
try {
url = new URL("img hosting location" + statusObject.getString("image"));
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
}catch (IOException e) {
}
holder.logo.setImageBitmap(bmp);
Picasso.with(mContext)
.load(String.valueOf(url))
.into(((ImageView) convertView
.findViewById(R.id.imageView)));
return convertView;
}
public static class ViewHolder {
TextView usernameHomepage;
TextView statusHomepage;
TextView orgName;
TextView pposition;
ImageView logo;
}
// Code for Custom Filter.
#Override
public Filter getFilter() {return new Filter(){
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String queryString = charSequence.toString().toLowerCase();
List<ParseObject> filteredList = new ArrayList<>();
ParseObject tmpItem;
String tmpUsername, tmpStatus, tmpPositions, tmpOrg;
for(int i=0; i<mBackupList.size(); i++){
tmpItem = (ParseObject) mBackupList.get(i);
tmpUsername = tmpItem.getString("firstname").toLowerCase();
tmpStatus = tmpItem.getString("lastname").toLowerCase();
tmpPositions = tmpItem.getString("position").toLowerCase();
tmpOrg = tmpItem.getString("organizationName").toLowerCase();
// The matching condition
if(tmpUsername.contains(queryString)||tmpStatus.contains(queryString)||
tmpPositions.contains(queryString)||tmpOrg.contains(queryString)){
filteredList.add(tmpItem);
}
}
FilterResults filterResults = new FilterResults();
filterResults.count = filteredList.size();
filterResults.values = filteredList;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, Filter.FilterResults filterResults) {
clear();
addAll((List<ParseObject>) filterResults.values);
}
};}
public void updateBackupList(List newList){
mBackupList.clear();
mBackupList.addAll(newList);
}
}
The ListView layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp">
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="335dp"
android:text="No data"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="56dp"
android:layout_toLeftOf="#+id/android:list"
android:layout_toStartOf="#+id/android:list" />
<SearchView
android:id="#+id/ser1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search.."
android:background="#android:color/holo_red_dark"
android:layout_marginTop="60dp"
android:layout_alignTop="#+id/android:empty"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</SearchView>
<ListView android:id="#id/android:list"
android:layout_width="263dp"
android:layout_height="241dp"
android:layout_marginTop="34dp"
android:layout_below="#+id/android:empty"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/individuals_img"
android:id="#+id/imageView3"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/android:empty"
android:layout_toEndOf="#+id/android:empty"/>
</RelativeLayout>
The item layout for the ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_gravity="center"
android:layout_height="110dp"
android:layout_width="110dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="19dp"
/>
<!-- img -->
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/organizationname"
android:textSize="10sp"
android:layout_below="#+id/idposition"
android:layout_alignLeft="#+id/idposition"
android:layout_alignStart="#+id/idposition"
android:paddingTop="10px" />
<TextView
android:text="yyyyyyyyyyyyyyyyy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idposition"
android:textSize="10sp"
android:layout_below="#+id/fname"
android:layout_alignLeft="#+id/fname"
android:layout_alignStart="#+id/fname" />
<TextView
android:id="#+id/fname"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_alignTop="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="13dp"
android:fontFamily="sans-serif" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lname"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_above="#+id/idposition"
android:layout_toRightOf="#+id/organizationname"
android:layout_toEndOf="#+id/organizationname" />
</RelativeLayout>
Main activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="app.com.anew.fbcapplication.AppHome">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_height="40dp"
android:background="#drawable/nav"
android:id="#+id/button2"
android:layout_weight="1"
android:layout_width="40dp"
android:layout_alignBottom="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:paddingLeft="25dp"
tools:layout_marginLeft="20dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/i"
android:id="#+id/button"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
app:srcCompat="#drawable/logo"
android:id="#+id/imageView2"
android:layout_weight="1"
tools:layout_marginLeft="15dp"
android:layout_width="280dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button"
android:layout_toStartOf="#+id/button"
android:layout_height="50dp" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#style/TabTextAppearance"
/>
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
I get error when trying your ListView (I think that it is individuals.xml), e.g. TextView android:id="#id/android:empty" has an attribute android:layout_below="#+id/button3" but I can't find button3 in your layout.
This may be the layout that you want, individuals.xml:
<RelativeLayout 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" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/individuals_img"
android:src="#drawable/individuals_img"
android:id="#+id/imageView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<SearchView
android:id="#+id/ser1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search.."
android:background="#android:color/holo_red_dark"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/imageView3"
android:layout_toStartOf="#+id/imageView3" >
</SearchView>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No data"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/ser1" />
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ser1" />
</RelativeLayout>
If this is not what you are looking for, please also show what your layout looks like in Android Studio layout editor. Hope it help :)