I have trouble getting data from an array then search it in my search box. It only shows one result which is the first item.
I think this is the code being called:
ViewOrders.this.orderArrayAdapter.getFilter().filter(s);
Other than that is there other option to get a search function?
Here is my code:
Class A
private OrdersList orderArrayAdapter;
private void setupAdapterOrdersList() {
orderArrayAdapter = new OrdersList(getApplicationContext(), R.layout.row_item_view_orders, ordersArray);
if (orderListView != null) {
orderListView.setAdapter(orderArrayAdapter);
}
}
private void searchFromListView() {
etSearch = findViewById(R.id.search_order);
/**
* Enabling Search Filter
* */
etSearch.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// When user changed the Text
ViewOrders.this.orderArrayAdapter.getFilter().filter(s);
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
Class B
package com.example.android.ontrack.adapters;
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.android.ontrack.R;
import com.example.android.ontrack.models.Order;
public class OrdersList extends ArrayAdapter<Order> {
Context mContext;
int mLayoutResourceId;
Order mData[] = null;
public OrdersList(Context context, int resource, Order[] data) {
super(context, resource, data);
this.mContext = context;
this.mLayoutResourceId = resource;
this.mData = data;
}
#Override
public Order getItem(int position) {
return super.getItem(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);
TextView nameOfSchool = row.findViewById(R.id.school_name_orders);
TextView nameOfAgent = row.findViewById(R.id.agent_name);
TextView orderId = row.findViewById(R.id.order_id);
TextView netRevenue = row.findViewById(R.id.net_revenue);
TextView orderQuantity = row.findViewById(R.id.total_quantity);
TextView date = row.findViewById(R.id.date);
Order orders = mData[position];
nameOfSchool.setText(orders.nameOfSchool);
nameOfAgent.setText(String.valueOf(orders.nameOfAgent));
orderId.setText(String.valueOf(orders.orderId));
netRevenue.setText(String.valueOf(orders.netRevenue));
orderQuantity.setText(String.valueOf(orders.totalQuantity));
date.setText(String.valueOf(orders.date));
return row;
}
}
/**
* Created by Prateek on 2/18/2017.
*/
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import newsee.novasyslabs.com.nodefetch.Others.CircularTransform;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context mContext;
public static ArrayList <String> embed_html = new ArrayList<>();
public static ArrayList <String> channel_thumbnail = new ArrayList<>();
public static ArrayList <String> channel_title = new ArrayList<>();
public static ArrayList <String> video_title = new ArrayList<>();
public static ArrayList <String> created_time = new ArrayList<>();
public static ArrayList <Integer> android_likes = new ArrayList<>();
public static ArrayList <Integer> pf_likes = new ArrayList<>();
public static ArrayList <Integer> android_views = new ArrayList<>();
public static ArrayList <Integer> pf_views = new ArrayList<>();
public RecyclerAdapter(Context mContext){
this.mContext = mContext;
}
class ViewHolder extends RecyclerView.ViewHolder{
WebView webView;
public ImageView chlogo;
public TextView chtitle;
public TextView vidtitle;
public TextView upld_time;
public TextView show_views;
public TextView show_likes;
public ImageView mLike;
int j = 0;
public ViewHolder(View itemView) {
super(itemView);
this.mLike = (ImageView)itemView.findViewById(R.id.video_like);
this.show_views = (TextView) itemView.findViewById(R.id.video_views_count);
this.show_likes = (TextView) itemView.findViewById(R.id.likes_count);
this.webView = (WebView) itemView.findViewById(R.id.web);
this.chlogo = (ImageView) itemView.findViewById(R.id.ch_logo);
this.chtitle = (TextView) itemView.findViewById(R.id.ch_name);
this.vidtitle = (TextView) itemView.findViewById(R.id.video_title);
this.upld_time = (TextView)itemView.findViewById(R.id.ch_time);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.layout_card, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.j==0) {
holder.mLike.setImageResource(R.drawable.liked);
CharSequence like = holder.show_likes.getText();
int new_like = Integer.parseInt((String) like) + 1;
Toast.makeText(mContext,"Liked" +new_like ,Toast.LENGTH_SHORT).show();
holder.j = 1;
} else {
holder.mLike.setImageResource(R.drawable.unlike);
CharSequence like = holder.show_likes.getText();
int new_like = Integer.parseInt((String) like) - 1;
Toast.makeText(mContext,"Unliked" +new_like,Toast.LENGTH_SHORT).show();
holder.j = 0;
}
}
});
holder.webView.getSettings().setJavaScriptEnabled(true);
holder.webView.loadData(embed_html.get(position), "text/html", "utf-8");
Picasso.with(mContext).load(channel_thumbnail.get(position)).transform(new CircularTransform()).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).into(holder.chlogo);
holder.chtitle.setText(channel_title.get(position));
holder.vidtitle.setText(video_title.get(position));
holder.upld_time.setText(created_time.get(position));
int and_views = android_views.get(position);
int views = pf_views.get(position);
views += and_views;
int and_likes = android_likes.get(position);
int likes = pf_likes.get(position);
likes += and_likes;
holder.show_views.setText("" + views);
holder.show_likes.setText("" + likes);
}
#Override
public int getItemCount() {
return embed_html.size();
}
}
when i am clicking then image change for 0th postion then 4,8,12.... why onclicklistener effect every 4 object in recyclerview i am changing the image resource onClick but the images changes for 4postion, 8position
I would recommend doing it this way, first in your onBindViewHolder method add this code:
holder.mLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showImage(position);
}
}
});
Then create a new method in this class only:
public void showImage(int position){
if (position == 0) {
holder.imageView.setImageResource(R.drawable.medal);
}
...
}
I'm working on an Android App that displays 151 pictures via Gridview. I have 151 sounds and I want to assign every sound to a single image, so I can play that sound tapping on an Image.
I'm using SoundPool. Ideas about how can I do this thing?
package com.example.thefe.newsmartkedex;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import static com.example.thefe.newsmartkedex.R.raw.pkmn1;
public class MainActivity extends AppCompatActivity {
int pkmn1, pkmn2, pkmn3;
SoundPool mySoundPool = new SoundPool (1, AudioManager.STREAM_MUSIC, 0);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pkmn1 = mySoundPool.load(this, R.raw.pkmn1, 1);
pkmn2 = mySoundPool.load(this, R.raw.pkmn2, 1);
pkmn3 = mySoundPool.load(this, R.raw.pkmn3, 1);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
position += 1; //forget about this
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
mySoundPool.play(/*???*/,1,1,1,0,1);
}
});
};
}
My ImageAdapter Class works like this:
package com.example.thefe.newsmartkedex;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by TheFe on 27/09/2016.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c, ArrayList list) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.pkmn1, R.drawable.pkmn2,
R.drawable.pkmn3, R.drawable.pkmn4,
R.drawable.pkmn5, R.drawable.pkmn6,
R.drawable.pkmn7, R.drawable.pkmn8,
R.drawable.pkmn9, R.drawable.pkmn10,
R.drawable.pkmn11, R.drawable.pkmn12,
R.drawable.pkmn13, R.drawable.pkmn14,
R.drawable.pkmn15, R.drawable.pkmn16,
R.drawable.pkmn17, R.drawable.pkmn18,
R.drawable.pkmn19, R.drawable.pkmn20,
R.drawable.pkmn21, R.drawable.pkmn22,
R.drawable.pkmn23, R.drawable.pkmn24,
R.drawable.pkmn25, R.drawable.pkmn26,
R.drawable.pkmn27, R.drawable.pkmn28,
R.drawable.pkmn29, R.drawable.pkmn30,
R.drawable.pkmn31, R.drawable.pkmn32,
R.drawable.pkmn33, R.drawable.pkmn34,
R.drawable.pkmn35, R.drawable.pkmn36,
R.drawable.pkmn37, R.drawable.pkmn38,
R.drawable.pkmn39, R.drawable.pkmn40,
R.drawable.pkmn41, R.drawable.pkmn42,
R.drawable.pkmn43, R.drawable.pkmn44,
R.drawable.pkmn45, R.drawable.pkmn46,
R.drawable.pkmn47, R.drawable.pkmn48,
R.drawable.pkmn49, R.drawable.pkmn50,
R.drawable.pkmn51, R.drawable.pkmn52,
R.drawable.pkmn53, R.drawable.pkmn54,
R.drawable.pkmn55, R.drawable.pkmn56,
R.drawable.pkmn57, R.drawable.pkmn58,
R.drawable.pkmn59, R.drawable.pkmn60,
R.drawable.pkmn61, R.drawable.pkmn62,
R.drawable.pkmn63, R.drawable.pkmn64,
R.drawable.pkmn65, R.drawable.pkmn66,
R.drawable.pkmn67, R.drawable.pkmn68,
R.drawable.pkmn69, R.drawable.pkmn70,
R.drawable.pkmn71, R.drawable.pkmn72,
R.drawable.pkmn73, R.drawable.pkmn74,
R.drawable.pkmn75, R.drawable.pkmn76,
R.drawable.pkmn77, R.drawable.pkmn78,
R.drawable.pkmn79, R.drawable.pkmn80,
R.drawable.pkmn81, R.drawable.pkmn82,
R.drawable.pkmn83, R.drawable.pkmn84,
R.drawable.pkmn85, R.drawable.pkmn86,
R.drawable.pkmn87, R.drawable.pkmn88,
R.drawable.pkmn89, R.drawable.pkmn90,
R.drawable.pkmn91, R.drawable.pkmn92,
R.drawable.pkmn93, R.drawable.pkmn94,
R.drawable.pkmn95, R.drawable.pkmn96,
R.drawable.pkmn97, R.drawable.pkmn98,
R.drawable.pkmn99, R.drawable.pkmn100,
R.drawable.pkmn101, R.drawable.pkmn102,
R.drawable.pkmn103, R.drawable.pkmn104,
R.drawable.pkmn105, R.drawable.pkmn106,
R.drawable.pkmn107, R.drawable.pkmn108,
R.drawable.pkmn109, R.drawable.pkmn110,
R.drawable.pkmn111, R.drawable.pkmn112,
R.drawable.pkmn113, R.drawable.pkmn114,
R.drawable.pkmn115, R.drawable.pkmn116,
R.drawable.pkmn117, R.drawable.pkmn118,
R.drawable.pkmn119, R.drawable.pkmn120,
R.drawable.pkmn121, R.drawable.pkmn122,
R.drawable.pkmn123, R.drawable.pkmn124,
R.drawable.pkmn125, R.drawable.pkmn126,
R.drawable.pkmn127, R.drawable.pkmn128,
R.drawable.pkmn129, R.drawable.pkmn130,
R.drawable.pkmn131, R.drawable.pkmn132,
R.drawable.pkmn133, R.drawable.pkmn134,
R.drawable.pkmn135, R.drawable.pkmn136,
R.drawable.pkmn137, R.drawable.pkmn138,
R.drawable.pkmn139, R.drawable.pkmn140,
R.drawable.pkmn141, R.drawable.pkmn142,
R.drawable.pkmn143, R.drawable.pkmn144,
R.drawable.pkmn145, R.drawable.pkmn146,
R.drawable.pkmn147, R.drawable.pkmn148,
R.drawable.pkmn149, R.drawable.pkmn150,
R.drawable.pkmn151
};
}
Create a model class that holds a reference to your image and the sound associated with it. When an item is clicked, get the object corresponding to the object, load the SoundPool object using the reference and play it.
public class Item {
int imageId;
int soundID;
public Item(int imageId, int soundID){
this.imageId = imageId;
this.soundID = soundID;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public int getSoundID() {
return soundID;
}
public void setSoundID(int soundID) {
this.soundID = soundID;
}
}
Create a List of Items,
List<Item> items = new ArrayList<Items>();
for(int i = 0; i < 10; i++) {
items.add(new Item(R.drawable.your_image_id, R.raw.your_sound_id));
}
Pass this list to your adapter. You will to modify the constructor of ImageAdapter. Initialize your adapter like this,
GridView gridview = (GridView) findViewById(R.id.gridview);
ImageAdapter adapter = new ImageAdapter(this, items);
gridview.setAdapter(adapter);
Inside you onItemClick,
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// your code
Item selectedItem = items.get(position);
int soundID = selectedItem.getSoundID();
pkmn1 = mySoundPool.load(MainActivity.this, soundID, 1);
mySoundPool.play(pkmn1, 1, 1, 1, 0, 1);
}
});
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi i am new to android development. I have made an app where picture are now shown in a gridview. But i want to make it more user friendly. I have found an example but i need some help to implement my code as the example. I want to write my UserList.java as ImageAdapter.java. I want to use imageview instead of holder. How can i do that??
Example i am trying to follow:
ImageAdapter.java
package com.step2rock.www.photographynowroadtopro;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by Sushimz on 5/7/2016.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
// R.drawable.pic_7, R.drawable.pic_8,
// R.drawable.pic_9, R.drawable.pic_10,
// R.drawable.pic_11, R.drawable.pic_12,
// R.drawable.pic_13, R.drawable.pic_14,
// R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
Now Here is my code.
UserList.java
package com.step2rock.www.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.step2rock.www.crudproject.R;
import com.step2rock.www.crudproject.UserlistActivity;
import com.step2rock.www.model.User;
public class UserList extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
UserlistActivity activity;
ArrayList<User> users;
public UserList(Context context, ArrayList<User> users) {
mContext = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.users = users;
}
#Override
public int getCount() {
return users.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
static class ViewHolder {
public ImageView ivUserImage;
public TextView tvUserName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
activity = new UserlistActivity();
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.userrow_activity, null);
holder.ivUserImage = (ImageView) convertView.findViewById(R.id.ivUserImage);
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvHeader);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
User user = new User();
user = users.get(position);
holder.ivUserImage.setImageBitmap(convertToBitmap(user.get_user_pic()));
holder.tvUserName.setText(user.get_first_name());
return convertView;
}
public Bitmap convertToBitmap(String base64String) {
byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);
Bitmap bitmapResult = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return bitmapResult;
}
}
You are new to android and want to create a grid of showing pictures.
You can develop the grid like below Follow this tutorial.