I have List view in layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<ListView
android:id="#+id/listInbox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:layout_weight="20"
android:listSelector="#drawable/list_selector" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="80"
android:orientation="horizontal" >
<Button
android:id="#+id/bdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="#string/delete_selected" />
</LinearLayout>
</LinearLayout>
and This is the layout adapter for this list view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
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:background="#drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip" />
</LinearLayout>
<!-- Title Of Song -->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Title"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<!-- Artist Name -->
<TextView
android:id="#+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:gravity="left"
android:text="Sender"
android:textColor="#343434"
android:textSize="15sp" />
<!-- Rightend Duration -->
<!-- Rightend Arrow -->
<ImageView
android:id="#+id/ivArraw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/url" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/artist"
android:layout_alignBottom="#+id/artist"
android:layout_alignRight="#+id/artist"
android:visibility="invisible"
/>
</RelativeLayout>
and that what the adapter defined in my activity
lv.setAdapter(new ImageInboxAdapter(inbox.this, bitmap, messageNo, messageTitle, senderId, senderName,selected));
and this is the baseadapter code
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageNewsAdapter extends BaseAdapter{
Context context;
String[] newsNo,title_news;
Bitmap[] bitmap;
public ImageNewsAdapter(Context context,String[] newsNo,String[] title_news, Bitmap[] bitmap ) {
this.context=context;
this.newsNo=newsNo;
this.title_news=title_news;
this.bitmap=bitmap;
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return title_news.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertView==null){
gridView=new View(context);
gridView=inflater.inflate(R.layout.custom_news, null);
}else{
gridView = (View) convertView;
}
TextView tv=(TextView)gridView.findViewById(R.id.tvTitle);
tv.setText(title_news[position]);
/*RelativeLayout rlImageMain=(RelativeLayout)gridView.findViewById(R.id.RlImageTitle);
rlImageMain.setBackground(new BitmapDrawable(bitmap[position]));
*/
ImageView ivMain= (ImageView)gridView.findViewById(R.id.ivMain);
ivMain.setImageBitmap(bitmap[position]);
return gridView;
}
}
the question I want to get checked values from checkbox and return it to my activity
I found the solution
first make a static Boolean variable
static Boolean checkboxstate[];
then define it in the constructor with the size of the list
checkboxstate=new Boolean[messageNo.length];
then make your checkbox on click listener and fill the boolean array
if(checkboxstate[position]==null){
checkboxstate[position]=false;
}
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked()){
checkboxstate[position]=true;
v.setSelected(true);
}else{
checkboxstate[position]=false;
v.setSelected(false);
}
}
});
finally
public class ImageInboxAdapter extends BaseAdapter{
Context context;
String[] messageNo,messageTitle,senderId,senderName;
Bitmap[] bitmap;
Boolean selected;
static Boolean checkboxstate[];
int checkedp;
public ImageInboxAdapter(Context context, Bitmap[] bitmap,String[] messageNo,String[] messageTitle, String[] senderId,String[] senderName,Boolean selected) {
this.context=context;
this.bitmap=bitmap;
this.messageNo=messageNo;
this.messageTitle=messageTitle;
this.senderId=senderId;
this.senderName=senderName;
this.selected=selected;
checkboxstate=new Boolean[messageNo.length];
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return messageNo.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#SuppressLint("NewApi")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertView==null){
gridView=new View(context);
gridView=inflater.inflate(R.layout.list_row_message, null);
}else{
gridView = (View) convertView;
}
TextView tvTitle=(TextView)gridView.findViewById(R.id.title);
tvTitle.setText(messageTitle[position]);
TextView tvSender=(TextView)gridView.findViewById(R.id.artist);
tvSender.setText(senderName[position]);
CheckBox cb=(CheckBox)gridView.findViewById(R.id.checkBox1);
ImageView ivArraw=(ImageView)gridView.findViewById(R.id.ivArraw);
/*RelativeLayout rlImageMain=(RelativeLayout)gridView.findViewById(R.id.RlImageTitle);
rlImageMain.setBackground(new BitmapDrawable(bitmap[position]));
*/
ImageView ivMain= (ImageView)gridView.findViewById(R.id.list_image);
ivMain.setImageBitmap(bitmap[position]);
if(selected==true){
cb.setVisibility(CheckBox.VISIBLE);
ivArraw.setVisibility(ImageView.INVISIBLE);
if(checkboxstate[position]==null){
checkboxstate[position]=false;
}
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked()){
checkboxstate[position]=true;
v.setSelected(true);
}else{
checkboxstate[position]=false;
v.setSelected(false);
}
}
});
}
return gridView;
}
}
Related
I want to set images in grid view with multiple columns. Images can be vary in columns.How can i achieve it. Thanks in advance
Images in GridView
create activity_main.xml
<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:orientation="vertical"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="3" >
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</RelativeLayout>][1]][1]
Create MainActivity.
public class MainActivity extends Activity {
GridView gv;
Context context;
ArrayList prgmName;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv=(GridView) findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Create a folder by name drawable in res directory. Insert the images into drawable folder by name as below.
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
Create the layout as programlist.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:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
Create Customclass ie CustomAdapter.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
You will get the Output like..
I am creating a gridview with different images and texts from a class containing those images and texts in a fragment. The GridView is inside a TabHost. The TabHost is working fine. I can see other buttons in the same view. But nothing is inside the GridView. Here is the code:
public class MenuPage extends Fragment{
GridView CategoryGridview;
View MainMenuView, HomePageView, GalleryView;
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Context context = getActivity();
this.MainMenuView = inflater.inflate(R.layout.mainmenupage, container, false);
this.GalleryView = inflater.inflate(R.layout.gallery, container, false);
this.CategoryGridview = (GridView)GalleryView.findViewById(R.id.gridViewGallery);
CategoryGridview.setAdapter(new GalleryAdapter(getActivity()));
:
:
:
return MainMenuView;
}
Adapter:
public class GalleryAdapter extends BaseAdapter{
ArrayList<Gallery> list;
Context context;
GalleryAdapter(Context context){
this.context = context;
list = new ArrayList<Gallery>();
int[] TempGalleryId = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k, R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o, R.drawable.p, R.drawable.q, R.drawable.r, R.drawable.s};
String[] TempGalleryName = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s"};
for (int i = 0; i < 19; i++){
Gallery TempGallery = new Gallery(TempGalleryId[i], TempGalleryName[i]);
list.add(TempGallery);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder holder = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_gallery, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else{
holder = (ViewHolder) row.getTag();
}
Gallery temp = list.get(position);
holder.imageViewGallery.setImageResource(temp.GalleryId);
return row;
}
}
class ViewHolder{
ImageView imageViewGallery;
ViewHolder(View v)
{
imageViewGallery = (ImageView) v.findViewById(R.id.imageViewGallery);
}
}
class Gallery{
int GalleryId;
String GalleryName;
Gallery(int GalleryId, String GalleryName){
this.GalleryId = GalleryId;
this.GalleryName = GalleryName;
}
}
In mainmenupage.xml, one of the tab include gallery.xml file for animation with a gridview imageViewGallery and some buttons in it.
<LinearLayout
android:orientation="vertical"
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/DinnerList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<include
android:id="#+id/gallery"
layout="#layout/gallery" />
</LinearLayout>
SingleGallery.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageViewGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/a" />
</RelativeLayout>
This is Gallery.xml
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnBack"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Back" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<GridView
android:id="#+id/gridViewGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnBack"
android:numColumns="auto_fit"
android:horizontalSpacing="15dp"
android:verticalSpacing="15dp"
android:stretchMode="spacingWidthUniform">
</GridView>
</RelativeLayout>
I tried change
CategoryGridview.setAdapter(new GalleryAdapter(getActivity()));
to
CategoryGridview.setAdapter(new GalleryAdapter(context));
and
this.CategoryGridview = GridView)MainMenuView.findViewById(R.id.gridViewGallery);
to
this.CategoryGridview = (GridView)GalleryView.findViewById(R.id.gridViewGallery);
still doesn't work.
I also tried open a new project with extend Activity(){}. It work fine with
CategoryGridview.setAdapter(new GalleryAdapter(this));
So I guess the problem is somewhere else. Can anyone help?
In my application, I keep yes and no button in listview as an item. setOnClickListener is defined in Adapter class. I have a problem with this. After I scroll the listview, I see the changed button in another row. How can I resolve my issue ?
public class QuizAdapter extends BaseAdapter {
private static final String FONTH_PATH_1 = "fonts/Brandon_reg.otf";
private static final String FONTH_PATH_2 = "fonts/Brandon_bld.otf";
private ArrayList<Question> questionList = new ArrayList<Question>();
private LayoutInflater inflater;
private Context context;
public QuizAdapter(Context context) {
super();
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
public void addListItem(final Question item) {
questionList.add(item);
notifyDataSetChanged();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return questionList.size();
}
#Override
public Question getItem(int position) {
// TODO Auto-generated method stub
return questionList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Question currentQuestion = getItem(position);
Typeface font1 = Typeface.createFromAsset(context.getAssets(),
FONTH_PATH_1);
Typeface font2 = Typeface.createFromAsset(context.getAssets(),
FONTH_PATH_2);
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.quiz_list, null);
holder.questionCounterTextView = (TextView) convertView
.findViewById(R.id.questionCounterTextView);
holder.questionCounterTextView.setTypeface(font2);
holder.questionTextView = (TextView) convertView
.findViewById(R.id.questionTextView);
holder.questionTextView.setTypeface(font1);
holder.yesButton = (Button) convertView
.findViewById(R.id.yesButton);
holder.yesButton.setTypeface(font2);
holder.noButton = (Button) convertView.findViewById(R.id.noButton);
holder.noButton.setTypeface(font2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.yesButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
holder.yesButton.setBackgroundColor(R.color.Gray);
}
});
holder.questionCounterTextView.setText(Integer.toString(position + 1));
holder.questionTextView.setText(currentQuestion.getQuestionMessage());
return convertView;
}
public static class ViewHolder {
public TextView questionCounterTextView, questionTextView;
public Button yesButton, noButton;
}
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#color/White"
android:orientation="vertical"
tools:context="...." >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<ImageView
android:id="#+id/flagImageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:src="#drawable/flag" />
<TextView
android:id="#+id/questionCounterTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/flagImageView"
android:layout_alignLeft="#+id/flagImageView"
android:layout_alignRight="#+id/flagImageView"
android:layout_alignTop="#+id/flagImageView"
android:layout_marginLeft="10dp"
android:text="1"
android:gravity="center|left"
android:textColor="#color/White"
android:textSize="#dimen/question_counter_text_size"/>
<TextView
android:id="#+id/questionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/flagImageView"
android:text="#string/loremIpsum2"
android:textColor="#color/Blue"
android:textSize="#dimen/text_size" />
</RelativeLayout>
<LinearLayout
android:id="#+id/quizLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:orientation="horizontal"
android:weightSum="640" >
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30" />
<Button
android:id="#+id/yesButton"
style="#style/button_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="275"
android:background="#color/QuizGreen"
android:text="#string/yes"
android:textColor="#color/White"
android:textSize="#dimen/question_button_text_size" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30" />
<Button
android:id="#+id/noButton"
style="#style/button_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="275"
android:background="#color/QuizOrange"
android:text="#string/no"
android:textColor="#color/White"
android:textSize="#dimen/question_button_text_size" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30" />
</LinearLayout>
</LinearLayout>
I solved my problem with defining a new String in Question class which is kept user answer. Controlling yes or no than change the background.
holder.yesButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (currentQuestion.getUserAnswer() == null) {
questionList.get(position).setUserAnswer("TRUE");
notifyDataSetChanged();
} else if (currentQuestion.getUserAnswer().equals("FALSE")) {
questionList.get(position).setUserAnswer("TRUE");
notifyDataSetChanged();
}
}
});
holder.noButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (currentQuestion.getUserAnswer() == null) {
questionList.get(position).setUserAnswer("FALSE");
notifyDataSetChanged();
} else if (currentQuestion.getUserAnswer().equals("TRUE")) {
questionList.get(position).setUserAnswer("FALSE");
notifyDataSetChanged();
}
}
});
if (currentQuestion.getUserAnswer() == null) {
holder.yesButton.setBackgroundColor(context.getResources()
.getColor(R.color.QuizGreen));
holder.noButton.setBackgroundColor(context.getResources().getColor(
R.color.QuizOrange));
} else if (currentQuestion.getUserAnswer().equals("TRUE")) {
holder.yesButton.setBackgroundColor(context.getResources()
.getColor(R.color.NewsFeedDividerGray));
holder.noButton.setBackgroundColor(context.getResources().getColor(
R.color.QuizOrange));
} else {
holder.yesButton.setBackgroundColor(context.getResources()
.getColor(R.color.QuizGreen));
holder.noButton.setBackgroundColor(context.getResources().getColor(
R.color.NewsFeedDividerGray));
}
Have you tried not using the holder, and doing something like this?
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout rl = (RelativeLayout)v.getParent();
Button bttn = (Button)rl.findViewById(R.id.yesButton);
bttn.setBackgroundColor(R.color.Gray);
}
i have an android application that retrieve data from sqlite database and display these data in a listView that extend a BaseAdapter.
in this app i have images in the drawable folder and i have in the sqlite a field that contain name of these images.
my question is how to retrieve these data and display it in a listView ??
i read this tutorial :(http://www.androidhub4you.com/2012/09/hello-friends-today-i-am-going-to-share.html).
my question is there any other way to do this ??
i will appreciate any help .
row_list_match_schedulle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Group"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignParentLeft="true"
android:text="name1" />
<TextView
android:id="#+id/textName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView2"
android:layout_alignParentRight="true"
android:text="Name2" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textLocation"
android:layout_toRightOf="#+id/textName1"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:scaleType="fitCenter"
android:src="#drawable/algeria_flag" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_toLeftOf="#+id/textName2"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:scaleType="fitCenter"
android:src="#drawable/algeria_flag" />
<TextView
android:id="#+id/textLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Location" />
</RelativeLayout>
ItemDetails.java
package com.devleb.expandablelistdemo3;
public class ItemDetails {
String stad_name;
String team1;
String team2;
String match_date;
String flags1;
String flags2;
String group;
public String getStad_name() {
return stad_name;
}
public void setStad_name(String stad_name) {
this.stad_name = stad_name;
}
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getMatch_date() {
return match_date;
}
public void setMatch_date(String match_date) {
this.match_date = match_date;
}
public String getFlags1() {
return flags1;
}
public void setFlags1(String flags1) {
this.flags1 = flags1;
}
public String getFlags2() {
return flags2;
}
public void setFlags2(String flags2) {
this.flags2 = flags2;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
}
this is what i wrote until now in the BaseAdapter
CustomAdapterMatchSchedule.java
package com.devleb.expandablelistdemo3;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapterMatchSchedule extends BaseAdapter {
LayoutInflater layoutInflater;
ArrayList<ItemDetails> itemdetailList;
Context context;
public CustomAdapterMatchSchedule(Context c, ArrayList<ItemDetails> listOfItem){
this.context = c;
itemdetailList = listOfItem;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return itemdetailList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemdetailList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ItemDetails itemListDetails = itemdetailList.get(position);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_list_match_schedule, null);
}
//Stad_name
TextView txtStadName = (TextView)convertView.findViewById(R.id.textLocation);
txtStadName.setText(itemListDetails.getStad_name());
//team1
TextView txtTeam1 = (TextView)convertView.findViewById(R.id.textName1);
txtTeam1.setText(itemListDetails.getTeam1());
//team2
TextView txtTeam2 = (TextView)convertView.findViewById(R.id.textName2);
txtTeam2.setText(itemListDetails.getTeam2());
//flag1
TextView txtflag1 = (TextView)convertView.findViewById(R.id.imageView1);
txtflag1.setText(itemListDetails.getTeam1());
//flag2
return null;
}
}
This is the most appropriate way,especially because each listview row contains a lot of items.If you want to get the items on a clicked row it becomes a walk in the pack,less than 10 lines of code.As far as am concerned,its the best way. Don't get worried,its also what i use.
i am trying to display image using GridView. This is the first time i using GridView, so i using the example from here and implement it to mine (i have tried the example that contained there, and it's works).
But, i have checked it many time, there's no error comes from LogCat, no Force Closed, the image didn't show. i have no idea where's the wrong part.
Here's my code:
choosepic.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bg_inner">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/book_inner"
android:layout_marginTop="50dp"
/>
<ImageButton
android:id="#+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home_btn"
android:background="#null"
/>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/bg_arrow_btn"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="#+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/right_arrow"
android:background="#null"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_marginRight="7dp"
android:layout_marginLeft="7dp"
/>
<ImageButton
android:id="#+id/prevBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/left_arrow"
android:background="#null"
android:layout_toLeftOf="#+id/nextBtn"
android:layout_marginTop="5dp"
/>
<GridView
android:id="#+id/gridView1"
android:numColumns="3"
android:gravity="center"
android:columnWidth="30dp"
android:stretchMode="columnWidth"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="70dp"
>
</GridView>
</RelativeLayout>
animalbutton.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:visibility="gone">
</TextView>
ImageAdapter.java
public class ImageAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<String> listNm;
private ArrayList<Integer> listAnim;
public ImageAdapter(Activity activity,ArrayList<String> listName, ArrayList<Integer> listImage) {
super();
this.listNm = listName;
this.listAnim = listImage;
this.activity = activity;
}
public static class ViewHolder
{
public ImageView imgViewAnim;
public TextView txtViewAnim;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.animalbutton, null);
view.txtViewAnim = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewAnim = (ImageView) convertView.findViewById(R.id.grid_item_image);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewAnim.setText(listNm.get(position));
view.imgViewAnim.setImageResource(listAnim.get(position));
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listNm.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
choosepic.java
public class choosepic extends Activity {
/** Called when the activity is first created. */
ImageAdapter mAdapter;
GridView gridView;
static final String[] animal = new String[] {
"cat", "cow","croc", "duck", "elephant", "giraffe", "lion", "moose", "mouse"};
private ArrayList<String> listNm;
private ArrayList<Integer> listAnim;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choosepic);
gridView = (GridView) findViewById(R.id.gridView1);
prepare_list1();
mAdapter = new ImageAdapter(this,listNm, listAnim);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Toast.makeText(getApplicationContext(), mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
public void prepare_list1(){
listNm = new ArrayList<String>();
listAnim = new ArrayList<Integer>();
for (int i = 0; i < animal.length; i++) {
listNm.add(animal[i]);
listAnim.add(getResources().getIdentifier("anim_"+animal[i], "drawable", getPackageName()));
}
}
}
i need some help. i appreciate any help. thank you in advance!
i think the problem is in your getCount() that returns 0 elements
instead of that make it like this
#Override
public int getCount() {
// TODO Auto-generated method stub
return listNm.size();
}