I am using cardslib by gabrielemariotti. I am using an expandable ListView Card. I have a custom layout inside it, and on of the views inside the ListView Card is an image view, name remove. I am planning to use the Remove imageview as a trigger to dynamically remove a card from the list.
The problem is, there are two layout involved on a Card, the header and the main content. The Cards by the way are contained inside the ListView. One of the layout, is the remove imageview is located. The code below can remove a Card by clicking it. CardListView
CardHeader header = new CardHeader(getActivity());
//Set the header title
header.setTitle(titleHeader);
//Set visible the expand/collapse button
header.setButtonExpandVisible(true);
header.setOtherButtonClickListener(new CardHeader.OnClickCardHeaderOtherButtonListener() {
#Override
public void onButtonItemClick(Card card, View view) {
Toast.makeText(getActivity(), "Drawable", Toast.LENGTH_SHORT).show();
if(mCardArrayAdapter!=null){
mCardArrayAdapter.remove(card); //It is an example.
}
}
});
The code below will trigger a Toast when the imageview Remove is clicked,
#Override
public void setupInnerViewElements(ViewGroup parent,View view){
//Add simple title to header
if (view!=null){
TextView mTitleView=(TextView) view.findViewById(R.id.card_header_inner_simple_title);
if (mTitleView!=null)
mTitleView.setText(mTitle);
ImageView img = (ImageView)view.findViewById(R.id.cancel);
img.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
}});
}
}
I wanted to combine the two. The library
Cardslib works like first you add all your cards to one cards array and then you initalize your CardArrayAdapter with your cards array.
It's same as using ListView. So deleting one item is also same as delete from your ListView.
1) On any event remove the card from cards array.
2) Call notifyDataSetChanged(); on your CardArrayAdapter object. It will update the Cards list and remove the deleted card from the view as well.
Related
I'm working on android app that creat layout with textView & checkbox programmatically for each text user input in EditText, and when the user select one of the checkbox and click on delete button the layout that contain that checkbox remove from the main layout
public void plusBtn(View view)
{
item = actv.getText().toString(); // text from EditText
actv.setText("");
creatt();
}
public void deletBtn(View view)
{
if(chbox.isChecked()){
linear.removeView(linr);
}
}
public void creatt()
{
linr = new LinearLayout(this);
linr.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linr.setOrientation(LinearLayout.HORIZONTAL);
linr.setGravity(Gravity.RIGHT);
TextView txt = new TextView(this);
txt.setText(item);
linr.addView(txt);
chbox = new CheckBox(this);
linr.addView(chbox);
linear.addView(linr); // main layout
}
But when I click on delete button just the last layout removed, and that not what I want.
though the technique you're using is not fine. but let me give you a shortcut. in your deletBtn method try to get the reference of the desired deleted view (try to handle case of id or boolean you've to make logic) and enclose this line linear.removeView(linr); into that condition.
Summery, only delete the view which meets the condition.
Alternative, do your whole task from scratch with the help of Recycler-view. as it Recycler-view will give you exact location of cell.
I'm developing an activity that makes the user create a post, so when the user clicks on a button, a layout must be made visible that makes the user choose what genre the movie is:
cTBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
postaCardVIew.setVisibility(View.GONE);
cT.setVisibility(View.VISIBLE);
//The recyclerview should start here
}
});
I have an array that contains all genres:
String genrelist[] = {"Commedia", "Animazione", "Anime", "Avventura", "Azione", "Biografico", "Documentario", "Drammatico", "Fantascienza","Fantasy",
"Guerra", "Horror", "Musical", "Storico", "Thriller", "Western", "Giallo", "Sentimentale",
};
I also created a layout with a cardview which will then be inflated which then represents all the items:
I would like to understand how to use recyclerview to output on each cardview one genre by one contained in the array. I have tried to look for the documentation online but it is still not clear to me. Could somebody help me?
I'am trying to delete a card having in recyclerview. The problem is when i click the delete button in my card the changes does not reflect the next activity. When i click Delete Button the textview and the imageview must change accordingly. But that is not happening.
this is my onclick in viewHolder. I have used Interface. :
public void onClick(View view)
{
//get data from array list
if(view.getId()==R.id.cont_root){
if(itemClickCallback!=null) {
itemClickCallback.onRootClick(getAdapterPosition());
}
}
else{
delete(getAdapterPosition());
}
}
}
This is my delete Method in the adapter :
public void delete( int position){
listData.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount()-1);
}
This is my code when a Cardview is clicked .Card's name is Root.
public void onRootClick(int p) {
//adapter.setItemClickCallback(this);
adapter.notifyItemChanged(p);
ListItem item =(ListItem)listData.get(p);
Intent i = new Intent(this,SecondActivity.class);
Bundle extras=new Bundle();
extras.putString(EXTRA_QUOTE, item.getTitle());
extras.putInt(EXTRA_ATTR,item.getImageResID());
i.putExtra(BUNDLE_EXTRAS,extras);
startActivity(i);
}
Note : i have not used interface method for deleting the element.
Please help. Iam new to Recyclerview. :(
You have to use the Methodes which relate to "what user is seeing" I can call them (Layout side Methods) and they are :
getLayoutPosition()
findViewHolderForLayoutPosition(int)
The first one returns the view position in accordance to the Layout it self and not the adapter
and the second one returns the ViewHolder for the item in the given position of the data set
In your code you are using what i can call (Adapter side Method)
getAdapterPosition()
any changes using The (adapter side Methods) will not be reflected to Layout untill you use (the Layout side Methods)
Hope This helps
I want to thank you in advance for the solution to my question.
I have a custom adapter for my listview. the list item has an imageView(1), a progressBar(3), and a download button(2).
When the download button for each list item is clicked, I grab vital details of the listitem such as the position of the view, the resourceid of the button, the resource id of imageview and the resource id of progress bar, then i make an parcealable object of class "Download".
Download download = new Download();
download.setUrl(url);
download.setButtonResource(this.downloadBt.getId());
download.setCreativeWork(creativeWork);
download.setDownloadBt(this.downloadBt);
download.setProgressBarResource(this.progressBar.getId());
download.setProgressBar(this.progressBar);
download.setContext(activity);
download.setViewResource(view.getId());
Intent intent = new Intent(activity, BroadcastService.class);
intent.putExtra("download", download);
Log.e("download button", url);
activity.startService(intent);
I start a service which does the download and reports back using a broadcast. When this broadcast message is received I want to update the progress bar.
My problem is how to get the progress bar concerned from my mainactivity which receives the message. How do I reference this?
I am currently using a parcelable object to pass the resource id for the progressbar, all the way from the adapter to the service, then to the receiving activity(mainActivity) using the intent's putExtra(), then I do this at the mainActivity
Download download = (Download)intent.getParcelableExtra("download");
ProgressBar progressBar = (ProgressBar)findViewById(download.getProgressBarResource());`
The issue here is that always only returns the first item of the list, regardless of which listitem is clicked. I would want to get each unique listitem and update the progress bar and the download button. Thanks
I am assuming that you show this layout in a ListView or RecyclerView. First thing that need to do is, add a field for index in your Download class to identify the position in the list from where that button was pressed. Next, pass the index from your getView() or onBindViewHolder() to the function that creates the Download object and add that index to the object.
When you receive the when you broadcast any update from your Service, do include the index value. Now, in your code in your activity where you receive the broadcast value, extract the value of index and the progress. Now, you could write something like this to update the progress of that view in the list:
int index = ... //some value you received in broadcast
int progress = ...//some value you received in broadcast
View view = listView.getChildAt(index);
if (view != null) {
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
if (progressBar != null) {
progressBar.setProgress(progress);
}
}
This can be one of the solution to your problem
First You need to pass progress value to the adapter that may be zero in start.
Implement setOnItemClickListener for your listview
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
//now point to your item position and do your stuff here
}
});
When ever you receive broadcast for download progress. Just update your ListView by Nullifying previous items with new items containing download progress values.
Not sure if this is what you looking for...
In you adapter. do below.. (I take a sample from my project which do a delete card from the list, when the delete button is clicked)
In getView function inside adapter.
Card card = list.get(position);
viewHolder.imgDelete.setTag(card);// --> i set the whole card info to tag
outside the adapter.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
if (id == R.id.iv_delete_card) {
//System.out.println("---> Card: " + tag);
//delete card
Object tag = v.getTag();
if (tag != null && tag instanceof Card) {
promptDeleteCard((Card) tag);
}
}
}
I am trying to take all the rows from my db and add it to the current layout, also, making each row clickable in the layout to take the user to a new screen with the id...
Here is my current code, but stuck on that part... I understand that I can put an onClickListener, but then does it have to be a button?
For a visual representation refer to a notepad app on any device where each note title appears and clicking on it takes you to that note.
public class MainActivity extends Activity implements OnClickListener {
private Button add_new_dictionary;
// Database helper
private DatabaseHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// db setup
db = new DatabaseHelper(getApplicationContext());
// get all dictionaries
List<db_dictionary> allDictionaries = db.getAllDictioniaries();
for (db_dictionary dictionary_found : allDictionaries) {
// create new view for each dictionary name include id and make it
// dynamic and include onclick to take to dictionary_view screen
Button dictionary_button = new Button(this);
}
add_new_dictionary = (Button) findViewById(R.id.add_new_dictionary);
}
#Override
public void onClick(View v) {
if (v == add_new_dictionary) {
Intent add_new_dictionary_intent = new Intent(MainActivity.this,
add_new_dictionary.class);
startActivity(add_new_dictionary_intent);
}
}
}
To re-iterate the question: How do I go about dynamically taking rows from my db and adding it to my layout dynamically based on how many results are returned from the query? (However, the rows should be able to point to a new screen with the dictionary id)
All views in android can implement the OnClickListener interface. So no, it doesn't HAVE to be a button.
As you've decided to use the activity to handle this then you need to tell your code to pass the event to your implementation wihin your activity.
// create new view for each dictionary name include id and make it
// dynamic and include onclick to take to dictionary_view screen
Button dictionary_button = new Button(this);
dictionary_button.setOnClickListener(this);
A trick I use to store information is the setTag method which would allow you to retrieve the correct reference during your onClick:
dictionary_button.setTag(some_record_id);
Then retrieve it later:
#Override
public void onClick(View v) {
if (v == add_new_dictionary) {
Intent add_new_dictionary_intent = new Intent(MainActivity.this,
add_new_dictionary.class);
startActivity(add_new_dictionary_intent);
}
else (
Object tag = v.getTag();
//now launch the detail activity using the data from the tag
}
}
You should really look into ListAdapters and cursors to do this properly, but this method should get you going for now
If you need to pick data from a db and show it as a list (getting click events) you should probably look into CursorAdapter and ListView
http://developer.android.com/reference/android/widget/CursorAdapter.html
http://developer.android.com/reference/android/widget/ListView.html
You can fins many examples on the web on how to use a cursoradapter and the listview