Android - Get text from inflated layout Listview - android

I have a listview in which I inflate a layout with multiple textviews and buttons. I understand to get the text from a view that was clicked is ((Textview)view.... However I am trying to get the text from the specific textview that is located in the layout in which the user clicked. I have tried using OnItemClick but when I use this the item must be focused before the any of the buttons functions work. I resorted to and prefer using onClickListeners in the getView method of my custom adapter. So simply put, how do I click a Button and get the text that is in TextView that is located in the appropriate inflated layout list view item, given that since each inflated layout is considered as one list item?
Here are pictures to clarify what i am looking for. Both layouts are members of a listview.
I want to click the button with the date on it and get the text from the textview in the middle of the layout. However when I click the button with the date on it, I can only get the text from the textview in the middle of the layout of the last child. If "My Party" is the first child in the listview and "3303 going away service..." is the second child, when I click the date button the code in my custom adapter returns the text from the last loaded text in the view which will be "3303 going away service". What I am trying to do is when I click the date button on "My party", get the text "My party". Like wise with the second child.
Here is the getView() in my custom adapter.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
viewHolder = new ViewHolder();
positionHolder = position;
Log.i("Position", "" + position);
if(convertView == null) {
try {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.post_layout, parent, false);
postLayout = convertView;
viewHolder.unameTV = (TextView) postLayout.findViewById(R.id.postUnameTv);
viewHolder.unameTV.setText(viewContent.get(index));
viewHolder.unameTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Starting new intent
Intent in = new Intent(getActivity(),
Profile.class);
// sending pid to next activity
String username =((TextView)view).getText().toString();
in.putExtra("username", username);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
viewHolder.fillSpace = (TextView)postLayout.findViewById(R.id.posthelpSpace);
viewHolder.fillSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewHolder.unameTV.performClick();
}
});
viewHolder.image = (ImageView) postLayout.findViewById(R.id.postProfPic);
DisplayImageOptions options = initiateDisplayImageOptions();
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewHolder.unameTV.performClick();
}
});
ImageLoader imageloader = ImageLoader.getInstance();
initImageLoader(getActivity());
imageloader.displayImage(viewContent.get(index + 1), viewHolder.image, options);
viewHolder.addToCalendarButton = (TextView) postLayout.findViewById(R.id.addToCalendarButton);
viewHolder.addToCalendarButton.setText(viewContent.get(index + 2));
viewHolder.addToCalendarButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(Calendar.MONTH, 2);
long time = cal.getTime().getTime();
Uri.Builder builder =
CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
builder.appendPath(Long.toString(time));
Intent intent =
new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
title = testText.getText().toString();
Log.i("Title", "" + title);
intent.putExtra("title", title); // **NOT WORKING**
startActivity(intent);
}
});
viewHolder.eventTitle = (TextView) postLayout.findViewById(R.id.postTitleTV);
viewHolder.eventTitle.setText(viewContent.get(index + 3));
viewHolder.eventTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
title = ((TextView)view).getText().toString();
Log.i("TITLE", "" + title);
}
});
testText = viewHolder.eventTitle;
viewHolder.eventImage = (ImageView) postLayout.findViewById(R.id.eventImage);
imageloader.displayImage(viewContent.get(index + 4), viewHolder.eventImage, options);
viewHolder.likesTV = (TextView) postLayout.findViewById(R.id.likesTV);
viewHolder.likesTV.setText("" + viewContent.get(index + 5));
viewHolder.planToAttendTV = (TextView) postLayout.findViewById(R.id.planToAttendTV);
viewHolder.planToAttendTV.setText(viewContent.get(index + 6));
viewHolder.addressTV = (TextView) postLayout.findViewById(R.id.postLocationTV);
viewHolder.addressTV.setText("" + viewContent.get(index + 7));
index = index + 8;
}
catch (IndexOutOfBoundsException ie)
{
ie.printStackTrace();
}
}
else
{
viewHolder = (ViewHolder) postLayout.getTag();
}
return postLayout;
}
UPDATE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:background="#drawable/fill_back"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/relativeLayout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/postUnameTv"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:gravity="center|center_vertical|center_horizontal"
android:textColor="#ff518eff"
android:textSize="12dp"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/postProfPic"
android:text="Joshua" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Public"
android:id="#+id/postProfileIcon"
android:background="#drawable/publicicon"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp"
android:layout_marginRight="70dp"
/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/postProfPic"
android:layout_below="#+id/postUnameTv"
android:layout_centerHorizontal="true"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="330dp"
android:id="#+id/eventImage"
android:layout_alignTop="#+id/space"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:layout_alignBottom="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:id="#+id/space" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:layout_above="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:id="#+id/space2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/postTitleTV"
android:layout_below="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:gravity="center|center_vertical|center_horizontal"
android:textSize="28dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:background="#drawable/details_button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginBottom="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/postLocationTV"
android:layout_below="#+id/postProfPic"
android:layout_alignParentStart="true"
android:drawableLeft="#drawable/locate_button"
android:drawablePadding="5dp"
android:gravity="clip_horizontal"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textSize="12dp"
android:singleLine="true"
android:layout_alignEnd="#+id/space"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/addToCalendarButton"
android:textSize="12dp"
android:layout_alignBottom="#+id/postLocationTV"
android:drawableLeft="#drawable/add_to_cal_button"
android:drawablePadding="5dp"
android:gravity="bottom"
android:layout_alignEnd="#+id/imageButton3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2 hours ago"
android:id="#+id/textView2"
android:textSize="8dp"
android:gravity="center_vertical|center_horizontal"
android:paddingRight="10dp"
android:textColor="#ff828084"
android:layout_above="#+id/eventImage"
android:layout_toRightOf="#+id/postProfPic"
android:layout_alignParentEnd="true"
android:layout_marginBottom="3dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/posthelpSpace"
android:layout_alignBottom="#+id/space"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/postProfPic"
android:layout_alignTop="#+id/postProfPic" />
</RelativeLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/likeButton"
android:background="#drawable/like_button_unsel"
android:layout_below="#+id/relativeLayout"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/likesTV"
android:textColor="#ffe11100"
android:layout_alignBottom="#+id/likeButton"
android:layout_toRightOf="#+id/likeButton"
android:layout_alignTop="#+id/likeButton"
android:gravity="center|center_vertical|center_horizontal"
android:paddingLeft="5dp"
android:paddingTop="5dp"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="0.3dp"
android:id="#+id/imageView"
android:layout_below="#+id/likeButton"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp"
android:background="#drawable/divider" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/planToAttendTV"
android:layout_below="#+id/likesTV"
android:paddingLeft="5dp"
android:layout_marginTop="10dp"
android:layout_alignParentEnd="true"
android:gravity="center"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="Yes"
android:id="#+id/button"
android:textSize="10dp"
android:layout_toRightOf="#+id/textView3"
android:background="#drawable/border_circular"
android:layout_marginLeft="2dp"
android:layout_below="#+id/planToAttendTV"
android:layout_marginTop="5dp"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="No"
android:id="#+id/button2"
android:textSize="10dp"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:background="#drawable/border_circular"
android:layout_marginLeft="8dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Are you attending?"
android:id="#+id/textView3"
android:gravity="center"
android:layout_alignBottom="#+id/button"
android:layout_toRightOf="#+id/likeButton"
android:layout_marginBottom="7dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="4dp"
/>

