I currently have ListFragment with a ArrayAdapter class
The codes are below.
1)I want to implement a checkbox from listview main xml to check all the checkboxes that are present in custom list row xml which is inflated using Array adapter.
2)how to refresh view in this code by clearing all checkboxes when a button is clicked.
listview_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"
android:orientation="vertical"
android:weightSum="10"
android:background="#fff2fff2" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
android:choiceMode="multipleChoice" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/btnin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In" />
<Button
android:id="#+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/checkAll"
android:checked="false" />
</LinearLayout>
</RelativeLayout>
custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/paackage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class
public class FileAdapter extends ArrayAdapter<String>{
private List<String> filelist = null;
private Context context;
CheckBox checkAll;
public FileAdapter(Context _context, int _resource,List<String> _filelist) {
super(_context,_resource,_filelist);
this.context = _context;
this.filelist = _filelist;
this.itemChecked = new boolean[_filelist.size()];
}
private class ViewHolder {
TextView Name;
TextView pName;
CheckBox ck1;
ImageView iconview;
}
#Override
public int getCount() {
return ((null != filelist) ? filelist.size() : 0);
}
#Override
public String getItem(int position) {
return ((null != filelist) ? filelist.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.custom_row, null);
holder = new ViewHolder();
holder.Name = (TextView) view.findViewById(R.id.name);
holder.pName = (TextView) view
.findViewById(R.id.paackage);
holder.ck1 = (CheckBox) view.findViewById(R.id.checkBox1);
holder.iconview = (ImageView) view.findViewById(R.id.app_icon);
view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.appName.setText();
holder.packageName.setText();
holder.iconview.setImageDrawable();
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
holder.ck1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.ck1.isChecked()) {
itemChecked[position] = true;
} else {
itemChecked[position] = false;
}
}
});
return view;
}
}
ListFragment class
public class FragmentA extends ListFragment implements View.OnClickListener {
public ArrayList<String> filelist;
Button in, delete;
ListView lv;
CheckBox cb;
private FileAdapter listadaptor = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
filelist = new ArrayList<String>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View inflatedView = inflater.inflate(R.layout.fragment_app_restore, container, false);
in = (Button) inflatedView.findViewById(R.id.btnin);
delete= (Button) inflatedView.findViewById(R.id.btndelete);
lv = (ListView) inflatedView.findViewById(android.R.id.list);
empty = inflatedView.findViewById(android.R.id.empty);
//calling asynctask to load files here
delete.setOnClickListener(this);
in.setOnClickListener(this);
return inflatedView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
cb = (CheckBox) v.findViewById(R.id.checkBox1);//this is the checkbox in row only
cb.performClick();
if (cb.isChecked()) {
} else {
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btndelete:
break;
case R.id.btnin:
break;
}
}
private class LoadFiles extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
filelist = GetFiles(root_path);
if (filelist != null) {
listadaptor = new AppRestoreFileAdapter(getActivity(),
R.layout.custom_row, filelist);
} else {
// lv.setEmptyView(empty);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
lv.setAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
}
}
Related
The image in the imageview disappears from the scrollview in the ListView. How can i solve it?
public class IzahAdapter extends BaseAdapter {
private ArrayList<IzahVeriModeli> list;
LayoutInflater layoutInflater;
Context context;
holder Holder;
int pos;
public IzahAdapter(Context context, ArrayList<IzahVeriModeli> list) {
this.context = context;
// Layout Inflater tanımlanıyor...
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 View getView(int position, View convertView, ViewGroup parent) {
Holder=null;
View satirView = null;
Button bIngg, bTrr;
ImageView resimm = null;
//
if (convertView == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listview_izahat, parent, false);
Holder = new holder();
Holder.resim = (ImageView) convertView.findViewById(resim);
Holder.bIng = (Button) convertView.findViewById(bIng);
Holder.bTr = (Button) convertView.findViewById(bTr);
Holder.bTr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pos = (Integer)v.getTag();
list.get(pos).setSelected(false);
System.out.println("pip2 = " + Integer.toString(pos));
notifyDataSetChanged();
}
});
Holder.bIng.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer)v.getTag();
list.get(pos).setSelected(true);
System.out.println("pip3 = " + Integer.toString(pos));
notifyDataSetChanged();
}
});
convertView.setTag(Holder);
}
else {
Holder = (holder) convertView.getTag();
}
Holder.bTr.setTag(position);
Holder.bIng.setTag(position);
Holder.resim.setTag(position);
System.out.println("pip1 = " + position);
if (list.get(position).isSelected()) {
Holder.bTr.setVisibility(View.VISIBLE);
Holder.bIng.setVisibility(View.GONE);
notifyDataSetChanged();
} else {
Holder.bTr.setVisibility(View.GONE);
Holder.bIng.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
if (this.list.get(position).getResim()!=null)
{
Holder.resim.setImageBitmap(this.list.get(position).getResim2());
}
else
{
Holder.resim.getLayoutParams().width=0;
}
return convertView;
}
}
The image in Imageview comes from the data base. ImageView is shrinking if it is null, if not it adds image, but when i scroll the listview, things get messed up.
I did a lot of hard work, but i did not get resolved it.
<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="90dp"
android:background="#drawable/satir_arkaplan"
android:orientation="horizontal"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="#+id/resim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:baselineAligned="false"
android:gravity="center_horizontal|center"
android:orientation="vertical"
android:padding="5dp">
<Button
android:id="#+id/bIng"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ing"
android:textSize="9dp"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/bTr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|center"
android:background="#drawable/turk"
android:textSize="10dp" />
</LinearLayout>
Try something like that:
if (this.list.get(position).getResim()!=null) {
Holder.resim.setVisibility(View.Visible);
Holder.resim.setImageBitmap(this.list.get(position).getResim2());
}
else {
Holder.resim.setVisibility(View.Gone);
Holder.resim.setImageresource(0);
}
I am trying to implement a Card view inside grid view. The grid view is showing perfectly; even when I touch on a grid view item it's showing nothing. But when I click on the text which is display below its work. I referred This but not working...
Thanks.
My code is as below,
// Contact_Backup.java
public class Contact_Backup extends AppCompatActivity {
#Bind(R.id.gridview)
GridView gridview;
String[] contact_screen_array;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_backup);
ButterKnife.bind(this);
contact_screen_array = getResources().getStringArray(R.array.TextScreen);
String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen);
String concat = "\nContacts";
gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat));
}
}
//Custom_Adapter
public class Custom_Adapter extends BaseAdapter {
Context contaxt;
String[] Array_text;
String[] Array_icon;
String concat;
LayoutInflater inflater;
public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat)
{
this.contaxt = context;
this.Array_text = Array_text;
this.Array_icon = Array_icon;
this.concat=concat;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return Array_text.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Bind(R.id.Screen_icon)
Button Screen_icon;
#Bind(R.id.Screen_text)
TextView Screen_text;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) contaxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(contaxt);
grid = inflater.inflate(R.layout.new_app_card_layout, null);
ButterKnife.bind(this, grid);
Screen_text.setText(Array_text[position]+concat);
Screen_icon.setText(Array_icon[position]);
Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf" );
Screen_icon.setTypeface(font);
} else {
grid = (View) convertView;
}
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
return grid;
}
}
// contact_backup.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="fill_parent"
android:background="#drawable/main_backgorund"
>
<RelativeLayout
android:background="#color/transparent"
android:padding="5.0dip"
android:id="#+id/top_bar_screen"
android:layout_weight="0.2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/newclr"
android:id="#+id/tvContacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contacts"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:layout_centerVertical="true" />
<TextView
android:textColor="#color/newclr"
android:id="#+id/tvLastBackup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/last_backup"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="#+id/v1"
android:layout_below="#+id/top_bar_screen"
android:background="#color/newclr"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe"
android:layout_below="#+id/v1"
android:paddingTop="20sp"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_weight="0.6"
>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:layout_height="wrap_content" />
</GridLayout>
<!-- com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="0.2"
android:layout_centerHorizontal="true" /-->
</RelativeLayout>
// new_app_card_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/fssr_card"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
card_view:cardBackgroundColor="#color/transparent"
>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:id="#+id/Contacts_Button_Layout"
android:background="#drawable/linearlayout_normal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="-1.0dip"
android:layout_weight="1.0"
>
<Button
android:textSize="35.0sp"
android:textColor="#color/newclr"
android:layout_gravity="center"
android:id="#+id/Screen_icon"
android:background="#null"
android:padding="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/fa_icon_contact"
android:drawablePadding="5.0dip"
android:textAllCaps="false" />
<TextView
android:textSize="15.0sp"
android:textColor="#color/white"
android:layout_gravity="center"
android:textAlignment="center"
android:id="#+id/Screen_text"
android:background="#color/newclr"
android:padding="5sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/contactsbackup"
android:textAllCaps="false"/>
</LinearLayout>
</android.support.v7.widget.CardView>
use this method for clicking on a gridview item:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your action when gridView item is clicked
}
});
Set the listener on the actual gridview object.
gridView.setOnItemClickListener(new OnItemClickListener()...);
Try this,
public class Contact_Backup extends AppCompatActivity {
#Bind(R.id.gridview)
GridView gridview;
String[] contact_screen_array;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_backup);
ButterKnife.bind(this);
contact_screen_array = getResources().getStringArray(R.array.TextScreen);
String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen);
String concat = "\nContacts";
gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat));
gridview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<contact_screen_array.length;i++) {
Log.d("TAG::", "Position"+contact_screen_array[i]);
}
}
});
}
}
Finally, I succeed. here is the code.
Just use Textview instead of Button in // new_app_card_layout.xml.
Just Update it,
<TextView
android:textSize="35.0sp"
android:textColor="#color/newclr"
android:layout_gravity="center"
android:id="#+id/Screen_icon"
android:background="#null"
android:padding="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/fa_icon_contact"
android:drawablePadding="5.0dip"
android:textAllCaps="false"
android:textAlignment="center" />
Thank You so much, All of you for help... :)
Try this i have updated your custom Adapter class.please check again with this updated code.
public class Custom_Adapter extends BaseAdapter {
Context contaxt;
String[] Array_text;
String[] Array_icon;
String concat;
LayoutInflater inflater;
public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat)
{
this.contaxt = context;
this.Array_text = Array_text;
this.Array_icon = Array_icon;
this.concat=concat;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return Array_text.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Bind(R.id.Screen_icon)
Button Screen_icon;
#Bind(R.id.Screen_text)
TextView Screen_text;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) contaxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(contaxt);
grid = inflater.inflate(R.layout.new_app_card_layout, null);
ButterKnife.bind(this, grid);
Screen_text.setText(Array_text[position]+concat);
Screen_icon.setText(Array_icon[position]);
Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf" );
Screen_icon.setTypeface(font);
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
} else {
grid = (View) convertView;
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
}
return grid;
}
}
Hope this helps you out !
I want to delete my listview items when I press delete button but the problem is I should first press listview row then press delete button to remove it. I want to delete items immediately after pressing delete button not first press on listview then press delete button
Can you help me in this situation?
and this is my code
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wholeshop);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// getting values from selected ListItem
final View view = v;
final int pos =position;
deleteone= (Button)v.findViewById(R.id.bdeleteone);
deleteone.getTag(position);
deleteone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
deleteone.setTag(view);
productsList.remove(pos);
ListAdapter adapter = new SimpleAdapter(
wholeShop.this, productsList,
R.layout.listshop, new String[] { "name",
"spinn","price"},
new int[] { R.id.wholename, R.id.wholespinn,R.id.wholeprice });
// updating listview
setListAdapter(adapter);
}
});
}
});
}
and this is my xmlfile for listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:layout_marginTop="50dp"
android:descendantFocusability="blocksDescendants"
>
</ListView>
and:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#ffffff"
android:weightSum="100">
<TextView
android:id="#+id/wholename"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="40"
android:paddingLeft="5dip"
android:text="Flower Name"
android:textColor="#000000" />
<TextView
android:id="#+id/wholespinn"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:paddingRight="10dip"
android:text="Flower spinn"
android:textColor="#000000"
android:layout_weight="30"/>
<TextView
android:id="#+id/wholeprice"
android:text="Flower price"
android:layout_gravity="right"
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_weight="30"/>
<Button
android:id="#+id/bdeleteone"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Del"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:focusable="false"
/>
</LinearLayout>
You are going with wrong way.
You may have to look this Example.
You have create Your Own Adapter like:
public class ListAdapter extends ArrayAdapter<String> {
customButtonListener customListner;
public interface customButtonListener {
public void onButtonClickListner(int position,String value);
}
public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
}
private Context context;
private ArrayList<String> data = new ArrayList<String>();
public ListAdapter(Context context, ArrayList<String> dataItem) {
super(context, R.layout.child_listview, dataItem);
this.data = dataItem;
this.context = context;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.child_listview, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView
.findViewById(R.id.childTextView);
viewHolder.button = (Button) convertView
.findViewById(R.id.childButton);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
viewHolder.text.setText(temp);
viewHolder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position,temp);
}
}
});
return convertView;
}
public class ViewHolder {
TextView text;
Button button;
}
}
I have two button at bottom i.e after listview ends.
I am using a custom listview which display list items with alternate colors.
how to set setOnClickListener for both button at bottom?
public class PieMainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRes = getResources();
new Random(new Date().getTime());
solbtn = (Button)findViewById(R.id.solution);
String recive ;
Bundle b = getIntent().getExtras();
final int piewrong=b.getInt("piewrong");
final int pieright=b.getInt("pieright");
final int pieunclick=b.getInt("pieunclick");
setContentView(R.layout.piechart_result);
recive = pieright+"/20";
mPie.setUnit(recive);
data1 = new ArrayList<String>();
fillData() ;
listtotal =getIntent().getStringArrayListExtra("listtotal");
timeuse =getIntent().getStringArrayListExtra("timeused");
adapter1 = new ListAdapter(this, data1, listtotal,timeuse);
ListView lvMain = (ListView) findViewById(R.id.list);
lvMain.setAdapter(adapter1);
lvMain.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
int questionno = position+1;
Bundle b = new Bundle();
b.putInt("questionno",questionno);
b.putInt("piewrong",piewrong);
b.putInt("pieright",pieright);
b.putInt("pieunclick",pieunclick);
Intent in=new Intent(PieMainActivity.this,Solution.class);
in.putStringArrayListExtra("listtotal", (ArrayList<String>) listtotal);
in.putStringArrayListExtra("timeused", (ArrayList<String>) timeuse);
in.putExtras(b);
startActivity(in);
finish();
}
});
}
void fillData() {
for (int i = 1; i <= 20; i++) {
data1.add("Question " + i);
}
}
void fillcmp() {
for (int i = 1; i <= 20; i++) {
cmp.add("cmp " + i);
}
}
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.staritsolutions.apptitude"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_weight="1.0">
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<Button
android:id="#+id/home"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginLeft="6dp"
android:layout_marginRight="3dp"
android:textSize="12dp"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:text="Main Menu"
android:background="#drawable/btn_blue"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="#+id/solution"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginLeft="6dp"
android:layout_marginRight="3dp"
android:textSize="12dp"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:text="Solutions"
android:background="#drawable/btn_blue"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
BaseAdapter file
public class ListAdapter extends BaseAdapter{
Context ctx;
LayoutInflater lInflater;
List<String> data1;
List<String> cmp;
List<String> timeused;
ListAdapter(Context context, List<String> data1 ,List<String> cmp ,List<String> timeused) {
ctx = context;
this.data1 = data1;
this.cmp = cmp;
this.timeused = timeused;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data1.size();
}
public int getCount1() {
return cmp.size();
}
#Override
public Object getItem(int position) {
return data1.get(position);
}
public Object getItem1(int position1) {
return cmp.get(position1);
}
#Override
public long getItemId(int position) {
return position;
}
public long getItemId1(int position1) {
return position1;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.listitem, parent, false);
}
/*if (position % 2 == 0) {
view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
} else {
view.setBackgroundResource(R.drawable.artists_list_background_alternate);
}*/
TextView text = (TextView) view.findViewById(R.id.heading);
int no = position;
if(cmp.get(position).equals("GREEN"))
{
text.setTextColor(Color.parseColor("#00D50E"));
}else if(cmp.get(position).equals("RED"))
{
text.setTextColor(Color.parseColor("#e84040"));
}else
{
text.setTextColor(Color.parseColor("#B441E9"));
}
((TextView) view.findViewById(R.id.heading)).setText(data1.get(position));
((TextView) view.findViewById(R.id.duration)).setText(timeused.get(position));
return view;
//String.valueOf(value);
}
}
So these buttons are not part of your ListView? Well, than in your onCreate do something like:
solbtn = (Button)findViewById(R.id.solution);
solbth.setOnClickListener( toolbar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourMethod();
}
});
And the same goes for the second button.
Or you can make your Activity implement View.OnClickListener, after doing this you can set your click listeners like this:
//this two lines in your OnCreate
button1.setOnClickListener(this);
button2.setOnClickListener(this);
//somewhere later in the code
#Override
public void onClick(View v) {
//do something
}
Just add the clickListeners in your onCrete() method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
solbtn = (Button) findViewById(R.id.home);
homebtn = (Button) findViewById(R.id.solution);
solbtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do stuff for solution button
}
});
homebtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// do stuff for home button
}
});
.....
}
My activity contains a textview and under that it has gridview elements. Stage 1: When the activity starts, it adjusts the elements in the order that I have set(like item1, item2,..) stage1 picture. Stage 2: But when I click on edittext, the gridview elements get adjusted to allocate space for the softkeypad stage2 picture. Stage 3: So when I press back button, the soft keypad disappears(as usual), but the order of gridview elements are getting changed stage3 picture. The order of child items of gridview changes. Can anyone please suggest me how to avoid this uncertainty in the order? Below is my entire code and xml file, though I think these are not that much necessary to solve this issue.
public class PresentActivity extends Activity {
GridView gridView;
static final String[] TEXT = new String[] {
"item1", "item2","item3", "item4", "item5","item6","item7","item8" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setGridView();
}
public void setGridView()
{
gridView = (GridView)findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, TEXT));
gridView.setNumColumns(3);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String currentText=(String) ((TextView)v.findViewById(R.id.grid_item_label)).getText();
Toast.makeText(getApplicationContext(), currentText, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is my custom adapter class.
public class ImageAdapter extends BaseAdapter{
private Context context;
private final String[] textValues;
public ImageAdapter(Context context, String[] textValues) {
this.context = context;
this.textValues = textValues;
}
#Override
public int getCount() {
return textValues.length;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
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);
// get layout from imagewithtext.xml
gridView = inflater.inflate(R.layout.imagewithtext, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(textValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String extractedText = textValues[position];
System.out.println("the position value is>>>>>>"+position);
if (extractedText.equals("item1")) {
imageView.setImageResource(R.drawable.icon);
}
else if (extractedText.equals("item2")) {
imageView.setImageResource(R.drawable.icon);
} else if (extractedText.equals("item3")) {
imageView.setImageResource(R.drawable.icon);
}else if(extractedText.equals("item4")){
imageView.setImageResource(R.drawable.icon);
} else if (extractedText.equals("item5")) {
imageView.setImageResource(R.drawable.icon);
} else if (extractedText.equals("item6")) {
imageView.setImageResource(R.drawable.pic1);
}
else if(extractedText.equals("item7")){
imageView.setImageResource(R.drawable.pic1);
}
else{
imageView.setImageResource(R.drawable.pic1);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
}
xml file which contains gridview:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="#string/button"
android:onClick="button1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="#string/present_location"
/>
</RelativeLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/editText1_hint"
/>
<GridView
android:id="#+id/gridView1"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
</LinearLayout>
Let's assume your items contain just 1 ImageView and 1 TextView (for the sake of simplicity).
Your getView method in the Adapter should do this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
final LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.imagewithtext,null);
//Custom ViewHolder, you have to create it in the same class.
vh = new ViewHolder();
vh.imageView = convertView.findViewById(R.id.imageView1);
vh.textView = convertView.findViewById(R.id.textView1);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.imageView.setImageResource(R.drawable.whatever);
vh.textView.setText("Whatever text you want to set here");
return gridView;
}
And in your adapter, just add this:
private class ViewHolder{
public ViewHolder(){}
public ImageView imageView;
public TextView textView;
//Add any views you want to use in getView here
}
GridViewActivity:-(This is main Activity)
public class GridVieweActivity extends Activity {
GridView gridView;
static final String[] TEXT = new String[] { "item1", "item2", "item3",
"item4", "item5", "item6", "item7", "item8" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setGridView();
}
public void setGridView() {
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, TEXT));
gridView.setNumColumns(3);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String currentText = (String) ((TextView) v
.findViewById(R.id.textView1)).getText();
Toast.makeText(getApplicationContext(), currentText,
Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter:-
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] textValues;
public ImageAdapter(Context context, String[] textValues) {
this.context = context;
this.textValues = textValues;
}
#Override
public int getCount() {
return textValues.length;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
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);
// get layout from imagewithtext.xml
gridView = inflater.inflate(R.layout.imagewithtext, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.textView1);
textView.setText(textValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView1);
String extractedText = textValues[position];
System.out.println("the position value is>>>>>>" + position);
if (extractedText.equals("item1")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item2")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item3")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item4")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item5")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item6")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (extractedText.equals("item7")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else {
imageView.setImageResource(R.drawable.ic_launcher);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
}
main.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" >
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="button1"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Present Location" />
</RelativeLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hint" />
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
</LinearLayout>
imagewithtext:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ImageView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:ellipsize="marquee"
android:text="TextView"
android:textSize="18sp" >
</TextView>
</RelativeLayout>
The above code is working fine.