Hi everyone I'm trying to show with a GridView some items that show the contents of an array of strings. This is the CardView I want to set as item:
So when I click on a button, the layout of the choice of the type of film becomes visible and I set an adapter:
cTBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postaCardVIew.setVisibility(View.GONE);
cT.setVisibility(View.VISIBLE);
GridView simpleGridView = findViewById(R.id.simpleGridView);
String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza","Fantasy",
"Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
};
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (posta.this,android.R.layout.simple_list_item_1, genrelist);
simpleGridView.setAdapter(adapter);
}
But this is what I am getting:
I want to inflate this layout though:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="150dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="30dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type"
android:layout_centerInParent="true"
android:textSize="20dp"
android:textStyle="bold"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
To get such a thing:
Can anyone help me figure out how I could inflate the desired layout?
In order to use a custom item layout, you need to create a custom adapter that extends from the BaseAdapter or ArrayAdapter
Then inflate and build your item layout in the getView() method:
public class CustomAdapter extends BaseAdapter {
String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza", "Fantasy",
"Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
};
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title = convertView.findViewById(R.id.type);
holder.title.setText(genrelist[position]);
return convertView;
}
static class ViewHolder {
TextView title;
}
public int getCount() {
return genrelist.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
And use it as the GridView adapter:
cTBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postaCardVIew.setVisibility(View.GONE);
cT.setVisibility(View.VISIBLE);
GridView simpleGridView = findViewById(R.id.simpleGridView);
simpleGridView.setAdapter(new CustomAdapter());
}
});
I had to manipulate the margin and cared width and wrap the CardView with a FrameLayout in order to force the margin among cards:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="4dp"
android:layout_marginVertical="4dp">
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="120dp"
android:layout_height="120dp"
app:cardCornerRadius="30dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Type: 1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
Result:
Related
I am populating Recycler View with a List of 7 string items but Recycler view only loads two of them
by the way my data is long text and it loads all items when text is short
this is my ContentAdapter.java
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
private LayoutInflater mInflater;
private List<String> mContent;
ContentAdapter(Context context, List<String> Content) {
this.mInflater = LayoutInflater.from(context);
mContent = Content;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.rvcontent_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ContentAdapter.ViewHolder holder, int position) {
//loads only two times !?
holder.txtContentPage.setText(mContent.get(position));
}
#Override
public int getItemCount() {
return mContent.size(); // size is 7
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txtContent;
ViewHolder(View itemView) {
super(itemView);
txtContent = itemView.findViewById(R.id.txtContent);
}
}
}
rvcontent_item.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>
and Activity
rvContents.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
rvContents.setLayoutManager(layoutManager);
ContentAdapter contentAdapter = new ContentAdapter(this, Data); // Data has 7 items
rvContents.setAdapter(contentAdapter);
and this is my layout activity xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Try with
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:nestedScrollingEnabled="false"
android:focusableInTouchMode="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
The issue is
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvContentPage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Here android:layout_height="match_parent" is a bug, since you are inside a scroll view your recyclerview should have height wrap_content. So that the parent will allow it to scroll.
Try changing ConstraintLayout height match_parent instead of wrap_content.
and if persists, Remove scrollView from root because RecyclerView itself makes layout scrollable.
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.
I have one activity that contains text view, buttons, spinner and list view
my main XML file contains :
<RelativeLayout 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="#ffff00"
tools:context="com.example.taxitabriz.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="#string/app_name"
android:textSize="35dp"
android:textStyle="bold"
android:textColor="#996600"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/linearlayout1" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#996600"
android:text="#string/exit"
android:background="#drawable/backbutton" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#996600"
android:text="#string/about"
android:background="#drawable/backbutton" />
</LinearLayout>
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="19dp" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/spinner1"
android:layout_above="#+id/linearlayout1"
android:layout_alignParentRight="true"
android:layout_weight="49.94" >
</ListView>
and part of my java code is here:
ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
sp.setAdapter(ard);
lv1=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
lv1.setAdapter(ard1);
but i can't change font color or font type in list view or spinner. how can i do it?
You need to create a CustomListAdapter.
private class CustomListAdapter extends ArrayAdapter {
private Context mContext;
private int id;
private List <String>items ;
public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
{
super(context, textViewResourceId, list);
mContext = context;
id = textViewResourceId;
items = list ;
}
#Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
TextView text = (TextView) mView.findViewById(R.id.textView);
if(items.get(position) != null )
{
text.setTextColor(Color.WHITE);
text.setText(items.get(position));
text.setBackgroundColor(Color.RED);
int color = Color.argb( 200, 255, 64, 64 );
text.setBackgroundColor( color );
}
return mView;
}
}
The list item looks like this (custom_list.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>
Use the TextView api's to decorate your text to your liking
and you will be using it like this
listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);
If you really want to use androids simple list layout, we see that they declare their textview's identifier as such:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/dropDownItemStyle"
android:textAppearance="?android:attr/textAppearanceLargeInverse"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
So just make your custom adapter, inflate their layout & find that text view's id. Something like the following:
public final class CustomAdapter extends ArrayAdapter<String> {
private Context context;
ViewHolder holder;
private ArrayList<String> messages;
public CustomAdapter(Context context, ArrayList<String> data) {
this.context = context;
this.messages = data;
Log.d("ChatAdapter", "called constructor");
}
public int getCount() {
return messages.size();
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.simple_dropdown_item_1line, null);
holder = new ViewHolder();
holder.message = (TextView) convertView
.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.message.setText(data.get(position));
holder.message.setTextColor(Color.BLUE);
// don't do this, remember the face variable once just showing explicitly for u
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
holder.message.setTypeface(face);
return convertView;
}
public static class ViewHolder {
public TextView message;
}
}
Edit: The following is how you would use the custom array adapter in your activity.
// I'm not sure if your sp or lv1 wants the adapter, but
// whatever you require your list view's data to be just
// set the custom adapter to it
CustomAdapter<String> myAdapter = new ArrayAdapter<String>(this, s1);
lv1=(ListView) findViewById(R.id.listView1)
lv1.setAdapter(myAdapter);
You can do like this, add a ListView item in xml,at res/layout/list_item1.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#0000ff"
android:testStyle="italic"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
then,
ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);
sp.setAdapter(ard);
lv1=(ListView) findViewById(R.id.listView1);
ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);// **change R.layout.list_item1**
lv1.setAdapter(ard1);
You have to put the .ttf file of font(which you want to add ) in asstes/fonts/ folder and write code in onCreate method like below. i have puted Chalkduster.ttf in my asstes/fonts/ folder
TextView txttest = (TextView) findViewById(R.id.txttest);
Typeface custom_font = Typeface.createFromAsset(getAssets(),
"fonts/Chalkduster.ttf");
txttest.setTypeface(custom_font);
You can handle clicks of selected item by write code just like below
ListView lvNews = (ListView) findViewById(R.id.lvNews);
lvNews.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// Write your code here
//int newsId = newsArray.get(position).getId();
//Intent i = new Intent(NewsListActivity.this,
// NewsDetailActivity.class);
//i.putExtra(Constants.NEWS_ID, newsId);
//i.putExtra("isFromNotification", false);
//startActivity(i);
}
});
Hoping someone can help. I'm having an issue it seems inflating my view. My goal is to display a list of items in a custom layout using a custom array adapter. I re-worked some of my original code using another SO post, but where my simple adapter worked fine, the new list doesn't display. Please see below:
This is my Fragment class.
HomeFeedFragment.java
public class HomeFeedFragment extends ListFragment {
private ListView listView;
private ArrayList<MainEvent> items;
private MainEventListViewAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_homefeed,
container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//GetEvents is just a test method to return a list of pre-filled event objects
items = GlobalList.GetEvents();
adapter = new MainEventListViewAdapter(getActivity(), android.R.id.list, items);
listView.setAdapter(adapter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
My custom row layout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip" />
</LinearLayout>
<!-- Heading Text -->
<TextView
android:id="#+id/eventheader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Event Description -->
<TextView
android:id="#+id/eventdescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_below="#id/eventheader"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#343434"
android:textSize="10sp"
tools:ignore="SmallSp" />
<!-- Posted Time -->
<TextView
android:id="#+id/eventpostedtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/eventheader"
android:layout_marginRight="5dip"
android:gravity="right"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
</RelativeLayout>
The actual fragment layout:
<?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:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And my custom ArrayAdapter:
public class MainEventListViewAdapter extends ArrayAdapter<MainEvent> {
private Context context;
private List<MainEvent> eventList;
public MainEventListViewAdapter(Context context, int textViewResourceId, List<MainEvent> eventList){
super(context, textViewResourceId, eventList);
this.context = context;
//this.eventList = eventList;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtEventHeader;
TextView txtEventDescription;
TextView txtEventPostedTime;
}
public int getCount(){
if(eventList!=null){
return eventList.size();
}
return 0;
}
public MainEvent getItem(int position){
if(eventList!=null){
return eventList.get(position);
}
return null;
}
public long getItemId(int position){
if(eventList!=null){
return eventList.get(position).hashCode();
}
return 0;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
MainEvent rowItem = getItem(position);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
convertView = inflater.inflate(R.layout.fragment_homefeed_rowlayout, parent, false);
holder = new ViewHolder();
holder.txtEventHeader = (TextView) convertView.findViewById(R.id.eventheader);
holder.txtEventDescription = (TextView) convertView.findViewById(R.id.eventdescription);
holder.txtEventPostedTime = (TextView) convertView.findViewById(R.id.eventpostedtime);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtEventHeader.setText(rowItem.getHeadline());
holder.txtEventDescription.setText(rowItem.getDescription());
holder.txtEventPostedTime.setText(rowItem.getPostedTime());
/*TextView text = (TextView) v.findViewById(R.id.label);
text.setText(m.getHeadline());
ImageView imageView = (ImageView) v.findViewById(R.id.icon);
imageView.setTag(m.getImageURL());
//DownloadImageTask dt = new DownloadImageTask();
dt.execute(imageView);
return v;*/
return convertView;
}
public List<MainEvent> getEventList(){
return eventList;
}
public void setEventList(List<MainEvent> eventList){
this.eventList = eventList;
}
}
^ I initially had an Async Task in this class to retrieve images for each item, but I've removed all of that for now.
I don't get any actual errors in LogCat, no crashes or anything, the View just isn't inflating. Any help is appreciated.
Well this is embarrassing. I found out the issue. Apparently, I forgot to uncomment the line in the adapter constructor that initializes the event list:
//this.eventList = eventList;
I'd commented it out temporarily to fix another bug. Working fine now, but thanks for your suggestion.
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);