I would recommend making the child its own layout as a list item. I would then create a custom list view adapter which would make your layout much simpler as well as being able to get the text from the specific item clicked without having to worry about repeated code.

Allright I guess I figured out what you are trying to get. Correct me If I'm wrong.
Normally, the idea of using custom adapter is based on List of Objects. You need to store all the information you have in an Object and create an ArrayList of those objects.
Then, whichever child you click in the list view, you will get the clicked child's index. This index is also the index of Object in your ArrayList.
After you know which index is clicked, then you can simply get the Object from your ArrayList with this index.
e.g If the 1st list item is clicked, the position you will get is "0", then you will need to use yourArrayList.get(0).getTheDataYouWant()

Related

Android Custom ListView. Showing different row layout after clicking on it

I'm newbie in android studio and I face this problem for two hours. Was looking for some olution in other answers and other websites without any result.
I've got fragment with ListView which contain some tasks to do. All tasks are stored in Database and I'm using Custom CursorAdapter to represent these data.
I have got two different row_layouts, one is with just image, title of task and deadline. Second one has the same stuff, two buttons and description of single task. User after clicking on single row in ListView should see these additional buttons and TextView. I've tried to to by myself but i get lost.
Here is adapter code:
public class ChallengeAdapter extends CursorAdapter {
private int position = 1;
public static class ChallengeViewHolder {
ImageView challengeIcon;
TextView challengeTitle;
TextView challengeDeadline;
TextView challengeDescription;
Button bFinished;
Button bRemove;
}
public static final int[] challengeIcons = {
R.drawable.brain_icon,
R.drawable.book_icon,
R.drawable.workout_icon
};
public ChallengeAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
public void selectedItem(int position) {
this.position = position;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ChallengeViewHolder holder = new ChallengeViewHolder();
if(this.position == cursor.getPosition()) {
View view2 = LayoutInflater.from(context).inflate(R.layout.extended_row_challenge_list, parent, false);
holder.challengeIcon = (ImageView) view2.findViewById(R.id.ChallengeIconExtended);
holder.challengeTitle = (TextView) view2.findViewById(R.id.ChallengeTitleExtended);
holder.challengeDeadline = (TextView) view2.findViewById(R.id.ChallengeDeadlineExtended);
holder.challengeDescription = (TextView) view2.findViewById(R.id.ChallengeDescriptionExtended);
holder.bFinished = (Button) view2.findViewById(R.id.bFinished);
holder.bRemove = (Button) view2.findViewById(R.id.bRemove);
view2.setTag(holder);
return view2;
}
View view = LayoutInflater.from(context).inflate(R.layout.row_challenge_list, parent, false);
holder.challengeIcon = (ImageView) view.findViewById(R.id.ChallengeIcon);
holder.challengeTitle = (TextView) view.findViewById(R.id.ChallengeTitle);
holder.challengeDeadline = (TextView) view.findViewById(R.id.ChallengeDeadline);
view.setTag(holder);
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ChallengeViewHolder holder = (ChallengeViewHolder) view.getTag();
holder.challengeTitle.setText(cursor.getString(cursor.getColumnIndexOrThrow("title")));
holder.challengeDeadline.setText(cursor.getString(cursor.getColumnIndexOrThrow("deadline")));
holder.challengeIcon.setImageResource(challengeIcons[cursor.getInt(cursor.getColumnIndexOrThrow("icon_id"))]);
}
}
Here comes code of single row:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/normalRowLayout"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ChallengeIcon"
android:layout_width="80dp"
android:layout_height="80dp"
/>
<TextView
android:id="#+id/ChallengeTitle"
android:layout_width="270dp"
android:layout_height="25dp"
android:textStyle="bold"
android:textSize="18sp"
android:text="HERE COMES THE BASS!"
android:layout_toRightOf="#+id/ChallengeIcon"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:textColor="#3696E4"
/>
<TextView
android:id="#+id/ChallengeDeadline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="28 days 6 hours 7 minutes 16 secunds"
android:layout_toRightOf="#+id/ChallengeIcon"
android:layout_marginLeft="15dp"
android:layout_below="#+id/ChallengeTitle"
android:layout_marginTop="15dp"
android:textColor="#FF0000"
/>
and extended row:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/extendedRowLayout"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/ChallengeIconExtended"
android:layout_width="80dp"
android:layout_height="80dp"
/>
<TextView
android:id="#+id/ChallengeTitleExtended"
android:layout_width="270dp"
android:layout_height="25dp"
android:textStyle="bold"
android:textSize="18sp"
android:text="HERE COMES THE BASS!"
android:layout_toRightOf="#+id/ChallengeIconExtended"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:textColor="#3696E4"
/>
<TextView
android:id="#+id/ChallengeDescriptionExtended"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_toRightOf="#+id/ChallengeIconExtended"
android:layout_marginLeft="5dp"
android:layout_below="#+id/ChallengeTitleExtended"
android:layout_marginTop="15dp"
android:text="description description description description description description description description description description description description vdescription description description description description description description"
android:textColor="#000000"
/>
<TextView
android:id="#+id/ChallengeDeadlineExtended"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="28 days 6 hours 7 minutes 16 secunds"
android:layout_toRightOf="#+id/ChallengeIconExtended"
android:layout_marginLeft="25dp"
android:layout_below="#+id/ChallengeDescriptionExtended"
android:layout_marginTop="15dp"
android:textColor="#FD1103"
/>
<Button
android:id="#+id/bFinished"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="#+id/ChallengeDeadlineExtended"
android:layout_marginTop="10dp"
android:text="Finished"
android:background="#drawable/roundedbuttongreen"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
/>
<Button
android:id="#+id/bRemove"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="#+id/ChallengeDeadlineExtended"
android:layout_marginTop="10dp"
android:text="Remove"
android:background="#drawable/roundedbuttonblue"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
/>
Also adding Photo how it looks like when i set position in adapter to 1:

