I have searched for this issue a few days. Here's the structure of the app.
activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<fragment android:id="#+id/fragment_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="ca.bluecross.ab.view.fragments.DrawerFragment"
tools:layout="#layout/abc_drawer_layout" />
</android.support.v4.widget.DrawerLayout>
Here's the layout of the fragment (some elements are not included to save space for post):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:id="#+id/scrollView"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
..
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="5dp"
android:background="#21c1c1c1">
..
</RelativeLayout>
..
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/expLVClaims"
/>
.
..
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TableRow
..
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
..
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
..
</TableRow>
</TableLayout>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="I have more to add"
android:id="#+id/btnAddMoreClaim"
android:layout_gravity="center_horizontal"
android:layout_span="2"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:background="#color/abc_blue"
android:textColor="#FFFFFF" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="I'm ready to submit"
android:id="#+id/btnReadyToSubmitClaim"
android:layout_span="2"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/btnAddMoreClaim"
android:background="#color/abc_blue"
android:textColor="#FFF" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/btnCancelEnterClaim"
android:layout_alignWithParentIfMissing="false"
android:layout_centerInParent="true"
android:background="#color/abc_blue"
android:textColor="#FFF" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Here's the layout for group header:
<?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="wrap_content">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgIndicator"
android:src="#drawable/icon_collapse"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Provider name"
android:id="#+id/txtProviderName"
android:layout_toRightOf="#+id/imgIndicator" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgDelete"
android:src="#drawable/icon_cross_delete"
android:layout_toLeftOf="#+id/imgEdit" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgEdit"
android:layout_alignParentEnd="true"
android:src="#drawable/icon_edit" />
</RelativeLayout>
Here's the child layout:
<?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"
android:background="#59868686">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutServiceDate">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of service"
android:id="#+id/txtClaimDetailServiceDateLabel"
android:layout_alignParentStart="true"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feb 10, 2015"
android:id="#+id/txtClaimDetailServiceDate"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutProvider">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Provider"
android:id="#+id/txtClaimDetailProviderLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="provider's name"
android:id="#+id/txtClaimDetailProviderName"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayoutProduct">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product or Service"
android:id="#+id/txtProductLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product/service"
android:id="#+id/txtProduct"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutClaimAmount">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Claim amount($)"
android:id="#+id/txtClaimDetailClaimAmountLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:id="#+id/txtClaimDetailClaimAmount"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutOtherAmount">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other amount($)"
android:id="#+id/txtClaimDetailOtherAmountLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:id="#+id/txtClaimDetailOtherAmount"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
Here's the adapter for the ExpandableListView:
public class EclaimsExpandableListAdapter extends BaseExpandableListAdapter {
private ArrayList<ClaimGroup> groups;
public LayoutInflater inflater;
public Activity activity;
float mDensity;
private static final int MAX_ITEMS_MEASURED = 15;
public EclaimsExpandableListAdapter(Activity act, ArrayList<ClaimGroup> groups){
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
mDensity = activity.getResources().getDisplayMetrics().density;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).getChildren().get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
//return 0;
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Log.d("EclaimsExpandableListAdapter :: getChildView() :: ", "Start");
ClaimDetail child = (ClaimDetail)getChild(groupPosition, childPosition);
if(convertView == null){
convertView = inflater.inflate(R.layout.eclaims_claim_detail_health_layout,null);
}
TextView txtServiceDate = (TextView)convertView.findViewById(R.id.txtClaimDetailServiceDate);
txtServiceDate.setText(child.getmServiceDate());
TextView txtProviderName = (TextView)convertView.findViewById(R.id.txtClaimDetailProviderName);
txtProviderName.setText(child.getProvider().getName());
TextView txtClaimAmount = (TextView)convertView.findViewById(R.id.txtClaimDetailClaimAmount);
txtClaimAmount.setText(child.getClaimAmount().toString());
TextView txtOtherAmout = (TextView)convertView.findViewById(R.id.txtClaimDetailOtherAmount);
if(child.getOtherAmount() == null){
txtOtherAmout.setVisibility(View.GONE);
}else{
txtOtherAmout.setText(child.getOtherAmount().toString());
}
return convertView;
}
#Override
public int getChildrenCount(int grpPostion) {
System.out.println("Group position :: " + grpPostion);
return groups.get(grpPostion).getChildren().size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Log.d("EclaimsExpandableListAdapter :: getGroupView() :: ", "Start");
if (convertView == null) {
convertView = inflater.inflate(R.layout.eclaims_claim_group_row, null);
}
/*TextView claimsLabel = (TextView)activity.findViewById(R.id.txtClaimItemsLabel);
if(groups.isEmpty()){
claimsLabel.setVisibility(View.GONE);
}else{
claimsLabel.setVisibility(View.VISIBLE);
}*/
ClaimGroup group = (ClaimGroup) getGroup(groupPosition);
ImageView indicator = (ImageView)convertView.findViewById(R.id.imgIndicator);
int nH = 0;
if(isExpanded){
indicator.setImageResource(R.drawable.icon_expand);
//nH = measureChildrenHeight(groupPosition);
}else{
indicator.setImageResource(R.drawable.icon_collapse);
//nH = convertView.getMeasuredHeight();
}
//parent.getLayoutParams().height = (int) (nH * mDensity);
//parent.invalidate();
TextView providerName = (TextView)convertView.findViewById(R.id.txtProviderName);
providerName.setText(group.getProvider().getName());
ImageView imgDelete = (ImageView)convertView.findViewById(R.id.imgDelete);
imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
String confirmation = activity.getString(R.string.eclaims_delete_claim_confirmation);
builder.setMessage(confirmation);
builder.setPositiveButton(R.string.alert_Cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
builder.setNegativeButton(R.string.alert_OK, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
groups.remove(groupPosition);
notifyDataSetChanged();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
ImageView imgEdit = (ImageView)convertView.findViewById(R.id.imgEdit);
imgEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
String confirmation = activity.getString(R.string.eclaims_edit_claim_confirmation);
builder.setMessage(confirmation);
builder.setPositiveButton(R.string.alert_Cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
builder.setNegativeButton(R.string.alert_OK, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// first: remove from the completed claims
Provider selectedProvider = ((ClaimGroup)getGroup(groupPosition)).getProvider();
ClaimGroup parent = groups.remove(groupPosition);
List<Product> prodList = parent.getProductList(); // retrieved the saved product list
ClaimDetail child = parent.getChildren().get(0); // there is only one child
Product selectedProduct = child.getProduct();
notifyDataSetChanged();
// second: populate the fields in claim detail block
TextView serviceDate = (TextView)activity.findViewById(R.id.txtServiceDate);
serviceDate.setText(child.getmServiceDate());
Spinner spinnerProvider = (Spinner)activity.findViewById(R.id.spinnerProviders);
ArrayAdapter<Provider> adapterProvider = (ArrayAdapter< Provider >)spinnerProvider.getAdapter();
int size = adapterProvider.getCount();
int pos_seletion = 0;
for(int i=0; i<size; i++){
Provider prvd = adapterProvider.getItem(i);
if(selectedProvider.equals(prvd)){
pos_seletion = i;
break;
}
}
System.out.println("Selected position for provider list :: " + pos_seletion);
spinnerProvider.setSelection(pos_seletion);
adapterProvider.notifyDataSetChanged();// do we need to notify?
Spinner spinnerProduct = (Spinner)activity.findViewById(R.id.spinnerProducts);
ArrayAdapter<Product> adapterProduct = (ArrayAdapter < Product >)spinnerProduct.getAdapter();
size = prodList.size();
pos_seletion = 0;
for(int j=0; j<size; j++){
Product prod = prodList.get(j);
if(selectedProduct.equals(prod)){
pos_seletion = j;
break;
}
}
System.out.println("Selected position for product list :: " + pos_seletion);
adapterProduct.clear();
adapterProduct.addAll(prodList);
spinnerProduct.setSelection(pos_seletion);
adapterProduct.notifyDataSetChanged(); // do we need to notify?
EditText claimAmt = (EditText)activity.findViewById(R.id.editClaimAmount);
claimAmt.setText(child.getClaimAmount().toString());
EditText otherAmt = (EditText)activity.findViewById(R.id.editOtherAmount);
if(child.getOtherAmount() != null){
otherAmt.setText(child.getOtherAmount().toString());
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
}
Here's the behavior: when clicking the group header, no child view is displayed. From LogCat, I can see that getViewGroup() in the adapter is called twice though, but getChildView() is not called at all. After searching the internet and trying them out, I still can't get the child view displayed when clicking on the group header, but when I tried setting the ExpanandableListView height with a fix height, say, 400dp, like this:
<ExpandableListView xmlns:
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#+id/expLVClaims"/>
the child view is then expanded and collapsed as expected. But the thing is that because the fixed height will makes the ExpandableListView occupy 400dp space vertically, even no data is in the adapter at all, which is unwanted behavior.
Can someone throw some hints?
Thanks in advance!
Shawn
First thing:
You should not use any scrolling component like expandable list, inside a scroll view here's why Listview inside ScrollView is not scrolling on Android.
Second thing:
Height of ExpandableListView should be match_parent.
I was also facing the same problem related to height, and making ExpandableListView height as match_parent resolved my problem. I think this will help you.
Related
I'm implementing a ListView and on quick scrolling in large lists this glitch occurs. I am using ViewHolder Pattern
[![UI Glitch in ListView][1]][1]
[1]:
getView :
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(mResource, parent, false);
viewHolder = new ViewHolder();
viewHolder.titleView = (TextView) convertView.findViewById(R.id.sheet_name_text_view);
viewHolder.rowsCount = (TextView) convertView.findViewById(R.id.rows_count_text_view);
viewHolder.selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final ImportedTable item = getItem(position);
if (item != null) {
viewHolder.titleView.setText(item.getTableName());
int columnsCount = 0;
for (boolean isSelected : item.getSelectedColumns()) {
if (isSelected) {
columnsCount++;
}
}
viewHolder.rowsCount.setText(context.getString(R.string.label_rowscounttext, (item.getRecordsCount() - item.getHeaderRow()), columnsCount));
}
final CheckBox isSelectedForImport = (CheckBox) convertView.findViewById(R.id.import_table_list_toggle_button);
RelativeLayout checkboxLayout = (RelativeLayout) convertView.findViewById(R.id.import_table_list_toggle_layout);
checkboxLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isSelectedForImport.performClick();
}
});
final TextView selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
isSelectedForImport.setChecked(item.isSelected());
if (!isSelectedForImport.isChecked()) {
convertView.setClickable(true);
viewHolder.titleView.setTextColor(Color.parseColor("#8A000000"));
} else {
convertView.setClickable(false); viewHolder.titleView.setTextColor(Color.parseColor("#DE000000"));
}
isSelectedForImport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId()) && !isSelectedForImport.isChecked()) {
isSelectedForImport.setChecked(true);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog dialog;
builder.setMessage(context.getString(R.string.importapplication_appscreen_label_lookupwarningmessage));
builder.setPositiveButton(context.getString(R.string.ui_label_continue), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < item.getColumnType().size(); i++) {
removeLookups(item.getSheetId(), item.getTableId(), i);
}
isSelectedForImport.setChecked(false);
item.setSelected(false);
dialog.dismiss();
}
});
builder.setNegativeButton(context.getString(R.string.ui_label_dontcontinue), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.show();
notifyDataSetChanged();
} else if (!isSelectedForImport.isChecked()) {
item.setSelected(isSelectedForImport.isChecked());
notifyDataSetChanged();
} else {
if (isSelectedForImport.isChecked()) {
for (int i = 0; i < item.getColumnType().size(); i++) {
if (item.getSelectedColumns().get(i) && item.getColumnType().get(i).equals("SINGLE_LOOKUP")) {
enableParents(item.getSheetId(), item.getTableId(), i);
}
}
notifyDataSetChanged();
}
item.setSelected(isSelectedForImport.isChecked());
}
mCallback.onTableSelectionToggle();
}
});
if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId())) {
Set<String> children = ((ImportActivity) context).getNewApplication().getChildTableNames(item.getSheetId(), item.getTableId());
Iterator<String> childIterator = children.iterator();
String childrenString = childIterator.next();
while (childIterator.hasNext()) {
childrenString += ", ";
childrenString += childIterator.next();
}
selectMessage.setText(context.getString(R.string.label_importlookupchildrentext) +" "+ childrenString);
selectMessage.setVisibility(View.VISIBLE);
} else {
selectMessage.setVisibility(View.GONE);
}
return convertView;
}
This does not occur all the time, and it doesn't occur for small lists. I have no other common problems described in other posts here (as long as this glitch doesn't occur, all views work correctly, and are in proper order).
XML Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/sheet_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#DE000000"
android:textSize="16sp"
tools:text="Sheet Name" />
<TextView
android:id="#+id/rows_count_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8A000000"
android:textSize="14sp"
tools:text="Rows : 3" />
<TextView
android:id="#+id/row_info_text"
android:textSize="12sp"
android:layout_width="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:visibility="gone"
tools:visibility="gone"
tools:text="This form is looked up by "/>
</LinearLayout>
<RelativeLayout
android:id="#+id/import_table_list_toggle_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="19dp"
android:paddingRight="19dp"
android:layout_alignParentRight="true">
<CheckBox
android:id="#+id/import_table_list_toggle_button"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingBottom="24dp"
android:paddingTop="24dp" />
</RelativeLayout>
As I mention problem is RelativeLayout layout.
try this...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/sheet_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#DE000000"
android:textSize="16sp"
tools:text="Sheet Name"/>
<TextView
android:id="#+id/rows_count_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8A000000"
android:textSize="14sp"
tools:text="Rows : 3"/>
<TextView
android:id="#+id/row_info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:ellipsize="end"
android:maxLines="1"
android:textSize="12sp"
android:visibility="gone"
tools:text="This form is looked up by "
tools:visibility="gone"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/import_table_list_toggle_layout"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="19dp"
android:paddingRight="19dp">
<CheckBox
android:id="#+id/import_table_list_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:paddingBottom="24dp"
android:paddingTop="24dp"/>
</RelativeLayout>
I am new here so please bear with my naivety. I am working on a music player that will display a menu on clicking 'dots' buttton(given in code below) in listview item. Inside the menu there will be a button to delete. The listview is getting populated by a custom adapter. Here is the code of adapter...
private class hashmapAdapter extends BaseAdapter implements Filterable {
HashMap<Integer, NowPlayingActivity.Details> NewDetails = new HashMap<>();
private hashmapAdapter(HashMap<Integer, NowPlayingActivity.Details> hashMap) {
if(NewDetails.size()==0) {
NewDetails.putAll(hashMap);
}
}
#Override
public int getCount() {
return NewDetails.size();
}
#Override
public Object getItem(int position) {
return NewDetails.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View result;
if(convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item,parent,false);
} else {
result = convertView;
}
((TextView)result.findViewById(R.id.Name)).setText(NewDetails.get(position).getName());
((TextView)result.findViewById(R.id.artist)).setText(NewDetails.get(position).getArtist());
((TextView)result.findViewById(R.id.duration)).setText(NewDetails.get(position).getDuration()+" || ");
dots = (Button)result.findViewById(R.id.dots);
dots.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = (TextView)result.findViewById(R.id.Name);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View popup_view = inflater.inflate(R.layout.option_layout,null);
RelativeLayout item = (RelativeLayout) popup_view.findViewById(R.id.popup);
final float width= (getResources().getDimension(R.dimen.width_entry_in_dp));
final float height= getResources().getDimension(R.dimen.height_entry_in_dp);
final PopupWindow popupWindow = new PopupWindow(popup_view,Math.round(width),Math.round(height),true);
popupWindow.showAtLocation(item, Gravity.END, 0, 0);
popupWindow.showAsDropDown(dots);
play_next_btn = (Button)popup_view.findViewById(R.id.play_next);
add_to_playlist = (Button)popup_view.findViewById(R.id.add_to_playlist);
share = (Button)popup_view.findViewById(R.id.share);
delete = (Button)popup_view.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<NewDetails.size();i++) {
if(NewDetails.get(i).getName().equals(name.getText().toString())) {
for(int j=0;j<hashMap.size();j++) {
if(hashMap.get(j).getName().equals(name.getText().toString())) {
if (true) {
NewDetails.remove(i);
hashMap.remove(j);
}
}
}
}
}
populate1(NewDetails);
popupWindow.dismiss();
}
});
}
});
return result;}
Now the problem is that after deleting element when i call populate1(NewDetails), it gives a null pointer exception on NewDetails.get(position).getName(). So on repopulating listview i am getting NPE error. Following is the populate1() function code...
public void populate1(HashMap<Integer,NowPlayingActivity.Details> hashMap) {
adapter = new hashmapAdapter(hashMap);
library.setAdapter(adapter);
adapter.notifyDataSetChanged();
library.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = (TextView)view.findViewById(R.id.Name);
String SongName = name.getText().toString();
Intent intent = new Intent(getApplicationContext(),NowPlayingActivity.class);
intent.putExtra("SongName",SongName);
setResult(555,intent);
finish();
}
});
}
I have already tried notifydatasetchanged() and listview.invalidateviews(), both didnt work. File is getting deleted but the adapter is not refreshing the list to remove element. Here is the xml layout of single_item for listview....`
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="70dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:background="#drawable/element_shape_list"
android:descendantFocusability="blocksDescendants">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v11"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/art_work"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#mipmap/ic_launcher"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"/>
<TextView
android:id="#+id/Name"
android:textSize="17sp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_toRightOf="#id/art_work"
android:layout_toEndOf="#+id/art_work"
android:singleLine="true"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#+id/art_work"
android:layout_toEndOf="#id/art_work"
android:layout_below="#id/Name">
<TextView
android:id="#+id/duration"
android:textSize="12sp"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_height="20dp"
android:singleLine="true"
android:gravity="clip_horizontal"
android:ellipsize="end"/>
<TextView
android:id="#+id/artist"
android:textSize="12sp"
android:textColor="#000"
android:layout_toRightOf="#+id/duration"
android:layout_toEndOf="#+id/duration"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:gravity="fill_horizontal"
android:layout_height="20dp"
android:singleLine="true" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent">
<Button
android:id="#+id/dots"
android:layout_width="40dp"
android:textStyle="bold"
android:background="?android:attr/selectableItemBackground"
android:layout_height="70dp"
android:text="#string/vertical_ellipsis"
android:textSize="25sp" />
</FrameLayout>
</RelativeLayout>
I am new to android,I am currently making a listview with custom list items,I want to do an action on Listview's item click event,I have searched so many similar threads with no luck,Can anyone help me to fix this.my code is as below,
<?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="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<TextView
android:id="#+id/tv_hdr"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="#color/orange"
android:gravity="center"
android:text="SELECT YOUR CITY"
android:textColor="#color/white"
android:textSize="22dp" />
<EditText
android:id="#+id/et_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_hdr"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/et_selector"
android:drawableLeft="#drawable/search"
android:hint="City Or Postal Code"
android:padding="10dp"
android:textSize="16dp" />
<TextView
android:id="#+id/tv_loc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/et_search"
android:background="#color/grey_light"
android:drawableLeft="#drawable/ic_loc"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text=" My Location"
android:textColor="#color/black"
android:textSize="16dp"
android:textStyle="bold" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scr_location"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tv_loc"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_populcar_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text=" Popular Cities"
android:textColor="#color/orange"
android:textSize="16dp"
android:textStyle="bold" />
<ListView
android:id="#+id/lv_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_populcar_city" />
<TextView
android:id="#+id/tv_states"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lv_city"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text="States"
android:textColor="#color/orange"
android:textSize="16dp"
android:textStyle="bold" />
<ListView
android:id="#+id/lv_state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_states" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
adapter
public class CityAdapter extends BaseAdapter {
private Context mContext;
private final ArrayList<City> city;
Integer selected_position = -1;
public CityAdapter(Context c, ArrayList<City> city) {
mContext = c;
this.city = city;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return city.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.raw_city, parent, false);
} else {
v = (View) convertView;
}
TextView tv_city = (TextView) v.findViewById(R.id.tv_city);
tv_city.setText(city.get(position).getCity());
return v;
}
}
activity
lv_city.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Pref.setValue( SelectCity.this, Const.PREF_CITY_ID, cityList.get( position ).getCity_id() );
finish();//finishing activity
}
} );
You are wrong class in listview item click
lv_city.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Pref.setValue( SelectCity.this, Const.PREF_CITY_ID,
cityList.get( position ).getCity_id() );
finish();//finishing activity
}
});
#quick learner's was correct, But i put some idea for your ref..
If you use custom adapter class, In my suggestion use setOnClickListener in custom adapter class this is better way to do this type of case!!
For example,
mViewHolder.MyUI.setOnClickListener
My ExpandableListView behaves normally on my Galaxy Note 3 as pictured below:
But on my Nexus 7 and Galaxy Note 10.1, there's a bunch of extra space that appears when I expand the list to show a child. (The space does not appear until I click a parent.)
From Nexus 7:
My first guess is that there is something inconsistent with my layout resources (I had different layout resources for sw-600 and sw-700) but removing the larger screen resources, the behaviour is the same. I also commented out everything in the getGroupView and getChildView methods in my adapter class other than inflating the layouts, and still this behaviour. So now I have no idea what is causing this behaviour. I guess the other significant detail is that I'm doing this in an AlertDialog, but it doesn't explain why it only seems to happen on larger devices.
Initializing the ExpandableListAdapter and AlertDialog:
private void showServicesDialog(final ArrayList<Service> services) {
View dView = activity.getLayoutInflater().inflate(R.layout.expandable_list_view, null);
View header = activity.getLayoutInflater().inflate(R.layout.service_list_header, null);
AlertDialog servicesDialog = DialogHelper.createLayoutAlertDialog(activity, null, dView,
R.string.cancel, R.string.add_services,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (selectedServices.size() > 0) {
addServicesToBackend();
}
}
});
ExpandableListView list = (ExpandableListView) dView.findViewById(R.id.expandableListView);
ServicesExpandableListAdapter adapter = new ServicesExpandableListAdapter(activity, services);
list.setGroupIndicator(null);
list.setSelector(R.drawable.list_selector_bg);
list.setDivider(null);
list.addHeaderView(header);
list.setAdapter(adapter);
servicesDialog.show();
}
From my Adapter class:
public ServicesExpandableListAdapter(Activity act, ArrayList<Service> s) {
activity = (MyActivity) act;
services = s;
itemChecked = new ArrayList<Boolean>();
for (int i = 0; i < services.size(); i++)
itemChecked.add(false);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return services.get(groupPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return services.get(groupPosition);
}
#Override
public int getGroupCount() {
return services.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private static class ParentViewHolder {
TextView nameTV, categoryTV, expandTV, typeTV, amountTV, warningTV;
CheckBox checkbox;
}
#Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ParentViewHolder holder = null;
final Service service = services.get(groupPosition);
if (convertView == null) {
convertView = activity.getLayoutInflater().inflate(R.layout.service_list_parent_item, null);
holder = new ParentViewHolder();
holder.nameTV = (TextView) convertView.findViewById(R.id.nameTV);
holder.typeTV = (TextView) convertView.findViewById(R.id.typeTV);
holder.categoryTV = (TextView) convertView.findViewById(R.id.categoryTV);
holder.expandTV = (TextView) convertView.findViewById(R.id.expandTV);
holder.amountTV = (TextView) convertView.findViewById(R.id.amountTV);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.selectCB);
holder.warningTV = (TextView) convertView.findViewById(R.id.warningTV);
convertView.setTag(holder);
} else {
holder = (ParentViewHolder) convertView.getTag();
}
//content commented out
return convertView;
}
private static class ChildViewHolder {
TextView typeTV, priceTV, infoTV;
CheckBox weekdaysCB, saturdaysCB, sundaysCB, holidaysCB;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder holder = null;
if (convertView == null) {
convertView = activity.getLayoutInflater().inflate(R.layout.service_list_child_item, null);
holder = new ChildViewHolder();
holder.typeTV = (TextView) convertView.findViewById(R.id.typeTV);
holder.priceTV = (TextView) convertView.findViewById(R.id.priceTV);
holder.infoTV = (TextView) convertView.findViewById(R.id.infoTV);
holder.weekdaysCB = (CheckBox) convertView.findViewById(R.id.weekdaysCB);
holder.saturdaysCB = (CheckBox) convertView.findViewById(R.id.saturdaysCB);
holder.sundaysCB = (CheckBox) convertView.findViewById(R.id.sundaysCB);
holder.holidaysCB = (CheckBox) convertView.findViewById(R.id.holidaysCB);
convertView.setTag(holder);
} else {
holder = (ChildViewHolder) convertView.getTag();
}
//content commented out
return convertView;
}
service_list_parent_item.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="wrap_content" >
<View
android:id="#+id/topDivider"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentTop="true"
android:background="#color/gray_divider" />
<TextView
android:id="#+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="#+id/warningTV"
android:text="Service name"
android:textAppearance="#style/regularText" />
<TextView
android:id="#+id/typeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nameTV"
android:layout_below="#+id/nameTV"
android:layout_marginBottom="3dp"
android:layout_marginRight="5dp"
android:background="#color/holo_light_purple"
android:drawableLeft="#drawable/ic_clock_tiny_white" />
<TextView
android:id="#+id/categoryTV"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/typeTV"
android:layout_marginBottom="3dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/expandTV"
android:layout_toRightOf="#+id/typeTV"
android:ellipsize="end"
android:lines="1"
android:text="Category Name that is super long" />
<TextView
android:id="#+id/warningTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/selectCB"
android:layout_marginRight="5dp"
android:background="#drawable/action_bar_selector"
android:drawableRight="#drawable/ic_warning_gray"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="5dp"
android:visibility="invisible" />
<CheckBox
android:id="#+id/selectCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="5dp" />
<TextView
android:id="#+id/amountTV"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/blue_button_selector"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:text="0.0"
android:textColor="#color/white"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/expandTV"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/bottomDivider"
android:drawableRight="#drawable/ic_expand_arrow_gray"
android:gravity="bottom"
android:paddingBottom="5dp"
android:text="#string/expand"
android:textColor="#color/lighter_gray_text" />
<View
android:id="#+id/bottomDivider"
android:layout_width="90dp"
android:layout_height="3dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="60dp"
android:background="#color/blue" />
</RelativeLayout>
service_list_child_item.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="wrap_content"
android:background="#color/gray_tasklist" >
<View
android:id="#+id/topDivider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_alignParentTop="true"
android:background="#color/blue" />
<View
android:id="#+id/centerView"
android:layout_width="1px"
android:layout_height="1px"
android:layout_below="#+id/topDivider"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/typeTV"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/topDivider"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/centerView"
android:drawableLeft="#drawable/ic_clock_tiny_purple"
android:drawablePadding="4dp"
android:gravity="center_vertical"
android:text="#string/time_service"
android:textColor="#color/holo_light_purple" />
<TextView
android:id="#+id/priceTV"
style="#style/regularTextSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/typeTV"
android:layout_marginLeft="35dp"
android:layout_toRightOf="#+id/centerView"
android:text="35e/hr"
android:textColor="#color/blue" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/typeTV"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" >
<CheckBox
android:id="#+id/weekdaysCB"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:enabled="false"
android:text="#string/weekdays" />
<CheckBox
android:id="#+id/holidaysCB"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/weekdaysCB"
android:layout_below="#+id/weekdaysCB"
android:enabled="false"
android:text="#string/holidays" />
<CheckBox
android:id="#+id/saturdaysCB"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/weekdaysCB"
android:layout_toRightOf="#+id/weekdaysCB"
android:enabled="false"
android:text="#string/saturdays" />
<CheckBox
android:id="#+id/sundaysCB"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/holidaysCB"
android:layout_alignLeft="#+id/saturdaysCB"
android:enabled="false"
android:text="#string/sundays" />
</RelativeLayout>
<TextView
android:id="#+id/infoTV"
style="#style/regularTextVerySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:padding="5dp" />
</RelativeLayout>
And lastly, expandable_list_view.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="wrap_content"
android:background="#color/gray_tasklist"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/expandableListView"
style="#style/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/gray_divider"
android:dividerHeight="1px"
android:childDivider="#00000000"/>
</LinearLayout>
I'm a newbie android.I have some problem about my mini app.
You can see figure below:
http://i481.photobucket.com/albums/rr175/viethungit/android.png
Firstly,row 1(layout 1) appear listview when click button add in row 1, row 2(layout 2) appear, and continue...click button add in row 1 ,layout 2 appear in listview....
I try search from Mr.Google but i don't find...
Anybody could help me!
This is layout, and activity I don't know how to implement
add_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:orientation="horizontal"
android:padding="5dip">
<!-- Image Item-->
<ImageButton
android:id="#+id/imgItem"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:contentDescription="#string/imgView" />
<!-- Name item -->
<EditText
android:id="#+id/edtItem"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/imgItem"
android:background="#drawable/bg"
android:hint="#string/txtTitle"
android:textSize="20dip" />
<!-- Button add -->
<Button
android:id="#+id/btnAdd"
android:background="#drawable/add"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
<!-- item -->
<TextView
android:id="#+id/txtSubtitle"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_toLeftOf="#+id/btnAdd"
android:gravity="right|center_vertical"
android:layout_centerVertical="true"
android:textSize="20dip"
android:text="#string/txtSubtitle" />
</RelativeLayout>
plus_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:orientation="horizontal"
android:padding="5dip" >
<!-- Image Item -->
<ImageView
android:id="#+id/imgItem"
android:src="#drawable/chomsao"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:contentDescription="#string/imgView" />
<!-- Name Item -->
<TextView
android:id="#+id/edtItem"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/imgItem"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="#string/txtTitle"
android:textSize="20dip" />
<!-- Quantity Item -->
<TextView
android:id="#+id/txtQtyItem"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="#id/edtItem"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:textSize="20dip"
android:text="#string/QuantityItem"/>
<!-- Button plus -->
<Button
android:id="#+id/btnPlus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/plus" />
<!-- Button minus -->
<Button
android:id="#+id/btnMinus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignBaseline="#id/btnPlus"
android:layout_toLeftOf="#+id/btnPlus"
android:background="#drawable/minus" />
<!-- Price Item -->
<TextView
android:id="#+id/txtSubtitle"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btnMinus"
android:gravity="right|center_vertical"
android:text="#string/txtSubtitle"
android:textSize="20dip" />
</RelativeLayout>
activity_main.xml
<RelativeLayout
android:id="#+id/relative1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/relative"
android:layout_above="#id/relative2"
android:background="#EEEEEE"
android:layout_marginLeft="1.5dp"
android:layout_marginRight="1.5dp"
android:layout_marginTop="1.5dp">
<ListView
android:id="#+id/listSale"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
public class AndroidCustomListViewActivity extends Activity {
private ListView myList;
private MyAdapter myAdapter;
private ImageView myImage;
public static String upload= " ";
public static String GalleryImage;
public ArrayList<ListItem> myItems = new ArrayList<ListItem>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listandimage);
myList = (ListView) findViewById(R.id.MyList);
myImage= (ImageView)findViewById(R.id.image1);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
ListItem listItem = new ListItem();
listItem.textdata="#";
listItem.caption = "";
myItems.add(listItem);
myList.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return myItems.size();
}
public ListItem getItem(int position) {
return myItems.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.text=(TextView )convertView.findViewById(R.id.textView1);
holder.captionEditText = (EditText) convertView.findViewById(R.id.ItemCaption);
holder.addOrDeleteButton = (Button) convertView.findViewById(R.id.buttonAdd);
holder.captionEditText.setFocusable(true);
holder.captionEditText.requestFocus();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Fill EditText with the value you have in data source
// holder.captionEditText.setId(position);
holder.text.setTag(position);
holder.captionEditText.setTag(position);
holder.captionEditText.setText(getItem(position).caption);
holder.addOrDeleteButton.setTag(position);
// / this updates tag of
// the button view as we
// scroll ///
holder.addOrDeleteButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
int tag = (Integer) view.getTag();
if (tag != (myItems.size() - 1)) {
myItems.remove(tag);
Log.d("GCM", "Item removed from " + tag);
myAdapter.notifyDataSetChanged();
} else {
ListItem listItem = new ListItem();
listItem.textdata="#";
listItem.caption = "";
myItems.add(listItem);
/*
* Log.d("GCM", holder.captionEditText.getText()
* .toString()); myItems.get((Integer)
* view.getTag()).caption = holder.captionEditText
* .getText().toString();
*/
myAdapter.notifyDataSetChanged();
myList.setSelection(myAdapter.getCount() - 1);
// holder.captionEditText.setFocusable(true);
// holder.captionEditText.requestFocus();
}
}
});
holder.captionEditText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// if(position < myItems.size())
// getItem(position).caption = s.toString();
myItems.get((Integer) holder.captionEditText.getTag()).caption = holder.captionEditText
.getText().toString();
}
});
if (position != (myItems.size() - 1)) {
holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttarecloseicon);
} else {
holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttareaddicon);
holder.text.setFocusable(true);
holder.captionEditText.setFocusable(true);
holder.text.requestFocus();
holder.captionEditText.requestFocus();
}
return convertView;
}
}
class ViewHolder {
TextView text;
EditText captionEditText;
Button addOrDeleteButton;
}
class ListItem {
String textdata;
String caption;
}
}
use this code edit it according to your need.
<?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"
>
<TextView
android:id="#+id/textView1"
android:layout_width="15dp"
android:layout_height="50dp"
android:text=" #"
/>
<EditText
android:id="#+id/ItemCaption"
android:layout_width="270dp"
android:layout_height="50dp"
android:layout_margin="3dip"
android:imeOptions="actionDone|flagNoExtractUi"
android:inputType="textNoSuggestions"
android:singleLine="true" >
</EditText>
<Button
android:id="#+id/buttonAdd"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_margin="3dip"
/>
</LinearLayout>
<?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:background="#fff"
android:orientation="horizontal" >
<ListView
android:id="#+id/MyList"
android:layout_width="370dp"
android:layout_height="160dp"
android:layout_gravity="center|top"
android:layout_marginLeft="150dp"
android:descendantFocusability="beforeDescendants"
>
</ListView>
<ImageView
android:id="#+id/image1"
android:layout_width="80dp"
android:layout_height="80dp"
/>
</LinearLayout>
mark the answer right if it was useful!!
You can create Elements (like Buttons, Views, and so on) and add them to an existing View like:
myListView.addChild(CreatedButton);