Changing a Item in a listView on a click - android

I want to make a ListView, when you long click on an item, the item will be added to the buttons: delete and update (only the clicked item):
So I have made a ListView in my MainLayout:
<ListView
android:id="#+id/neighborhood"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/allAddresses"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/AddresSearch" />
and a Custom ListView Item lay out, which contains 3 TextViews:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".passItemView">
<TextView
android:id="#+id/num"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="28dp"
android:text="cv"
app:layout_constraintEnd_toStartOf="#+id/house"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/house"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:text="cvc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/pass"
android:layout_width="86dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:text="cv"
app:layout_constraintEnd_toStartOf="#+id/num"
app:layout_constraintTop_toTopOf="parent" />
and this layout is conneted to a class "passItemView", and there, what I did is that on longClick on the layout it will add 2 button (or even just will make a toast)
But it does nothing.
public class passItemView extends AppCompatActivity {
Button delete = new Button(this);
Button update = new Button(this);
ConstraintLayout btnLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass_item_view);
btnLayout = (ConstraintLayout) findViewById(R.id.button_layout);
btnLayout.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(passItemView.this,"Toast it",Toast.LENGTH_LONG).show();
btnLayout.addView((delete));
btnLayout.addView((update));
return false;
}
});
Thank you for your Help.

use an boolean value (ex : showButtons) and initialize set it false for all items.
when you clicked someitem make items showButtons variable true and notifydatasetchange (reload lsitview) again.

Related

make lottie animation setVisibility immediately

i have an adroid project and i want to show a lottie animation during the time that an algorithm run but it does not become visible at time.
in my xml file:
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="250dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
android:visibility="gone"
app:lottie_rawRes="#raw/loading" />
// i tried also with android:visibility="invisible"
in java:
LottieAnimationView lottieanimationview;
lottieanimationview = findViewById(R.id.animation_view);
in specific function where i want to start to see the animation, i write:
lottieanimationview.setVisibility(LottieAnimationView.VISIBLE);
then, inside this function i have a call to another function that run the algorithm (in a cpp file that in the project).
now, instead of get visible and then run the another function (i want it to be like a loading animation),
the lottie get visible just after the run of the second function done. how can i make the lottie get visible immediately when i call setVisibility?
thank you
Ok.. Here is the code which I tried :
I have some lottie views in this(Screenshot attached below).
But we will be only using toggle view and thumbsDown
When we switch Toggle on, we are setting thumbsdown view visibilty gone
and when we switch Toggle off, thumbsdown view is visible again.
activity_main.xml
<!-- Custom Action Bar -->
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lav_actionBar"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_fileName="gradient_bg.json"
app:lottie_loop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Demo Lottie"
android:textColor="#android:color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="#+id/lav_actionBar"
app:layout_constraintEnd_toEndOf="#+id/lav_actionBar"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/lav_actionBar" />
<!-- Thumbs Up Button -->
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lav_thumbUp"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="80dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="false"
app:lottie_fileName="thumb_up.json"
app:lottie_loop="false"
app:lottie_speed="1.25" />
<!-- Thumbs Down Button (We just rotate the previous one by 180 deg ;) )-->
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lav_thumbDown"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="80dp"
android:layout_marginBottom="8dp"
android:rotation="180"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="false"
app:lottie_fileName="thumb_up.json"
app:lottie_loop="false"
app:lottie_speed="1.25" />
<!-- Toggle Switch -->
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lav_toggle"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lav_thumbUp"
app:layout_constraintVertical_bias="0.4"
app:lottie_autoPlay="false"
app:lottie_fileName="toggle_switch.json"
app:lottie_loop="false"
app:lottie_speed="1.75" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
LottieAnimationView thumb_up;
LottieAnimationView thumb_down;
LottieAnimationView toggle;
LottieAnimationView imprint;
int flag = 0;
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thumb_up = findViewById(R.id.lav_thumbUp);
thumb_up.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
thumb_down.setProgress(0);
thumb_down.pauseAnimation();
thumb_up.playAnimation();
Toast.makeText(MainActivity.this, "Cheers!!", Toast.LENGTH_SHORT).show();
//---- Your code here------
}
});
thumb_down = findViewById(R.id.lav_thumbDown);
thumb_down.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
thumb_up.setProgress(0);
thumb_up.pauseAnimation();
thumb_down.playAnimation();
Toast.makeText(MainActivity.this, "Boo!!", Toast.LENGTH_SHORT).show();
//---- Your code here------
}
});
toggle = findViewById(R.id.lav_toggle);
toggle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeState();
}
});
} //
private void changeState() {
if (flag == 0) {
toggle.setMinAndMaxProgress(0f, 0.43f);
toggle.playAnimation();
flag = 1;
//---- Your code here------
thumb_down.setVisibility(View.GONE);
} else {
toggle.setMinAndMaxProgress(0.5f, 1f);
toggle.playAnimation();
flag = 0;
//---- Your code here------
thumb_down.setVisibility(View.VISIBLE);
}
} }
Main thing is :
thumb_down.setVisibility(View.GONE);
thumb_down.setVisibility(View.VISIBLE);
Hope it helps.