Cannot access resources in a layout in a PopupWindow, Objects are null referenced

I have an android application when clicked on an option from a side bar it goes to a fragment, and then into another fragment which has clickable radio buttons. When clicked on these it will create a popup window with some text fields in it.
Basically this is how the flow goes,
Activity --> Fragment 1 --> Fragment 2 --> PopupWindow
When i try to access the resources inside this popupWindow, in example:
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();
in the line damageComponenetAutoCompleteTextview.requestFocus(); it gives me an error,
Attempt to invoke virtual method on a null object reference
I believe it's due to a wrong view i'm referring when calling on the Resources. Hope someone could point me to what i'm doing wrong. Thanks in Advance.
This is the method i'm using to create the popupWindow.
public void showDamagedItemEntryPopup(RadioButton radioButton, View view){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewG = ((ViewGroup)view.findViewById(R.id.damaged_comp_popup));
//I tried passing the root view to inflate() method instead of passing it null. Didn't work.
//View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);
View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.popupAnimation);
//I tried setting the content view manually but didnt work
//popupWindow.setContentView(view.findViewById(R.id.damaged_comp_popup));
Button buttonClose = (Button)popupView.findViewById(R.id.close_add_component_btn);
// Close button damaged item popop window
buttonClose.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
originalAmount = (EditText)this.findViewById(R.id.popup_add_component_original_amount);
customerContribution = (EditText)this.findViewById(R.id.popup_percentage);
quantity = (EditText)this.findViewById(R.id.popup_quantity);
finalAmount = (EditText)this.findViewById(R.id.popup_add_component_final_amount);
remarks = (EditText)this.findViewById(R.id.popup_add_component_remarks);
// Item Spinner
itemSpinnerArray = new ArrayList<String>();
itemSpinnerArray.add("Select Item");
// Status Spinner
ArrayList<String> statusSpinnerArray = new ArrayList<String>();
statusSpinnerArray.add("MR");
statusSpinnerArray.add("DR");
statusSpinnerArray.add("SP");
// Error comes at this point initially. When these Resource access line codes are commented, the popup works fine.
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();
ArrayAdapter<String> itemSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, itemSpinnerArray);
damageComponenetAutoCompleteTextview.setThreshold(1);
damageComponenetAutoCompleteTextview.setAdapter(itemSpinnerArrayAdapter);
damageComponenetAutoCompleteTextview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//int index = cityNames.indexOf(actvCity.getText().toString());
itemSpinnerValue = (String) parent.getItemAtPosition(position);
Log.d("SK-->", "----------------------------------------------------------");
Log.d("SK-->","itemSpinnerValue: " + itemSpinnerValue);
}
});
statusSpinner = (Spinner)this.findViewById(R.id.popup_status_spinner);
ArrayAdapter<String> statusSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, statusSpinnerArray);
statusSpinner.setAdapter(statusSpinnerArrayAdapter);
//Creating a text Watcher
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
//String answerString = originalAmount.getText().toString();
if (originalAmount.getText().toString().trim().equals("") || customerContribution.getText().toString().trim().equals("")
|| quantity.getText().toString().trim().equals("")) {
// Error , one or more editText are empty
}
else
{
calculateFinalAmount();
}
//and now we make a Toast
//modify "yourActivity.this" with your activity name .this
//Toast.makeText(yourActivity.this,"The string from EditText is: "+answerString,0).show();
}
};
// Adding Text Watcher to our text boxes
originalAmount.addTextChangedListener(textWatcher);
customerContribution.addTextChangedListener(textWatcher);
quantity.addTextChangedListener(textWatcher);
// Show the popup
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}
This is the XML i'm using for the popup.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/damaged_comp_popup"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/popup_wire">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="2dp"
android:background="#color/popup_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/popup_damage_component_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:hint="#string/damaged_componenet_item_string"/>
<Spinner
android:id="#+id/popup_status_spinner"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/popup_damage_component_item"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp" />
<EditText
android:id="#+id/popup_add_component_original_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/popup_damage_component_item"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:layout_toRightOf="#+id/popup_status_spinner"
android:ems="10"
android:hint="#string/original_amount_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_percentage"
android:layout_width="52dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/popup_status_spinner"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="2dp"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="#string/percentage_string"
android:inputType="number" />
<TextView
android:id="#+id/popup_percentageMark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/popup_percentage"
android:layout_toRightOf="#+id/popup_percentage"
android:text="#string/percentage_string"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="1dp"
android:layout_marginRight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<EditText
android:id="#+id/popup_quantity"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/popup_percentage"
android:layout_alignBottom="#+id/popup_percentage"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="6dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/popup_percentageMark"
android:ems="10"
android:hint="#string/quantity_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_add_component_final_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/popup_quantity"
android:layout_alignBottom="#+id/popup_quantity"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/popup_quantity"
android:ems="10"
android:hint="#string/final_amount_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_add_component_remarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/popup_percentage"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="#string/remarks_string"
android:inputType="text|textMultiLine" />
<Button
android:id="#+id/add_component_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_below="#+id/popup_add_component_remarks"
android:background="#drawable/correct"
android:layout_marginBottom="15dp"
android:onClick="onSaveItem"/>
<Button
android:id="#+id/close_add_component_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popup_add_component_remarks"
android:layout_toRightOf="#+id/add_component_btn"
android:layout_marginLeft="10dp"
android:background="#drawable/cancel"/>
</RelativeLayout>
</LinearLayout>
It seems your damageComponenetAutoCompleteTextview belongs to the popupWindow which you inflated at top.
So try changing
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
To
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) popupView.findViewById(R.id.popup_damage_component_item);
And other relevant elements as well.

