I have a custom listview in my application. The needs of the user varies with the option he selects and the result is on the listview. I am representing the same list once by using ArrayList<String> and other time, while using ArrayList<HashMap<String,String>>. Now, while using the former i.e. ArrayList<String>, the result in the listView row gets aligned perfectly but then, while using the later i.e. ArrayList<HashMap<String,String>> the alignment gets worse. I am not understanding what the problem can be. Please help!
Below I am posting the images, code and source code of the XML which is used as a row in my custom Listview.
Images Link: (As I am new user I cannot upload images)
Using ArrayList<String>
http://i46.tinypic.com/2r2b0g5.jpg
Using ArrayList<HashMap<String, String>>
http://i47.tinypic.com/11w7tqe.jpg
Code:
For Arraylist< HashMap< String, String>>
ArrayList<HashMap<String,String>> searchList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<productName.size();i++)
{
String currentListItem=productName.get(i).toString();
if(textLength<=currentListItem.length()){
//compare the String in EditText with Names in the ArrayList
if(searchText.equalsIgnoreCase(currentListItem.substring(0,textLength))){
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("PRODUCTNAME",productName.get(i));
temp.put("PRODUCTID", productID.get(i));
temp.put("BRANDNAME", brandName.get(i));
temp.put("VENDORNAME", vendor.get(i));
temp.put("OPENINGSTOCK", openingStock.get(i));
temp.put("AVAILABLESTOCK", availableStock.get(i));
temp.put("ADDSTOCK", addStock.get(i));
temp.put("PURCHASRPRICE", purchasePrice.get(i));
temp.put("SALESPRICE", salesPrice.get(i));
searchList.add(temp);
}
}
}
SimpleAdapter aaadapter = new SimpleAdapter(
new_stock_io.this,
searchList,
R.layout.list_otcmain123,
new String[] {"PRODUCTID", "PRODUCTNAME", "BRANDNAME","VENDORNAME","OPENINGSTOCK","AVAILABLESTOCK","ADDSTOCK","SALESPRICE", "PURCHASEPRICE"},
new int[] {R.id.txt_id,R.id.txt_name, R.id.txt_bname, R.id.txt_vendor, R.id.txt_openingstock,R.id.txt_availablestock,R.id.txt_addstock,R.id.txt_salesprice,R.id.txt_purchaseprice}
);
lv.setAdapter(aaadapter);
For ArrayList< String>
List<String> vendorName = new ArrayList<String>();
List<String> vendorID = new ArrayList<String>();
List<Object> objectArray = new ArrayList<Object>();
List<String> stockID = new ArrayList<String>();
List<String> picture = new ArrayList<String>();
List<String> productID = new ArrayList<String>();
List<String> productName = new ArrayList<String>();
List<String> brandName = new ArrayList<String>();
List<String> vendor = new ArrayList<String>();
List<String> openingStock = new ArrayList<String>();
List<String> availableStock = new ArrayList<String>();
List<String> addStock= new ArrayList<String>();
List<String> purchasePrice= new ArrayList<String>();
List<String> salesPrice= new ArrayList<String>();
List<String> subCategoryID = new ArrayList<String>();
List<String> subCategoryName= new ArrayList<String>();
List<String> productCategory= new ArrayList<String>();
List<String> subCategoriesNameArray = new ArrayList<String>();
List<String> subCategoriesIDArray= new ArrayList<String>();
for (int i = 0; i < productID.size(); i++)
{
Object obj = new Object();
objectArray.add(obj);
}
adapter = new ListItemsAdapter(objectArray, 1);
lv.setAdapter(adapter);
//List Adapter for listView.
public class ListItemsAdapter extends ArrayAdapter<Object> {
ViewHolder holder1;
public ListItemsAdapter(List<Object> items, int x) {
super(new_stock_io.this,android.R.layout.simple_list_item_1, items);
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.list_otcmain123, null);
holder1 = new ViewHolder();
holder1.tv_id=(TextView)convertView.findViewById(R.id.txt_id);
holder1.tv_name=(TextView)convertView.findViewById(R.id.txt_name);
holder1.tv_brandname=(TextView)convertView.findViewById(R.id.txt_bname);
holder1.tv_openingstock=(TextView)convertView.findViewById(R.id.txt_openingstock);
holder1.tv_availablestock=(TextView)convertView.findViewById(R.id.txt_availablestock);
holder1.tv_addstock=(TextView)convertView.findViewById(R.id.txt_addstock);
holder1.tv_addstock.setTag(position);
holder1.tv_addstock.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
// TODO Auto-generated method stub
Object o = (Object)holder1.tv_addstock.getTag();
Log.d("method focus", "call");
Log.d("position", position+"");
Log.d("edit text value", o.toString());
Log.d("value",":"+holder1.tv_addstock.getText().toString());
addStock.set(position, holder1.tv_addstock.getText().toString());
}
});
holder1.tv_prizeperunit=(TextView)convertView.findViewById(R.id.txt_salesprice);
holder1.tv_vendor=(TextView)convertView.findViewById(R.id.txt_vendor);
holder1.tv_prizeperunitpurchase=(TextView)convertView.findViewById(R.id.txt_purchaseprice);
convertView.setTag(holder1);
if (checkboxClicked==0)
{
holder1.tv_addstock.setTag(objectArray.get(position));
holder1.tv_id.setText(productID.get(position));
holder1.tv_name.setText(productName.get(position));
holder1.tv_brandname.setText(brandName.get(position));
holder1.tv_openingstock.setText(openingStock.get(position));
holder1.tv_availablestock.setText(availableStock.get(position));
holder1.tv_addstock.setId(position);
holder1.tv_addstock.setText(addStock.get(position));
holder1.tv_prizeperunit.setText(salesPrice.get(position));
holder1.tv_vendor.setText(vendor_Name);
holder1.tv_prizeperunitpurchase.setText(purchasePrice.get(position));
}
else if (checkboxClicked==1)
{
//holder1.tv_addstock.setTag(objectArray.get(position));
holder1.tv_id.setText(onCheckProductID.get(position));
holder1.tv_name.setText(onCheckProductName.get(position));
holder1.tv_brandname.setText(onCheckBrandName.get(position));
holder1.tv_openingstock.setText(onCheckOpeningStock.get(position));
holder1.tv_availablestock.setText(onCheckAvailableStock.get(position));
holder1.tv_addstock.setId(position);
holder1.tv_addstock.setText(onCheckAddStock.get(position));
holder1.tv_prizeperunit.setText(onCheckSalesPrice.get(position));
holder1.tv_vendor.setText(onCheckVendor.get(position));
holder1.tv_prizeperunitpurchase.setText(onCheckPurchasePrice.get(position));
}
return convertView;
}
}
//Simple holder class for above Adapter class.
public class ViewHolder
{
TextView tv_id,tv_name,tv_brandname,tv_vendor,tv_openingstock,tv_availablestock,tv_addstock,tv_prizeperunit,tv_prizeperunitpurchase;
}
XML:
<LinearLayout
android:id="#+id/ll_list_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txt_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:gravity="center"
android:text="ID"
android:textSize="6dip"
android:textStyle="bold" />
<View
android:id="#+id/view1"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:gravity="center"
android:text="Name"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view2"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<View
android:id="#+id/view3"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" /> -->
<TextView
android:id="#+id/txt_bname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:gravity="center"
android:text="Brand Name"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view3"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_vendor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:gravity="center"
android:text="Vendor"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view27"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_openingstock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:gravity="center"
android:text="Opening Stock"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view26"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_availablestock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:gravity="center"
android:text="Available Stock"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view25"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_addstock"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.10"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" />
<View
android:id="#+id/view24"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_salesprice"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.10"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" android:width="7dp" />
<View
android:id="#+id/view24"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_purchaseprice"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.12"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" android:width="7dp"/>
</LinearLayout> </RelativeLayout>
Related
Hello my customlist view shows only one record. Everything is fine it shows multiple records in console but when ever i try to show the records in ListView it shows only last record its overriding first record, currently there are two records which are showing in my console but in ListView it shows only last record, I am searching for solution since yesterday , I have spend more then 6 hours to solve this problem.
Cart_Adapter.java
public class Cart_Adapter extends BaseAdapter {
Activity activity;
ArrayList<ProductBean> list;
String abc;
public Cart_Adapter(Activity activity, ArrayList<ProductBean> list) {
this.activity = activity;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v=view;
if(v==null){
LayoutInflater layoutInflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=layoutInflater.inflate(R.layout.cartxml,null);
}
TextView title=(TextView) v.findViewById(R.id.title);
TextView size=(TextView) v.findViewById(R.id.size);
TextView color=(TextView) v.findViewById(R.id.color);
Button remove=(Button) v.findViewById(R.id.remove);
ImageView imageView=(ImageView) v.findViewById(R.id.imageView);
title.setText(list.get(i).getTitle());
size.setText(list.get(i).getSize());
color.setText(list.get(i).getColor());
ArrayList<ImagesBean> aa =list.get(i).getImagesList();
if(aa!=null){
for(i=0; i<aa.size(); i++) {
abc = String.valueOf(aa.get(i).getImgone());
System.out.println("--testing" + abc);
}
}
System.out.println("===="+abc);
if(abc !=null ) {
imageView.setImageBitmap(getBitmapFromString(abc));
// imageView.setImageBitmap(getBitmapFromString(list.get(i).getImagesList()));
}else{}
return v;
}
private Bitmap getBitmapFromString(String encode){
byte[] b = Base64.decode(encode,Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b,0,b.length);
}
}
class from where I am getting database record
ArrayList<String> arrayList=mydatabase.getpid(userid);
System.out.println("--outside"+arrayList);
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
}
Activity_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bhm.ishopping.Cart">
<android.support.constraint.ConstraintLayout
android:id="#+id/customlayout"
android:layout_width="410dp"
android:layout_height="63dp"
android:layout_marginStart="8dp"
android:background="#39a423"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.617"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView15"
android:layout_width="86dp"
android:layout_height="29dp"
android:layout_marginStart="320dp"
android:textColor="#android:color/holo_blue_bright"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="16dp" />
</android.support.constraint.ConstraintLayout>
<TextView
android:id="#+id/textView19"
android:layout_width="171dp"
android:layout_height="34dp"
android:layout_marginEnd="120dp"
android:layout_marginTop="76dp"
android:text="Your Cart Detail"
android:textColor="#android:color/black"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="#+id/lv"
android:layout_width="373dp"
android:layout_height="411dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/customlayout" />
<Button
android:id="#+id/submit"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_marginStart="76dp"
android:layout_marginTop="40dp"
android:text="Submit Your Order"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lv" />
</android.support.constraint.ConstraintLayout>
Cart.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="99dp"
android:layout_height="87dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/remove"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView"
android:layout_marginStart="15dp"
android:layout_toEndOf="#+id/imageView"
android:text="Ttile"
android:textColor="#android:color/background_dark"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.456"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/textView16"
android:layout_marginTop="102dp"
android:text="Color"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView16" />
<TextView
android:id="#+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/textView17"
android:layout_below="#+id/textView17"
android:text="Size"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView17" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/size"
android:layout_alignTop="#+id/remove"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignStart="#+id/size"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title" />
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_above="#+id/textView18"
android:layout_marginEnd="26dp"
android:layout_toStartOf="#+id/remove"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/color" />
<Button
android:id="#+id/remove"
android:layout_width="46dp"
android:layout_height="67dp"
android:layout_alignBottom="#+id/size"
android:layout_alignParentEnd="true"
android:layout_marginEnd="13dp"
android:background="#5d8f37"
android:text="X"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
console output show two records
enter code here System.out: ===pids1 System.out: ==database record[ProductBean{id='', title='ddd', color='dff', size='Select Size', type='Shart', cost='ddf', imagesList=null System.out: ===pids2 System.out: ==database record[ProductBean{id='', title='aa', color='aa', size='Select Size', type='Select Type', cost='aa', imagesList=null}]
You need to make your cartxml hight to android:layout_height="wrap_content"
like below code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
EDIT
ArrayList<ProductBean> list= new ArrayList();
ArrayList<String> arrayList=mydatabase.getpid(userid);
System.out.println("--outside"+arrayList);
myadapter = new Cart_Adapter(Cart.this, list);
lv.setAdapter(myadapter);
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
list.addAll(mydatabase.getproduct_details(pid));
}
}
myadapter.notifyDataSetChanged().
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
You are setting adapter inside loop .Which is totally wrong . Put this code outside Loop .
And one more thing Its time to upgrade to RecyclerView . Its easy and better implementation of ViewHolder Pattern .
ArrayList<String> arrayList=mydatabase.getpid(userid);
ArrayList<String> dataList=new ArrayList();;
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
// Do your stuff add data to list here
dataList.add(stringToAdd);
}
}
myadapter = new Cart_Adapter(Cart.this, dataList);
lv.setAdapter(myadapter);
Do this way and if your dataset is not building properly then i thing you are using wrong dataset for your list . Or probably you need an ExpandableListView . Figure out the dataSet first currently which is 'ArrayList'.
Print your list size in getCount(), to be sure if list is being sent inside. Check your item's layout_height, it should be wrap_content.
Because you are setting adapter each items in loop.
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
}
so it will take last pid from the iteration and will fetch product details which you are setting in card adapter.
Wrong part is: you are calling setAdapter() with each iteration in the loop
Solution:
Collect the list of records in loop, you can keep adding product details in the array list and then set the final list outside loop in the adapter.
Check your cartxml layout file. The root RelativeLayout height will be wrap_content otherwise it will take all the available space for a single item. Use android:layout_width="wrap_content" for RelativeLayoutin in your cartxml
I have a problem how to hide the button in specific row in my listview with SimpleAdapter. This is my sample code.
alert_noti_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
alert_pop_list.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"
>
<TextView
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/confirm"
android:layout_toStartOf="#+id/confirm" />
<TextView
android:id="#+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="10dp"
android:textStyle="bold"
android:visibility="visible"
android:layout_below="#+id/view1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/confirm"
android:layout_toStartOf="#+id/confirm" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/confirm"
android:layout_alignBottom="#+id/view2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/ic_confirm" />
</RelativeLayout>
Activity :
List<Map<String, String>> listData = new ArrayList<Map<String, String>>();
Map<String, Object> names = new HashMap<String, Object>();
names.put("First","head1");
names.put("Second", "txt1");
names.put("First","head2");
names.put("Second", "txt2");
names.put("First","head3");
names.put("Second", "txt3");
...
listData.add((HashMap) names);
ListView lv = (ListView) dialog_mail.findViewById(R.id.listView1);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(home.this,, android.R.layout.two_line_list_item, names);
SimpleAdapter adapter = new SimpleAdapter(home.this, listData,
R.layout.alert_pop_list,
new String[] {"First", "Second" },
new int[] {R.id.view1, R.id.view2});
lv.setAdapter(adapter);
how do i hide the button if the value is head2 and show to the rest of the list.
You need to custom a BaseAdapter instead of SimpleAdapter and write some code at getView() method。 just like:
public View getView(int p,View view,ViewGroup parent) {
if (view == null) {
view = LayoutInflate.xxxx;
}
Button btn = (Button) view.findViewById(R.id.btn);
DataModel d = list.get(p);
if (d.isHidden || d.data.equals("head2")) {
btn.setVisibility(View.GONE);
} else {
btn.setVisibility(View.visible);
}
return view;
}
so you need to create a DataModel to store some property.
public class DataModel {
public boolean isHidden;
public HashMap<String,String> dataMap;
//and so on.....
}
//My xml code
<ScrollView
android:id="#+id/scrollview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="35dp">
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/background_light"
android:gravity="center"
android:paddingBottom="5dp" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Venue, Date"
android:textSize="12sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:text="Description"
android:textSize="12sp"
android:paddingBottom="5dp" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="14dp"
android:src="#drawable/demo" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Event of the Week" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView7"
android:layout_marginLeft="14dp"
android:text="Event Name" />
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView6"
android:layout_marginTop="14dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:paddingBottom="5dp"
android:verticalSpacing="10dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Today's Events" />
</RelativeLayout>
</ScrollView>
//MainActivity
class LoadProfile extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EventHome.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
String json = null;
PROFILE_URL = "http://www.example.com/filter_event_android.php?pin="+phone;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PROFILE_URL);
httppost.setEntity(new UrlEncodedFormEntity(params));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
json = EntityUtils.toString(resEntity);
Log.i("All Events: ", json.toString());
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
#SuppressLint("InlinedApi") #Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
pDialog.dismiss();
try{
event = new JSONObject(json);
final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
JSONArray user = event.getJSONArray("events");
String contains=json.toString();
if(contains.contains("id"))
{
for (int i = 0; i < user.length(); i++) {
JSONObject object = user.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", object.getString("id"));
map.put("name", object.getString("name"));
map.put("date_d", object.getString("date_d"));
map.put("location", object.getString("location"));
map.put("images", "http://www.example.com/"+object.getString("images"));
arraylist.add(map);
}
String[] from = {"name", "date_d", "location", "images"};
int[] to = {R.id.textView1, R.id.textView2, R.id.textView3, R.id.iv_flag};
ListAdapter adapters = new MyAdapter(EventHome.this,arraylist,R.layout.list_event_home,from,to);
gv1.setAdapter(adapters);
}
else
{
gv1.setVisibility(View.GONE);
TextView dynamicTextView = new TextView(EventHome.this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dynamicTextView.setText("No events available");
}
gv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(EventHome.this,EventSingle.class);
i.putExtra("event_id", arraylist.get(arg2).get("id"));
startActivity(i);
}
});
}catch(Exception e)
{
e.printStackTrace();
}
}
}
//MyAdapter.java
public class MyAdapter extends SimpleAdapter{
public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
}
public View getView(int position, View convertView, ViewGroup parent){
// here you let SimpleAdapter built the view normally.
View v = super.getView(position, convertView, parent);
// Then we get reference for Picasso
ImageView img = (ImageView) v.getTag();
if(img == null){
img = (ImageView) v.findViewById(R.id.iv_flag);
v.setTag(img); // <<< THIS LINE !!!!
}
// get the url from the data you passed to the `Map`
#SuppressWarnings("rawtypes")
String url = (String) ((Map)getItem(position)).get("images");
// do Picasso
Picasso.with(v.getContext()).load(url).into(img);
// return the view
return v;
}
}
The above layout shows only one row values in my gridview. But i have morethan 5 values for gridview. Why is it not showing the other values. I tried a lot but no use. Does anyone have solution.
This is probably caused by having GridView inside of ScrollView. Since both layouts are scrollable this causes a lot of problems, in your case the height of GridView cannot be properly determined and the scroll events are eaten by the ScrollView.
With ListView you can simply declare the ListView as root element and then add other scrollable content as headers or footers. GridView natively does not support this but fortunately there is subclass implementation of HeaderGridView which solves this problem.
What you should do is put only the HeaderGridView to your xml inflated by the activity.
<?xml version="1.0" encoding="utf-8"?>
<your.package.HeaderGridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp" />
and then implement the RelativeLayout as a header view in different xml (for example header.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/background_light"
android:gravity="center"
android:paddingBottom="5dp">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Venue, Date"
android:textSize="12sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:text="Description"
android:textSize="12sp"
android:paddingBottom="5dp" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="14dp"
android:src="#drawable/demo" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Event of the Week" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView7"
android:layout_marginLeft="14dp"
android:text="Event Name" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Today's Events" />
</RelativeLayout>
In your activity you add the header to your HeaderGridView
HeaderGridView gridView = (HeaderGridView) findViewById(R.id.gridView1);
View header = getLayoutInflater().inflate(R.layout.header, gridView, false);
gridView.addHeaderView(header);
// set adapter AFTER adding headerViews
gridView.setAdapter(MyAdapter(...))
Tried it, worked like a charm. Hope it helps!
The problem and fix I have found is that if the gridview has rows data bound the select statement must have that particular field in my case I am evaluating Grade and this grade was not included in my select statement and the gridview was returning single row no matter what but the moment I included the Grade in my select statement the problem was fixed
Protected Sub GVRos_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GVRos.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim stat As String
'This row evaluates and puts the value in stat as per the Finalised columon result
stat = DataBinder.Eval(e.Row.DataItem, "Grade").ToString()
If stat = "A" Then
e.Row.BackColor = System.Drawing.Color.LightGreen
ElseIf stat = "B" Then
e.Row.BackColor = System.Drawing.Color.LightBlue
ElseIf stat = "C" Then
e.Row.BackColor = System.Drawing.Color.Yellow
ElseIf stat = "D" Then
e.Row.BackColor = System.Drawing.Color.Orange
ElseIf stat = "F" Then
e.Row.BackColor = System.Drawing.Color.Red
Else
e.Row.BackColor = System.Drawing.Color.White
End If
End If
End Sub
I have two TextViews tvA and tvB which appear in a ListView lvA.
tvA and tvB display data pulled from a Sqlite Database using Cursor.
Cursor cursor = myDb.getAllRows();
String[] from = new String[]
{DBAdapter.KEY_A,DBAdapter.KEY_B};
int[] to = new int[]
{R.id.tvA,R.id.tvB};
SimpleCursorAdapter myCursorAdapter =new SimpleCursorAdapter(this,R.layout.lvA, cursor,from,to);
myList = (ListView) findViewById(R.id.lvA);
myList.setAdapter(myCursorAdapter);
registerForContextMenu(myList);
The issue is that tvA and tvB doesn't appear as one item in lvA (i.e. they don't appear on the same line) and appear as two different items.
Below is the 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="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<TextView
android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_toRightOf="#id/tvA"/>
</RelativeLayout>
If you want both the TextViews in the same align then you can use LinearLayout with android:layout_weight attribute.
To have equal width TextView, give same weight value.
You can use TableLayout
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<TextView android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
</TableRow></TableLayout >
or LinearLayout with android:orientation="horizontal"
and TextView with android:layout_weight="1"
Here's how I do it with three items on the same row = This is my item list filled from a cursor
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView_StudentName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_gravity="left"
android:text="#string/student_id"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:gravity="center"
android:text="#string/student_Location"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_alignParentRight="true"
android:text="#string/student_class" >
</TextView>
</RelativeLayout>`
I'm in the process of replacing the hard sizes with info from the dimen file, but so far this has worked.
TRY this :
Main Java:
//DECLARE:
private Map<String, Map<String, String>> mMenuView = new HashMap<String, Map<String, String>>();
//IN onCreateView
Adapter mAdapter = new NewAdapter(getActivity().getApplicationContext(), mMenuView);
mListView.setAdapter(mAdapter);
// ADD onActivityCreated or onStart save data from your DB and store in mMenuView
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("TEXTVALE_A", datafromdb_A);
tempMap.put("TEXTVALE_B", datafromdb_B);
mMenuView.put(Integer.toString(mMenuView.size()), tempMap);
}
//NEWADAPTER
private class NewAdapter extends BaseAdapter {
private Context mContext;
private TextView mTitle1;
private TextView mTitle2;
private Map<String, Map<String, String>> mData = new HashMap<String, Map<String, String>>();
public NewAdapter(Context applicationContext,Map<String, Map<String, String>> mMenuValue) {
mContext = applicationContext;
mData = mMenuValue;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Map<String, String> getItem(int position) {
return mData.get(Integer.toString(position));
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Map<String, String> data = mData.get(Integer.toString(position));
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.linear_layout, null);
mTitle1 = (TextView) convertView.findViewById(R.id.t1A);
mTitle2 = (TextView) convertView.findViewById(R.id.t1B);
}
if (data != null) {
mTitle1.setText(data.get("TEXTVALE_A"));
mTitle2.setText(data.get("TEXTVALE_B"));
}
return convertView;
}
}
linear_layout.xml
<LinearLayout
android:id="#+id/content_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/tab1_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center|center_horizontal"
>
<TextView
android:id="#+id/t1A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
android:textColor="#color/text_color3x"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="0.5"
android:orientation="vertical"
android:gravity="left|center_vertical|center_horizontal"
>
<TextView
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
/>
</LinearLayout>
</LinearLayout>
I have created listview and i want to implement sectionindexer and alphabetIndexer in my listview.
I am reffering http://www.anddev.org/tutalphabetic_fastscroll_listview_-_similar_to_contacts-t10123.html. That works excellent. But for my code , i am not getting how to implement that functionality.
here is my list_item.xml code :
<?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:orientation="vertical"
android:gravity="center"
android:background="#drawable/list_selector"
android:padding="5dp"
android:id="#+id/tab">
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/musicsymbol"/>
</LinearLayout>
<TextView
android:id="#+id/songTitle"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:layout_alignTop="#+id/thumbnail"
android:color="#10bcc9"
android:maxLines="1"
android:layout_toRightOf="#+id/thumbnail"
/>
<TextView
android:id="#+id/duration"
android:layout_width="56dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/songTitle"
android:gravity="right"
android:layout_marginRight="5dip"
android:textSize="15dip"
android:textColor="#10bcc9"
android:textStyle="bold"
android:maxLines="1"
/>
<TextView
android:id="#+id/songArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/songTitle"
android:textSize="14dp"
android:color="#10bcc9"
android:textStyle="bold"
android:lines="1"
android:layout_toRightOf="#+id/thumbnail"
/>
My JAVA code :
Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null , null, null,MediaStore.Audio.Media.TITLE );
if (cursor == null)
{
//Query Failed , Handle error.
}
else if (!cursor.moveToFirst())
{
//No media on the device.
}
else
{
int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);
int artistcolumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int durationcolumn =cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
for(int i=0;i<cursor.getCount();i++)
{
String thisTitle = cursor.getString(titleColumn);
String path = cursor.getString(idColumn);
String artist = cursor.getString(artistcolumn);
Long duration = cursor.getLong(durationcolumn);
Utilities objUtilities = new Utilities();
String timeDuration = objUtilities.milliSecondsToTimer(duration);
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle",thisTitle);
song.put("songPath", path);
song.put("artist", artist);
song.put("duration",timeDuration);
// Adding each song to SongList
songsList.add(song);
cursor.moveToNext();
}
}
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
String[] from = {"songTitle", "artist" , "duration"};
int[] to={R.id.songTitle,R.id.songArtist, R.id.duration};
adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, from, to);
setListAdapter(adapter);
so how to implement sectionindexer with my this listadapter. Please suggest me some solution. Thanx.
You can use this library. This is the simplest Section Adapter available for Android's ListView.