TextView Edit Text is not responding to Click Events

I have a layout with three TextViews, two TextViews are responding to Click Events but the last one is not responding. I am using Navigation to navigate to other fragments. The OnClick method is called inside an adapter which implements view.OnClickListener.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_marginStart="#dimen/defaultMargin"
android:layout_height="wrap_content">
<TextView
android:text="How CADO works"
style="#style/CircularBook"
android:drawableEnd="#drawable/arrow_right"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/howCadoWorks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
app:layout_constraintTop_toBottomOf="#id/howCadoWorks"
android:layout_width="match_parent"
android:background="#color/pinkishGray"
android:layout_height="0.5dp"
android:id="#+id/view3"></View>
<TextView
android:text="FAQ"
style="#style/CircularBook"
android:layout_width="0dp"
android:drawableEnd="#drawable/arrow_right"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/faqs"
app:layout_constraintTop_toBottomOf="#+id/view3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<View
app:layout_constraintTop_toBottomOf="#id/faqs"
android:layout_width="match_parent"
android:background="#color/pinkishGray"
android:layout_height="0.5dp"
android:id="#+id/view4"></View>
<TextView
android:text="Contact Us"
android:drawableEnd="#drawable/arrow_right"
style="#style/CircularBook"
android:layout_width="0dp"
android:layout_height="#dimen/buttonHeight"
android:id="#+id/contactus"
app:layout_constraintTop_toBottomOf="#+id/view4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
OnClickMethod
public void onClick(#Nullable View view) {
NavController var3 = Navigation.findNavController(view);
if(view.getId()==R.id.contactus){
**var3.navigate(R.id.contactus_fragment);**
return;
}
else if (view.getId()==R.id.faqs){
var3.navigate(R.id.faqFragmnt);
return;
}
else if (view.getId()==R.id.howCadoWorks){
Toast.makeText(view.getContext(),"Feature is in Progress", Toast.LENGTH_SHORT).show();
// var3.navigate(R.id.contactus_fragment);
return;
}
else
{
}
}
I have tried multiple ways, rechecked id's as well but nothing is working.
as #Nilesh Rathod said, you need to make sure that all your text view have set an on click listener like:
textView.setOnClickListener(this);
or you can also:
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here you put the code you want to use for the specific textView click event
}
});

Animate LayoutChanges does not work properly