how to set the specific row item option(visible, invisible) in listview in android

frist my english skill weak.
description>
this is list view.
ㅡㅡㅡㅡㅡㅡㅡㅡ
ㅣㅡㅡㅡㅡㅡㅣㅢ <-- this is button , i init set invisible
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡㅣㅢ
ㅣㅡㅡㅡㅡㅡ ////// <-- i want make visible button
ㅣㅡㅡㅡㅡㅡ ////// <-- specific position
I make the custom ListView
ListView row contains texts, Button.
The Button is set invisible option in xml file.
then, I want set the visible specific row button.
I tried that and failed
after make ArrayList for ListView, marking matching position like this
for(i=0; i<arraylist.size(); i++){
int t41=Integer.parseInt(arraylist.get(i).getm());
if(month == t41){
confirm_replay[i]=true;
temp55=i;
}
}
I can set the textValue. through adapter.getItem(int position).
but i don't know, how to control specific button.
also try add button into adapter. failed..
also search question in google but my eng skill bad. failed
add my code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:paddingTop="10dp"
android:text="match day(weekend)"
android:textSize="28dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:background="#2d000000"
android:layout_width="match_parent"
android:layout_height="3dp">
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/m"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="10dp"
android:id="#+id/d"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/yoil"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/vsTeam"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/league"
android:paddingLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/공갈"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_youtube"
android:text="다시보기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/m"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:paddingLeft="10dp"
android:id="#+id/d"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/yoil"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/time"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/vsTeam"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/league"
android:paddingLeft="10dp"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/공갈"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_youtube"
android:text="다시보기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
</b>
adapter
class MlistViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<MatchInfomation> matchinformationlist = null;
private ArrayList<MatchInfomation> arraylist;
public MlistViewAdapter(Context context,
List<MatchInfomation> matchinformationlist) {
mContext = context;
this.matchinformationlist = matchinformationlist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<MatchInfomation>();
this.arraylist.addAll(matchinformationlist);
}
public class ViewHolder {
TextView m;
TextView d;
TextView yoil;
TextView vsTeam;
TextView time;
TextView league;
}
#Override
public int getCount() {
return matchinformationlist.size();
}
#Override
public MatchInfomation getItem(int position) {
return matchinformationlist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.activity_match_list, null);
button_youtube.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.google.android.youtube");
intent.putExtra("query", "Android");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
});
// Locate the TextViews in listview_item.xml
holder.m = (TextView) view.findViewById(R.id.m);
holder.d = (TextView) view.findViewById(R.id.d);
holder.yoil = (TextView) view.findViewById(R.id.yoil);
holder.vsTeam = (TextView) view.findViewById(R.id.vsTeam);
holder.time = (TextView) view.findViewById(R.id.time);
holder.league = (TextView) view.findViewById(R.id.league);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.m.setText(matchinformationlist.get(position).getm());
holder.d.setText(matchinformationlist.get(position).getd());
holder.yoil.setText(matchinformationlist.get(position).getyoil());
holder.vsTeam.setText(matchinformationlist.get(position).getvsTeam());
holder.time.setText(matchinformationlist.get(position).gettime());
holder.league.setText(matchinformationlist.get(position).getleague());
return view;
}
}
If you want to adjust a specific row, you can use the position parameter in your getView() method in adapter. For instance;
if(position==55){
holder.m.setVisibility(View.GONE);
}
I'm not sure i understand your question.
If i'm right you juste want your button to be invisble for specific row. To do so add the specific line at the end of your getView method
yourButton.setVisibility(View.INVISIBLE)
If you want to hide the entire row, the best way will probably be to change your adapter to diplay only row with content.
before setting the values to the view check the condition whatever you want like:-
if condition is true then set your view visible
else set your view invisible
if(true){
button.setVisibility(View.VISIBLE);
}else{
button.setVisibility(View.GONE);
}

