i am using following code to create custom adapter for listview.
now i want to use trackball click event in it but i dont know how to do that can any one help me out in creating ontracballevent in custom adapter?
i have tried writing few lines but not able to solve it.
public class EfficientAdapter extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
private Context context;
int pos;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView = mInflater.inflate(R.layout.adaptor_contentposts, null);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//click functionality
}
});
MotionEvent event= MotionEvent.CREATOR.createFromParcel(null);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
//display click message
}
convertView.onTrackballEvent(event);
return convertView;
}
class ViewHolder {
TextView textLine;
TextView textLine2;
TextView PostedByAndPostedOn;
ImageButton ImgButton;
}
#Override
public Filter getFilter() {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public int getCount() {
return ad_id.length;
}
#Override
public Object getItem(int position) {
return ad_id[position];
}
}
Adapters have nothing to do with "ontracballevent". You either need to subclass ListView or handle it in your activity.
Related
I'm trying to change the color of buttons in the rows of listview by clicking a button. Here I've a listview composed of 25 rows and each rows contains a button. I've used custom adapter for listview to place button in each row. I like to know how to do following things:
Click on a button and its color is changed.
After step (1) the onClickListener also change the color of the left-over buttons in the listview to normal.
The above two steps should happen to all of the buttons means if any of the button is clicked then it should be colored and remaining will turn back to normal.
the listview is like
Button 1
Button 2
Button 3
Button 4
You have to access your Button from Adapter. Follow it -
public class TestAdapter extends BaseAdapter {
private Context ctx;
private List<TestList> list;
public TestAdapter(Context ctx, List<TestList> list){
this.ctx = ctx;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public TestList getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.layout_item, parent, false);
}
Button button = (Button) convertView.findViewById(R.id.button);
button.setText(list.get(position));
return convertView;
}
}
Use onclick listener for button in your custom adapter
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private List<CustomList> list;
public CustomAdapter(Context ctx, List<CustomList> list){
this.ctx = ctx;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public CustomList getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.layout_item, parent, false);
}
Button button = (Button) convertView.findViewById(R.id.button);
button.setText(list.get(position));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
\\change color here
}
});
return convertView;
}
}
I am developing an Android app with a lot of ListView's. i created my own ListAdapter but i am not so happy with the OnItemClickListener. What is the best way to add a listener on an item if every item has other functionality?
You may try this, I'm using this Custom Adapter & OnClickListener()
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Jobs> jobsItems;
public CustomListAdapter(Activity activity, List<Jobs> jobsItems) {
this.activity = activity;
this.jobsItems = jobsItems;
}
#Override
public int getCount() {
return jobsItems.size();
}
#Override
public Object getItem(int location) {
return jobsItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.custom_list_row_no_preview, null);
Button submit = (Button) convertView.findViewById(R.id.btnSubmit);
serial.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return convertView;
}
}
Similarly you can add any TextView, EditText or Button into your ListView and set onCickListener()
I have a CustomBase adapter with a button attached to it,when I click on the button the row is to be deleted, which is working perfectly fine, but my problem is, that data which is populated in my adapter is from a remote server, I want to be getting the information contained by the adapter before deleting it and giving it to my MainActivity, in my case, I would like to be getting the title before the view is removed and sending it to the server, telling it to delete it from storage, the big issue is all code to do with the button is in my baseAdapter class, I do not know if I can give my MainActivity the data before the row is Deleted, I cannot think of a way of achieving that, if anyone has an idea please share, below is my Base Adapter.
public class CartAdapter extends BaseAdapter{
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
ViewHolder holder;
public CartAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
notifyDataSetChanged();
layoutInflater = LayoutInflater.from(context);
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.singlecartitem, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
final ImageButton delete = (ImageButton) convertView.findViewById(R.id.delete);
convertView.findViewById(R.id.postCommentBox);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listData.remove(position);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
delete.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ListItem newsItem = listData.get(position);
holder.title.setText(newsItem.getTitle());
return convertView;
}
static class ViewHolder {
TextView title;
int position;
}
}
Are you using a ListFragment? If you do, check
this out.
If you do not, make your activity implement an interface e.g. HandleDeletion with a method e.g. handleTitle(String title). In your clickListener cast the context object to HandleDeletion and call the method handleTitle(String title) on it. This is a so called callback.
[EDIT]
Example:
public interface HandleDeletion {
handleTitle(String title);
}
public class MyActivity implements HandleDeletion {
...
handleTitle(String title) {
//do whatever you want
}
...
}
public class CartAdapter extends BaseAdapter{
...
private Context context;
public CartAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
this.context = context;
notifyDataSetChanged();
layoutInflater = LayoutInflater.from(context);
}
public View getView(final int position, View convertView, ViewGroup parent) {
...
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((HandleDeletion)context).handleTitle(listData.get(position).getTitle());
listData.remove(position);
notifyDataSetChanged();
}
});
...
}
...
}
I have created a standard listview and I have populated it with text. Is it possible to add one or two buttons to each row which will then perform some sort of function?
Yes, its possible you need to create custom layout for the row,
see this
example
You just use this custom adapter in your Listview creation and then you will write your code in this block...
use this code in your activity...
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
And then here you replace your Layout....
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
#Override
public Sample getItem(int position) {
return mlist.get(position);
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);// Replace your
// layout....
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// you just put your Logic here And use this custom adapter to
// load your Data By using this particular custom adapter to
// your listview
// Change your imageview here
}
});
return convertView;
}
}
I want to access a button from an activity in a lazyadapter's getview( that is to make clickable from lazyadapter) is there any method to do that? The button click brings the user to the next screen. Please if someone has any idea or code.
You just follow this code on get your Click function and call your next activity in the onclick listener....
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
#Override
public Sample getItem(int position) {
return mlist.get(position);
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// you just put your Logic here And use this custom adapter to
// load your Data By using this particular custom adapter to
// your listview
}
});
return convertView;
}
}