I want to animate text in my textswitcher. I went through all the stack questions and the documentation, but they didn't solve my problem.
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv__inc_pre_sing__screen_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:gravity="center"
android:text="PRACTICE"
android:textAllCaps="true"
android:textColor="#color/colorAccent"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.ConstraintLayout
android:id="#+id/rl__inc_pre_sing__tm_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#efff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv__inc_pre_sing__screen_title">
<ImageView
android:id="#+id/tv__inc_pre_sing__quotation_mark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:src="#drawable/ic_archive_black_24dp"
app:layout_constraintTop_toTopOf="parent" />
<TextSwitcher
android:id="#+id/tv__inc_pre_sing__teacher_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/tv__inc_pre_sing__quotation_mark"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_archive_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv__inc_pre_sing__teacher_message" />
</android.support.constraint.ConstraintLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Click Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<View
android:layout_width="0dp"
android:layout_height="16dp"
android:background="#drawable/ic_launcher_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/rl__inc_pre_sing__tm_container" />
</android.support.constraint.ConstraintLayout>
Here is my code
public class Main3Activity extends AppCompatActivity {
private TextSwitcher mSwitcher;
Button btnNext;
int currentIndex=-1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Resources resources = getApplicationContext().getResources();
final String[] textString = resources.getStringArray(R.array.teacher_messages);
btnNext = findViewById(R.id.button);
mSwitcher = findViewById(R.id.tv__inc_pre_sing__teacher_message);
mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(Main3Activity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextColor(Color.BLUE);
return myText;
}
});
// Declare the in and out animations and initialize them
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);
// set the animation type of textSwitcher
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
// ClickListener for NEXT button
// When clicked on Button TextSwitcher will switch between texts
// The current Text will go OUT and next text will come in with specified animation
btnNext.setOnClickListener(new View.OnClickListener() {
//
public void onClick(View v) {
// TODO Auto-generated method stub
currentIndex++;
// If index reaches maximum reset it
if(currentIndex==textString.length)
currentIndex=0;
mSwitcher.setText(textString[currentIndex]);
((ViewGroup) findViewById(R.id.rl__inc_pre_sing__tm_container)).getLayoutTransition()
.enableTransitionType(LayoutTransition.CHANGING);
}
});
}
}
The animation on ConstraintLayout whose id is rl__inc_pre_sing__tm_container does not work properly. Suppose my TextSwitcher has text whose length is greater than 3 and then TextSwitcher shows text whose length is 1 then the ConstraintLayout does not animate, if the next text is of length 1 then the constraintlayout animates.
I am not able to figure out this weird behaviour.
Here is the issue: When the TextSwitcher transitions from X lines of text to something less than X lines then the TextSwitcher's height does not change.
The TextSwitcher inherits some behavior from ViewAnimator which considers the height of all views when determining the height of the widget. The solution is to add the following XML to the TextSwitcher definition in the layout file that will inhibit this behavior:
android:measureAllChildren="false"
Thanks to this Stack Overflow answer that states all this better than I have.

Cannot Click List Item in HorizontalScrollView