How to get a smooth scroll in a listview with image and data

I have a listview that displays some data with a image for each row. The fact is that when I scroll the listview it freezes not scrolling smooth. My code is the following:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;
final int posicion = position;
final float latitud;
final float longitud;
final ParkingClass parkingItems = mListParkings.get(position);
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.activity_listado_parking, null);
//configure view holder
viewHolder = new ViewHolder();
viewHolder.imgBtnBorrarItem = (ImageButton)convertView.findViewById(R.id.imgBtnBorrarItem);
viewHolder.imageButtonFotoHolder = (ImageButton)convertView.findViewById(R.id.fotoParking);
viewHolder.imgBtnNotasItem = (ImageButton)convertView.findViewById(R.id.imgBtnNotasItem);
viewHolder.imgBtnRuta = (ImageButton)convertView.findViewById(R.id.imgBtnRuta);
//viewHolder.path = parkingItems.getFoto();
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder)convertView.getTag();
}
if (parkingItems.getFoto() != null)
{
if (parkingItems.getFoto() != "NF")
{
Bitmap bitmap;
bitmap = ImageHelper.decodeFile(parkingItems.getFoto());
viewHolder.imageButtonFotoHolder.setImageBitmap(bitmap);
}
else
{
viewHolder.imageButtonFotoHolder.setImageDrawable(mContext.getResources().getDrawable(R.drawable.no_photo_icon96));
viewHolder.imageButtonFotoHolder.setScaleType(ScaleType.FIT_XY);
}
}
TextView txtCiudad = (TextView)convertView.findViewById(R.id.txtCiudad);
txtCiudad.setText(parkingItems.getCiudad());
TextView txtCalle = (TextView)convertView.findViewById(R.id.txtCalle);
txtCalle.setText(parkingItems.getCalle());
TextView txtNum = (TextView)convertView.findViewById(R.id.txtFecha);
txtNum.setText(parkingItems.getFecha().toString());
if (parkingItems.getLatitud() != null)
latitud = parkingItems.getLatitud();
else
latitud = 0.0f;
if (parkingItems.getLongitud() != null)
longitud = parkingItems.getLongitud();
else
longitud = 0.0f;
return convertview;}
So, how can I get a smooth scrolling? Adding a asyntask? with asynctask I couldn't get a good performance. Neither with scrolllistener. How can I achieve a good development to get a smooth scroll?
Thank you in advanced.
UPDATE
My layout xml is the following:
<?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:id="#+id/fotoLayout"
android:padding="6dip" >
<ImageButton
android:id="#+id/fotoParking"
android:background="#null"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher"
android:gravity="left"
android:contentDescription="#string/imagen" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/fotoParking"
android:paddingLeft="5dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Fecha: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Ciudad: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:text="Calle: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtFecha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:text="TextView" />
<TextView
android:id="#+id/txtCiudad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:maxLines="1"
android:text="TextView" />
<TextView
android:id="#+id/txtCalle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:maxLines="1"
android:text="TextView" />
</RelativeLayout>
getview method should not do any heavy processes on UI thread for smoothly scrollable listview.
Instead of fetching bitmap and displaying it GetView method, use Universal Image Loader this to display images.

