I had a problem I'm programing my app in which I wants to use dynamically set the images from drawable.
means I want to use like: flag.setImageResource(R.drawable.+my_variable+);
this is my code:
public class AlphabetAdapter extends BaseAdapter {
private Context context;
private final String[] alphabets;
public AlphabetAdapter(Context context, String[] alphabets) {
this.context = context;
this.alphabets = alphabets;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.custom_six_alphabets, null);
TextView textView = (TextView) gridView.findViewById(R.id.label);
textView.setText(alphabets[position]);
ImageView flag = (ImageView) gridView .findViewById(R.id.flag);
String mobile = alphabets[position];
if (mobile.equals("apple")) {
flag.setImageResource(R.drawable.apple);
} else if (mobile.equals("ant")) {
flag.setImageResource(R.drawable.ant);
} else if (mobile.equals("aeroplane")) {
flag.setImageResource(R.drawable.airplane);
} else if (mobile.equals("alligator")) {
flag.setImageResource(R.drawable.alligator);
} else if (mobile.equals("arrow")) {
flag.setImageResource(R.drawable.arrow);
} else {
flag.setImageResource(R.drawable.arm);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
You can try something like:
int i = context.getResources().
getIdentifier(mobile, "drawable", this.getPackageName());
flag.setImageResource(i);
Above code: will 1st fetch the id for mobile from drawable which is equivalent to R.drawable.{mobile} for example if mobile is "ant" code will return id for drawable ant i.e. R.drawable.ant and in 2nd line we will use this id and set Image Resource..
You can try changing your array for int type.
Replace this:
String[] alphabets;
For this:
int[] alphabets;
And in your code you can use this line:
rh.imageviewLeft.setBackgroundResource(alphabets[position]);
Note: In the array that you send fill it for example with this:
int [] img = new int [] { R.drawable.apple, R.drawable.ant }
And add in your Adapter this:
#Override
public int getCount()
{
return alphabets.length;
}
#Override
public Object getItem(int position)
{
return alphabets[position];
}
#Override
public long getItemId(int position)
{
return position;
}
I hope help, and sorry for my bad english.
Try this ..
in your activity
public static HashMap<String, Integer> ThemeId = new HashMap<String, Integer>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
ThemeId.clear();
ThemeId.put("apple", R.drawable.apple);
ThemeId.put("ant", R.drawable.ant);
ThemeId.put("aeroplane", R.drawable.aeroplane);
ThemeId.put("alligator", R.drawable.alligator);
ThemeId.put("arrow", R.drawable.arrow);
ThemeId.put("arm", R.drawable.arm);
}
//in base adapter
import android.content.Context;
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 AlphabetAdapter extends BaseAdapter {
private Context context;
private final String[] alphabets;
public AlphabetAdapter(Context context, String[] alphabets) {
this.context = context;
this.alphabets = alphabets;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
gridView = inflater.inflate(R.layout.custom_six_alphabets, null);
TextView textView = (TextView) gridView.findViewById(R.id.label);
textView.setText(alphabets[position]);
ImageView flag = (ImageView) gridView.findViewById(R.id.flag);
String mobile = alphabets[position];
flag.setImageResource(YourActivity.ThemeId.get(mobile));
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return alphabets.length;
}
#Override
public String getItem(int position) {
return alphabets[position];
}
#Override
public long getItemId(int position) {
return position;
}
}
Related
I want to display String[] arrayTitles on top of String[] arrayDescriptions, which should be sub items, in the list view. How am I supposed to do this? Posting a working version of this code would be much appreciated.
ListView listview = (ListView)findViewById(R.id.listview);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_list);
String[] arrayTitles = formattedTitles.split(",");
String[] arrayDescriptions = formattedDescriptions.split(",");
for (int i = 0; i < arrayLocations.length; i++) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(context, android.R.layout.simple_list_item_1, arrayTitles);
listview.setAdapter(arrayAdapter);
}
}
I am posting answer using Custom Adapter, you can just create one class CustomAdapter.java
package com.example.your.package.name;
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 CustomAdapter extends BaseAdapter {
Context mContext;
String[] arrayTitles;
String[] arrayDescriptions;
public CustomAdapter(Context mContext,String[] arrayTitles,String[] arrayDescriptions)
{
this.mContext = mContext;
this.arrayTitles = arrayTitles;
this.arrayDescriptions = arrayDescriptions;
}
#Override
public int getCount() {
return arrayTitles.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(parent.getContext());
v = vi.inflate(R.layout.item_list_row, null);
}
TextView tvTitle = (TextView) v.findViewById(R.id.txtTitle);
TextView tvDesc = (TextView) v.findViewById(R.id.txtDesc);
tvTitle.setText(arrayTitles[position]);
tvDesc.setText(arrayDescriptions[position]);
return v;
}
}
create one item_list_row.xml
<?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">
<TextView
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:padding="5dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description"
android:paddingLeft="5dp"
android:textStyle="normal"/>
</LinearLayout>
and replace ArrayAdapter with CustomAdapter in your Activity(having ListView)
your onCreate method will look like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
ListView listview = (ListView)findViewById(R.id.listview);
String[] arrayTitles = formattedTitles.split(",");
String[] arrayDescriptions = formattedDescriptions.split(",");
CustomAdapter arrayAdapter = new CustomAdapter(ListViewActivity.this, arrayTitles,arrayDescriptions);
listview.setAdapter(arrayAdapter);
}
I think It will work fine, If it will not work let me suggest what else you want, I will edit my answer
Thank you
Use two types view
I posting working code below main logic in getview().
public class FriendsList extends BaseAdapter{
private Context context;
private final int Separater_Type = 0;
private final int List_Type = 1;
ArrayList< FriendsInfo> list=new ArrayList<FriendsInfo>();
private ActiveList mActiveList = null;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
public FriendsList(Context context,ArrayList<FriendsInfo> list,ActiveList activeList){
this.context=context;
mActiveList = activeList;
this.list=list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return list.get(position).getType();
}
ViewHolder holder = null;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
int rowType;
rowType = list.get(position).type;
if (convertView == null) {
holder = new ViewHolder();
switch (rowType){
case Separater_Type:
convertView = View.inflate(context, R.layout.activelistheader, null);
holder.headerName = (TextView) convertView.findViewById(R.id.list_separator);break;
case List_Type:
convertView = View.inflate(context, R.layout.rowlist, null);
holder.avatar = (CircleImageView)convertView.findViewById(R.id.avatarimage);
holder.headerName = (TextView) convertView.findViewById(R.id.name);
holder.selectMark = (ImageView) convertView.findViewById(R.id.select_img);
//holder.select_img = (ImageView)convertView.findViewById(R.id.select_img);
break;
}
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
if(list.get(position).type ==0){
holder.headerName.setText(list.get(position).name);
}else{
}
return convertView;
}
private class ViewHolder {
CircleImageView avatar;
TextView headerName;
ImageView selectMark;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return true;
}
}
This is my adapter class.i retrieved data from array.xml and whenever i set in grid view it doesn't comes.i am using custom adapter for that
public class SlokaAdapter extends BaseAdapter
{
private Context context;
private final String[] web;
//private final int[] Imageid;
public SlokaAdapter(Context context, String[] web) {
this.context = context;
this.web = web;
}
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
//grid = new View(context);
grid = inflater.inflate(R.layout.custom_slokalist, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_sloka);
} else {
grid = (View) convertView;
}
return grid;
}
#Override
public int getCount() {
return web.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
}
and this is my code in program and it throws same problem as above
public class Slokaa extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shlokalist);
GridView gridView1=(GridView)findViewById(R.id.grid_viewStyle);
final String[] testArray1 = getResources().getStringArray(R.array.Chapter1homelist);
final String[] testArray2=getResources().getStringArray(R.array.Chapter2homelist);
if(chNo.equalsIgnoreCase("chapter 1")){
gridView1.setAdapter( new SlokaAdapter(this, testArray2));
String[] chp_names = getResources().getStringArray(R.array.ChapterName);
String tabname1 = chp_names[1];
txtChapterName.setText(tabname1);
}
I am trying to design a menu like this:
It almost works, however there is a problem. The menu works fine at start, but after scrolling down and up again, the items are gone and downloaded again and it takes a little time. Is there a way to set a number of elements to create at start and keep them from disappearing?
Here is my code:
public class MyActivity extends Activity implements AdapterView.OnItemClickListener {
String headers[];
String image_urls[];
List<MyMenuItem> menuItems;
ListView mylistview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
menuItems = new ArrayList<MyMenuItem>();
headers = getResources().getStringArray(R.array.header_names);
image_urls = getResources().getStringArray(R.array.image_urls);
for (int i = 0; i < headers.length; i++) {
MyMenuItem item = new MyMenuItem(headers[i], image_urls[i]);
menuItems.add(item);
}
mylistview = (ListView) findViewById(R.id.list);
MenuAdapter adapter = new MenuAdapter(this, menuItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
}
}
public class MyMenuItem {
private String item_header;
private String item_image_url;
public MyMenuItem(String item_header, String item_image_url){
this.item_header=item_header;
this.item_image_url=item_image_url;
}
public String getItem_header(){
return item_header;
}
public void setItem_header(String item_header){
this.item_header=item_header;
}
public String getItem_image_url(){
return item_image_url;
}
public void setItem_image_url(String item_image_url){
this.item_image_url=item_image_url;
}
}
public class MenuAdapter extends BaseAdapter{
Context context;
List<MyMenuItem> menuItems;
MenuAdapter(Context context, List<MyMenuItem> menuItems) {
this.context = context;
this.menuItems = menuItems;
}
#Override
public int getCount() {
return menuItems.size();
}
#Override
public Object getItem(int position) {
return menuItems.get(position);
}
#Override
public long getItemId(int position) {
return menuItems.indexOf(getItem(position));
}
private class ViewHolder {
ImageView ivMenu;
TextView tvMenuHeader;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();
holder.tvMenuHeader = (TextView) convertView.findViewById(R.id.tvMenuHeader);
holder.ivMenu = (ImageView) convertView.findViewById(R.id.ivMenuItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyMenuItem row_pos = menuItems.get(position);
Picasso.with(context)
.load(row_pos.getItem_image_url())
// .placeholder(R.drawable.empty)
// .error(R.drawable.error)
.into(holder.ivMenu);
holder.tvMenuHeader.setText(row_pos.getItem_header());
Log.e("Test", "headers:" + row_pos.getItem_header());
return convertView;
}
}
Possibly you can implement lazy loading with some sort of caching of images or whatever you are downloading...
You say the images are donwloaded, i assume that the images are coming from internet. Implement a custom cache for http download or download and store the images in one place before you use
Why is my code not working? I have already tried using notifyDataSetChanged(), or using invalid() for my listview, or using .setAdapter() again for my adapter, but its still not working please help me.
public class ListDel extends Activity{
ListView mylist_del;
ListViewAdapter listViewAdapter_del;
TypedArray images_del;
ImageView bin_image;
private static String[] names, ages;
storageListImplementation lists = new storageListImplementation();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.del_layout);
names = lists.getNames();
ages = lists.getAges();
images_del = getResources().obtainTypedArray(R.array.icons);
bin_image = (ImageView)findViewById(R.id.del_image_icon);
listViewAdapter_del = new ListViewAdapter(this);
mylist_del = (ListView)findViewById(R.id.listview_delete);
mylist_del.setAdapter(listViewAdapter_del);
}
private class ListViewAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public ListViewAdapter(Context context){
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return names.length;
}
public Object getItem(int pos) {
return pos;
}
public long getItemId(int position) {
return position;
}
private OnClickListener binClick(ImageView img, final int position, String name) {
return new View.OnClickListener(){
public void onClick(View v) {
lists.del(names[position]);
mylist_del.invalidate();
//mylist_del.setAdapter(listViewAdapter_del);
listViewAdapter_del.notifyDataSetInvalidated();
listViewAdapter_del.notifyDataSetChanged();
//newlistviewadap();
//mylist_del.setAdapter(new ListViewAdapter(this));
//listViewAdapter_del.notifyDataSetChanged();
//String addSuccessful = "You have successfully deleted "+names[position]+" and his/her information in you database";
//Toast.makeText(getApplicationContext(), addSuccessful, Toast.LENGTH_LONG).show();
//ListDel.this.finish();
}
};
}
public void newlistviewadap(){
mylist_del.setAdapter(listViewAdapter_del);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder textView;
if(convertView==null){
convertView = mInflater.inflate(R.layout.del_listview_row, null);
textView = new ViewHolder();
textView.text = (TextView) convertView.findViewById(R.id.del_values_row);
textView.age = (TextView) convertView.findViewById(R.id.del_age_row);
textView.image = (ImageView) convertView.findViewById(R.id.del_image);
textView.delbutton = (ImageView) convertView.findViewById(R.id.del_image_icon);
convertView.setTag(textView);
}else{
textView = (ViewHolder) convertView.getTag();
}
textView.text.setText(names[position]);
textView.age.setText(ages[position]);
textView.image.setImageDrawable(images_del.getDrawable(position));
textView.delbutton.setOnClickListener(binClick( textView.delbutton, position, names[position]));
return convertView;
}
}
static class ViewHolder{
TextView text;
TextView age;
ImageView image;
ImageView delbutton;
}
}
Try removing
listViewAdapter_del.notifyDataSetInvalidated();
Once you've called it, mylist_del will not retrieve more data from listViewAdapter_del, so only keep the code below and try again:
lists.del(names[position]);
listViewAdapter_del.notifyDataSetChanged();
I have a problem with EditText fields in a ListActivity.
The code complies all right, but the functionality is strange, typing in the first field and hiding the keyboard after this the text appears in another editfield.
Help me with my logical issue
package com.example.helloandroid;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class AddComp extends ListActivity {
static final int DATE_DIALOG_ID = 0;
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private String[] attitude_names;
private String[] attitude_values;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME);
attitude_values = new String[attitude_names.length];
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null);
holder = new ViewHolder();
holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name);
holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.Attitude_Name.setText(attitude_names[position]);
holder.Attitude_Value.setHint(attitude_names[position]);
attitude_values[position] = holder.Attitude_Value.getText().toString();
return convertView;
}
class ViewHolder {
TextView Attitude_Name;
EditText Attitude_Value;
}
#Override
public int getCount() {
return attitude_names.length;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
setContentView(R.layout.addcomp);
}
}
Problem solved by adding Entry in manifest and the use of a TextWatcher (this is needed because the view of one list row is internly called several times that means that for 500 list entries the programm uses only afew intances of the row.view-class to be mor efficient)
therfore it is need to use a text watcher that saves the changed data in a extra datastructur for exsample an array..
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private String[] attitude_names;
public String[] attitude_values;
private String name;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME);
attitude_values = new String[attitude_names.length];
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null);
holder = new ViewHolder();
holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name);
holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value);
holder.Attitude_Value.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable edt)
{
attitude_values[holder.ref] = edt.toString();
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//attitude_values[ref] = Attitude_Value.getText().toString();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref=position;
holder.Attitude_Name.setText(attitude_names[position]);
holder.Attitude_Value.setHint(attitude_names[position]);
holder.Attitude_Value.setText(attitude_values[position]);
return convertView;
}
class ViewHolder {
TextView Attitude_Name;
EditText Attitude_Value;
int ref;
}
#Override
public int getCount() {
return attitude_names.length;
}
}
This will help you
``
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private String[] attitude_names;
public String[] attitude_values;
private String name;
public static HashMap<Integer,String> myList=new HashMap<Integer,String>();
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME);
attitude_values = new String[attitude_names.length];
}
// initialize myList
for(int i=0;i<attitude_names.length;i++)
{
myList.put(i,"");
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null);
holder = new ViewHolder();
holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name);
holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value);
holder.Attitude_Value.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable edt)
{
myList.put(pos,s.toString.trim());
attitude_values[holder.ref] = edt.toString();
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//attitude_values[ref] = Attitude_Value.getText().toString();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref=position;
holder.Attitude_Name.setText(attitude_names[position]);
holder.Attitude_Value.setHint(attitude_names[position]);
holder.Attitude_Value.setText(myList.get(position));
return convertView;
}
class ViewHolder {
TextView Attitude_Name;
EditText Attitude_Value;
int ref;
}
#Override
public int getCount() {
return attitude_names.length;
}
}
Here I have included a HashMap object which will keep on eye of what EditText contains value.And when you scroll the listview,it will be rendered again by calling its getView method.
In this code,when you firstly load listview,all your edittext will be with no text.once you enter some text,it will be noted in myList.So when you again render the list,your text would be prevented.
I found the reason the behaviour of the strange focusing
one has to add android:windowSoftInputMode="adjustPan" as value to the activity in the Projects Manifest
but on the other hand the problem that often more than one value of the edidfield is changed is not solved rightnow