I'm noticing that by adding the HorizontalScrollView it's disabling my ability to select items on my ListView. This seems like a pretty basic/common feature to want to add (I want to be able to swipe my item to the left to offer a delete option) but HorizontalScrollView appears to be incompatible with items that you would like to also touch to select. Note that if I change HorizontalScrollView to a LinearLayout or otherwise the items become selectable.
I was suspecting that touch listeners may be ineffective due to the fact that the swiping capability overrides any other kind of touch, but I can't really think of why it doesn't work otherwise. Can anyone help to resolve this issue? I would like to be able to use HorizontalScrollView alongside a click compatibility (unless there is another way to achieve the same functionality).
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraintLayout4"
android:layout_width="384dp"
android:layout_height="110dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/task_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:layout_marginTop="28dp"
android:fontFamily="#font/robotolight"
android:text="#string/task_name"
android:textColor="#android:color/black"
android:textSize="24sp"
app:layout_constraintStart_toEndOf="#+id/task_view_icon"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/task_view_icon"
android:layout_width="77dp"
android:layout_height="77dp"
android:layout_marginStart="17dp"
android:layout_marginTop="17dp"
android:contentDescription="#+string/icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/main_page_icon_switch_x4" />
<TextView
android:id="#+id/task_view_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="36dp"
android:fontFamily="#font/robotolight"
android:text="#string/duration"
android:textColor="#android:color/black"
android:textSize="14sp"
app:layout_constraintStart_toEndOf="#+id/task_view_name"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/task_view_detail"
android:layout_width="228dp"
android:layout_height="31dp"
android:layout_marginStart="17dp"
android:fontFamily="#font/robotolight"
android:text="#string/task_view_detail_literal"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="#+id/task_view_icon"
app:layout_constraintTop_toBottomOf="#+id/task_view_name" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="#+id/task_delete"
android:layout_width="116dp"
android:layout_height="110dp"
android:background="#color/colorPrimary"
app:layout_constraintEnd_toEndOf="#id/constraintLayout4"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/task_delete_literal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="13dp"
android:layout_marginTop="39dp"
android:fontFamily="#font/roboto_regular"
android:text="#string/delete"
android:textColor="#android:color/background_light"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</LinearLayout>
</HorizontalScrollView>
For anyone having this problem, I managed to find a way to workaround this. My problem was that I could not click on the ListView item because of the HorizontalScrollView setup in the XML, however in my case I was using an adapter along with my ListView (that allows you link your listView with your actual list row view) and in that adapter I simply defined and linked the container of the view that I want to press and set an onClickListener.
This goes in your custom adapter:
ConstraintLayout myLayout = convertView.findViewById(R.id.constraintLayout1);
ConstraintLayout anotherLayout = convertView.findViewById(R.id.constraintLayout2); //lets' say I'm using this to delete the item when clicked
myLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent exampleIntent = new Intent(viewParent.getContext(), SomeActivity.class);
}
});
anotherLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(view.getContext());
builder.setMessage("Are You Sure?").setNegativeButton("No", null);
builder.setPositiveButton("yes", new android.support.v7.app.AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Database database = new Database(viewParent.getContext());
database.deleteItem(item);
List<Item> items = database.getItems();
ListView list = (ListView) viewParent.findViewById(R.id.my_linear_list);
//List<Item> items = new Database(viewParent.getContext()).getItems();
TaskAdapter adapterNew = new ItemAdapter(viewParent.getContext(), tasks);
list.setAdapter(adapterNew);
}
});
builder.show();
}
});

How to get information from a customized ListView in Android

I have a ListView that I have customized to display information gathered from an SQLite database. Each item is a represented using a CardView widget containing TextViews. I have set the ListView property in the .xml file to:
android:choiceMode="singleChoice"
allowing the user to make a selection from the list.
When the user clicks the floating action button, I want to be able to take information displayed in one of the TextViews of the CardView selected in the ListView.
I have looked at other questions on the topic and they suggest using the onClick method, but I do not understand how that would work because I do not want to retrieve the information when the user makes the selection from the list. Rather, I want the information when they have selected the floating action button.
Here is part of my Activity code:
public class SetViewerActivity extends AppCompatActivity {
private static final String TAG = "SetViewerActivity";
private boolean fabState;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set contents of activity
setContentView(R.layout.activity_set_viewer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// set action for main floating action button
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fabState = false;
fab.setOnClickListener(new View.OnClickListener() {
// get set info and edit in new Activity
#Override
public void onClick(View view) {
editSelectedSet(view);
}
});
// set up list view and populate with custom set info from database
SQLiteDatabase db = new MemCardSQLiteHelper(this).getReadableDatabase();
Cursor cursor = db.rawQuery(MemCardDbContract.Sets.SELECT_ALL_SETS,
null);
ListView lvSets = (ListView) findViewById(R.id.listViewSets);
SetViewAdapter adapter = new SetViewAdapter(this, cursor);
lvSets.setAdapter(adapter);
}
private void editSelectedSet(View view) {
// HOW DO I FIND THE SET INFORMATION HERE ??
// start activity with selected set information for editing
Intent intent = new Intent(this, CreateNewSet.class);
intent.putExtra(MainActivity.CREATE_SET_NUM, selectedSet);
startActivity(intent);
}
}
and here is my xml for each ListView item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
app:contentPadding="2dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewName" />
<TextView
android:id="#+id/textViewColour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toBottomOf="#+id/textViewAmount" />
<TextView
android:id="#+id/textViewSetId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
You can assign an onClick on each item and when an item is selected (clicked) than you can save the data as a field member. Then read the data where ever you want.

Categories

Resources