Why I am getting NullPointerException on ImageView?

My code is as follows:
activity_for_me.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/recco_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
<ImageView
android:id="#+id/checkin"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:onClick="ClickCheckIn"
android:src="#drawable/checkin" />
</RelativeLayout>
recco_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#layout/nine_patch" >
<ImageView
android:id="#+id/outlet_icon"
android:layout_marginLeft="15dp"
android:layout_height="55dp"
android:layout_width="55dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="15dp"
android:hint="Distance"
/>
<TextView
android:id="#+id/outlet_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_marginLeft = "15dip"
android:hint="outlet"
/>
<TextView
android:id="#+id/reward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/outlet_name"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_marginLeft = "15dip"
android:layout_marginTop="5dp"
android:textColor="#color/abc_search_url_text_holo"
android:hint="Reward"
/>
<View
android:id="#+id/rule"
android:layout_below="#id/reward"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_width="fill_parent"
android:layout_marginLeft = "15dip"
android:layout_height="1dp"
android:background="#c0c0c0"/>
<ImageView
android:id="#+id/favourite_icon"
android:layout_below="#id/rule"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_height="15dp"
android:layout_width="15dp"
android:src="#drawable/heart_empty"/>
<ImageView
android:id="#+id/loc_icon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/rule"
android:layout_marginLeft = "15dip"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/outlet_icon"
android:src="#drawable/map_pointer"/>
<TextView
android:id="#+id/locality"
android:layout_toRightOf="#id/loc_icon"
android:layout_toLeftOf="#id/distance"
android:layout_below="#id/rule"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft = "5dip"
android:layout_marginTop="5dp"
android:hint="Locality"
/>
</RelativeLayout>
Activity.java
public class ForMeActivity extends Fragment {
#SuppressLint("ClickableViewAccessibility")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout rootView = (RelativeLayout) inflater.inflate(
R.layout.activity_for_me, container, false);
ImageView favourite_heart = (ImageView) rootView
.findViewById(R.id.favourite_icon);
favourite_heart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(YourActivityName.this,
// "The favorite list would appear on clicking this icon",
// Toast.LENGTH_LONG).show();
System.out.println("Favourite Icon clicked");
}
});
list = (ListView) rootView.findViewById(R.id.recco_list);
ImageView check_in_img = (ImageView) rootView
.findViewById(R.id.checkin);
ImageView outlet_logo = (ImageView) rootView
.findViewById(R.id.outlet_icon);
final String data = getArguments().getString("data");
ForMeFile = new File(getActivity().getExternalFilesDir(filepath),
filename);
final String cookie = getArguments().getString("cookie");
System.out.print(cookie);
check_in_img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getActivity(), CheckInView.class);
startActivity(in);
System.out.println("Checkin Icon clicked");
}
});
}
I want to change image for favourite_icon ImageView when I click on it. But as soon as I click on the icon I get NullPointerException. What is the reason that click of check_in_img is working but click of favourite_icon is not working even though the current layout is the one which contains favourite_icon? Other sources of SO says to check the same.
favourite_icon is a part of recco_list.xml and you are trying to find it in activity_for_me.xml. The view does not exist there and hence, the exception.
R.id.favourite_icon is not there in activity_for_me.xml.
And if you want to access the items in the listview, I suggest you read about ListView and getView().

Categories

Resources