List onItemClickListener not working with - java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:focusable="false"
android:focusableInTouchMode="false">
<android.support.v7.widget.CardView
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="60dp"
android:padding="5dp"
android:elevation="6dp">
<TextView
android:id="#+id/major_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/black"
android:textSize="#dimen/text_size_large"
android:text="Computer Science"
android:gravity="center" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/subject_scroll"
android:paddingBottom="8dp"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
`This is dropdown.xml which is the view that will be inside the listview.
<LinearLayout 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">
<ListView
android:id="#+id/courses_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:orientation="vertical"
android:clickable="false">
</ListView>
</LinearLayout>
main.xml
the listview with id couses_list with contain the dropdown.xml view
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("ListView", "onItemClick: true");
});
I have tried setting the setFocusable() to false to everyview inside listview and other method shown in precious solutions in stack overflow but nothing worked for me.What wrong am i doing here..
ListView uses MajorAdapter which extends BindableAdapter
public class MajorAdapter extends BindableAdapter<Major> {
public class ViewHolder {
TextView majorName;
private boolean isOpen = false;
private RecyclerView subjectRecyclerView;
Major major;
List<Subject> subjects = new ArrayList<>();
ViewHolder(View view){
majorName = (TextView)view.findViewById(R.id.major_name);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
subjectRecyclerView = (RecyclerView)view.findViewById(R.id.subject_scroll);
subjectRecyclerView.setLayoutManager(linearLayoutManager);
SubjectAdapter subjectAdapter = new SubjectAdapter(subjects);
subjectRecyclerView.setAdapter(subjectAdapter);
subjectRecyclerView.addItemDecoration(new RecyclerListDecorater(view.getContext()));
}
public void toggle(){
if(this.isOpen)
this.isOpen = false;
else
this.isOpen = true;
}
public boolean isOpen(){
return this.isOpen;
}
public Model getMajor(){
return major;
}
public RecyclerView getRecyclerView(){
return this.subjectRecyclerView;
}
}
public MajorAdapter(Context context){
super(context);
}
#Override
public View getNewView(LayoutInflater inflater, int position, ViewGroup container) {
View view = inflater.inflate(R.layout.majors,null,false);
view.setFocusable(false);
ViewHolder holder = new ViewHolder(view);
view.setTag(holder);
return view;
}
#Override
public void bindView(Major item, int position, View view) {
ViewHolder holder= (ViewHolder)view.getTag();
holder.major = item;
holder.majorName.setText(item.getCourseName());
}
}
here is the bindable adapter
public abstract class BindableAdapter<T> extends ArrayAdapter<T> {
private LayoutInflater inflater;
public BindableAdapter(Context context){
super(context,0);
setup(context);
}
private void setup(Context context){
inflater = LayoutInflater.from(context);
}
#Override
public final View getView(int position, View view, ViewGroup container){
if(view == null){
view = getNewView(inflater, position, container);
if(view == null)
throw new IllegalStateException("View created cannot be null");
}
bindView(getItem(position), position, view);
return view;
}
public abstract View getNewView(LayoutInflater inflater, int position, ViewGroup container);
public abstract void bindView(T item, int position, View view);
#Override
public View getDropDownView(int position, View view, ViewGroup parent){
return getView(position, view, parent);
}
}

Related

How to reference a view inside an element from my ListView?

So I have created a custom adapter for my ListView with three Views - TextView, ImageView and a basic View:
<?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"
android:id="#+id/AllNotesFragment">
<TextView
android:id="#+id/addNoteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25dp"
android:textSize="18sp"
android:gravity="center"
android:fontFamily="#font/ukij_qolyazma"
android:text="+new"
/>
<ImageButton
android:id="#+id/deleteNoteImageButton"
android:layout_width="50dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:layout_marginEnd="30dp"
android:textSize="24sp"
android:gravity="center"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:fontFamily="#font/ukij_qolyazma"
/>
<View
android:id="#+id/underlineView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="70dp"
android:background="#color/colorMainDark" />
<ListView
android:id="#+id/notesListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="75dp"
android:layout_marginBottom="0dp">
</ListView>
I have created and instantiated the ListView
notesListView = view.findViewById(R.id.notesListView);
And filled it with a bunch of my notes
notes = (ArrayList<Note>) db.getAllNotesForDay(NotesForDayActivity.getRememberDay(),
NotesForDayActivity.getRememberMonth(),
NotesForDayActivity.getRememberYear());
for (Note note : notes) {
noteTitles.add(note.getTitle());
}
NotesListAdapter adapter = new NotesListAdapter(((NotesForDayActivity) getActivity()).getContext(), notes);
notesListView.setAdapter(adapter);
With my custom adapter:
public class NotesListAdapter extends BaseAdapter {
private static final String TAG = "NotesListAdapter";
public static Context context;
private RelativeLayout notesListRelativeLayout;
private TextView noteTitleTextView;
private ImageView tickImage;
private View underlineView;
private List<Note> notes;
// !
private View listItemsView;
public NotesListAdapter(Context context, List<Note> notes) {
this.notes = notes;
this.context = context;
}
#Override
public int getCount() {
return NotesForDayActivity.getCountNotes();
}
#Override
public Object getItem(int position) {
return notes.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
listItemsView = convertView;
if (listItemsView == null) {
listItemsView = LayoutInflater.from(NotesForDayActivity.context).inflate(R.layout.notes_list_layout, null);
}
underlineView = listItemsView.findViewById(R.id.underlineView);
notesListRelativeLayout = (RelativeLayout) listItemsView.findViewById(R.id.notesListRelativeLayout);
noteTitleTextView = (TextView) listItemsView.findViewById(R.id.noteTitleTextView);
tickImage = (ImageView) listItemsView.findViewById(R.id.tickImageView);
noteTitleTextView.setText(notes.get(position).getTitle());
return listItemsView;
}
I can access an item inside my ListView with a OnItemClickListener, but I do not know how to access a View inside that particular Item of my ListView.
So If I set my OnClick like this:
notesListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
How can I reference one of the three Views in that item. For example I need to set the visibility of my imageview:
Any help is appreciated.
Try this, OnItemClickListener will return the view, so that you can get access through that view
notesListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView tickImage = view.findViewById(R.id. tickImageView);
if(tickImage!=null){
tickImage.setVisibility(View.GONE);
}
}
});

Trying to create a custom ArrayAdapter with ArrayList

I'm trying to get ArrayList with different variables that i'm getting from a database to display in a customized listview
this is the main xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View the balance sheet"
android:textSize="29sp"
android:id="#+id/view"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView">
</ListView>
</RelativeLayout>
this is the customized layout
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/description_names"
android:text="description"
android:layout_marginTop="25sp"
android:layout_marginLeft="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/values_names"
android:text="value"
android:layout_marginTop="25sp"
android:layout_marginLeft="105sp"/>
</RelativeLayout>
this is the adapter, i wanted both the ArrayLists from the main activity but it's not working
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(#NonNull Context context, ArrayList<String> mDetails,ArrayList<String> mValues) {
super(context,R.layout.custom_layout ,mDetails);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(getContext());
View customView=inflater.inflate(R.layout.custom_layout,parent,false);
String details=getItem(position);
String values=getItem(position);
TextView description_names=(TextView)customView.findViewById(R.id.description_names);
TextView values_names=(TextView)customView.findViewById(R.id.values_names);
description_names.setText(details);
return customView;
}
}
main activity
ListView listvIew=(ListView)findViewById(R.id.listView);
final ArrayAdapter<String>array=new CustomAdapter(this,mDetails,mValues);
listvIew.setAdapter(array);
private ArrayList<String>mDetails=new ArrayList<>();
private ArrayList<String>mValues=new ArrayList<>();
V3Ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value=dataSnapshot.child("records").child("values").getValue(String.class);
Log.d(TAG, "onChildAdded: the value is"+value);
mDetails.add(value);
mValues.add(value);
array.notifyDataSetChanged();
}
i'm getting values from the database then putting them in ArrayLists then i'm trying to display them in a customized listview but it's not working when i try to use the second Arraylist
adapter calls from your main activity should be like,
CusatomeAdapter adapter;
adapter = new CusatomeAdapter(mActivity,mList1 ,mList2);
mListview.setAdapter(adapter);
here is your adapter calss
public class CusatomeAdapter extends BaseAdapter {
public ArrayList<String> mList1;
public ArrayList<String> mList2;
Activity activity;
public CusatomeAdapter(Activity activity,ArrayList<String> mList1 , ArrayList<String> mList2){
super();
this.activity = activity;
this.mList1 = mList1;
this.mList2 = mList2;
}
#Override
public int getCount() {
return mList1.size(); //give the size of list here
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_detail, null);
holder = new ViewHolder();
holder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
//same way define another text view and set their value ..
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_detail.setText(mList1.get(position).toString());
return convertView;
}
private class ViewHolder {
TextView tv_detail;
}
}

How to make the navigation drawer header picture scrollable?

I wanted to create a navigation drawer with a something like profile picture above the items of the list. Although I successfully created one but I have a problem on it: First the header picture the one with a circle image and the background also the text Ej Lungay is not scrollable. I wanted to make it scrollable, the moment that when i scroll down it must be also scroll down. I provided the image url below and the code:
Here is the sliding menu which contains the ListView and the Header of the navgation drawer:
fragment_sliding_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/white"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:background="#drawable/background_poly">
<edu.ejapp.dotalegitstore.RoundedImage
android:id="#+id/user_pic"
android:layout_width="70dp"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:src="#drawable/hoiist"
android:layout_marginLeft="8dp"/>
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/user"
android:layout_below="#id/user_pic"
android:layout_marginLeft="8dp"
android:textColor="#color/white"
/>
</RelativeLayout>
<ListView
android:id="#+id/list_view"
android:divider="#color/gray_light"
android:dividerHeight="0dip"
android:layout_width="match_parent"
android:layout_height="0dp"
android:listSelector="#color/app_main"
android:layout_weight="1"/>
</LinearLayout>
And fragment_sliding_menu.xml is implemented here:
SlidingMenuFragment.java
public class SlidingMenuFragment extends Fragment {
List<String> data;
ListView list_view;
SlidingMenuListAdapter adapter;
#SuppressLint("InflateParams")
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_sliding_menu, null);
setUpView(root);
return root;
}
void setUpView(ViewGroup root){
list_view = (ListView)root.findViewById(R.id.list_view);
initList();
setUpClick();
}
void initList(){
data = new ArrayList<String>();
data.add("Home");
data.add("Arcana");
data.add("Courrier");
data.add("Hero Sets");
data.add("Immortals");
adapter = new SlidingMenuListAdapter(getActivity(),data);
list_view.setAdapter(adapter);
}
void setUpClick(){
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
BaseActivity.mTitle="Home";
break;
case 1:
BaseActivity.mTitle="Arcana";
break;
case 2:
BaseActivity.mTitle="Courrier";
break;
case 3:
BaseActivity.mTitle="Hero Sets";
break;
case 4:
BaseActivity.mTitle="Immortals";
break;
default:
BaseActivity.mTitle="Dota Legit Store";
}
MainActivity.closeDrawer();
}
});
}
}
And this is the ListView item: lis_view_sliding_menu_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:orientation="horizontal" >
<TextView
android:layout_margin="10dip"
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#color/gray_dark"
android:text="" />
</LinearLayout>
And then the xml files are implemented in this class: SlidingMenuListAdapter.java
public class SlidingMenuListAdapter extends BaseAdapter {
List<String>data;
Context context;
public SlidingMenuListAdapter(Context context,List<String> data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int id) {
// TODO Auto-generated method stub
return id;
}
#Override
public long getItemId(int id) {
// TODO Auto-generated method stub
return id;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.list_view_sliding_menu_items, null);
viewHolder.name = (TextView)convertView.findViewById(R.id.name);
//## Initial Views
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//## Setup Data below
viewHolder = (ViewHolder) convertView.getTag();
TextView name = viewHolder.name;
name.setText(data.get(position));
return convertView;
}
public class ViewHolder {
TextView name;
}
}
Any other function of the program is working properly.
My question is: How can i make the header (please visit the image i provided above) scrollable like the list items (home, arcana, ...)? Please help me.. Im an android beginner. Any suggestion will be accepted will pleasure.

onListItemClick is not work for added two clickable items in the list row

I created a ListFragment by using a custom adapter. OnListItemClick is not work after I add a button in the list row. If I click on the list row it should be intent to other class, but now when I click on the list row, it did not have any action. No error and no action. So I think it might because there has two clickable items in the list row, so the onListItemClick is not work. But I do not know how can I solve this problem.
Fragment1.java
public class Fragment1 extends ListFragment implements AdapterInterface{
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_show_queue, container, false);
// initialize the items list
mItems = new ArrayList<ListViewItem>();
// initialize and set the list adapter
adapter = new ListViewAdapter2(getActivity(), mItems, this);
setListAdapter(adapter);
return rootView;
}
#Override
public void buttonPressed(int position) {
System.out.println("Fragment1: " + position);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(getActivity(), QueueDetail.class);
i.putExtra("qid", qid.get(position).toString());
startActivity (i);
}
}
ListViewAdapter2.java
public class ListViewAdapter2 extends ArrayAdapter<ListViewItem> {
AdapterInterface buttonListener;
private int position;
public ListViewAdapter2(Context context, List<ListViewItem> items, AdapterInterface buttonListener) {
super(context, R.layout.listview_layout2, items);
this.buttonListener = buttonListener;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
this.position = position;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.listview_layout2, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
viewHolder.txt = (TextView) convertView.findViewById(R.id.txt);
viewHolder.smallTxt = (TextView) convertView.findViewById(R.id.smallTxt);
viewHolder.datetime = (TextView) convertView.findViewById(R.id.datetime);
viewHolder.viewBtn = (Button) convertView.findViewById(R.id.viewBtn);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.viewBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("ListViewAdapter: " + position);
buttonListener.buttonPressed(position);
}
});
...
return convertView;
}
private int getViewBtnPosition(){
return position;
}
}
fragment_show_queue.xml
<FrameLayout 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" >
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</FrameLayout>
listview_layout2.xml
<?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/img"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:layout_toRightOf="#+id/img"
android:layout_toLeftOf="#+id/datetime"
android:layout_toStartOf="#+id/datetime">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp" />
<TextView
android:id="#+id/smallTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:id="#+id/datetime"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text=""
android:textSize="10dp" />
<Button
android:layout_width="35dp"
android:layout_height="20dp"
android:id="#+id/viewBtn"
android:text="JOIN"
android:textSize="10dp"
android:background="#b5e61d"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
add android:focusable="false" and android:focusableInTouchMode="false" to your Button in the row's layout file.

Change Background colour of ListView in android at run time

I am doing project in Android. I want to change background color as well as textcolor of selected item from ListView. Here is my code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listView1"
android:layout_width="265dp"
android:layout_height="366dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_weight="0.00"
android:drawSelectorOnTop="true" >
</ListView>
</LinearLayout>
</LinearLayout>
So,I have ListView with some student names and with facility of multiple choice by using checkbox.
ListView stud_lst=(ListView) findViewById(R.id.listView1);
stud_lst.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
I want to change the background and text color of selected student.
I already saw some answers but I am not getting it.
Please help me.
Use a custom adapter and in your activity class do the following:
// mListview is ur listview object.
mListview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
view.setBackgroundColor("your bg's color id");
}
}
You have to create a Custom Adapter to change the item's Background Color. Here is the example of Custom adapter:
public class PaListAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private ArrayList<String> platevalue = new ArrayList<String>();
ViewHolder holder;
public PaListAdapter(Context context,ArrayList<String> value)
{
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
//mycontext = context;
platevalue.clear();
platevalue =value;
}
public int getCount()
{
return platevalue.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.select_dialog, null);
holder = new ViewHolder();
holder.hTransID =(TextView) convertView.findViewById(R.id.txtChoice);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.hTransID.setText(platevalue.get(position));
return convertView;
}
static class ViewHolder
{
TextView hTransID;
}
}
select_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
android:background="#000000"
>
<TextView
android:id="#+id/txtChoice"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"/>
</LinearLayout>
In Activity Class.Define it like:
simpleefficientadapter efficientadapter;
efficientadapter=new simpleefficientadapter(CLASSNAME.this, VALUES);
listView.setAdapter(efficientadapter);

Categories