Android: How to refresh a tablelayout after removing a row? - java

I have a tablelayout that retrieves data from a *.txt file.
For every line of data in the txt file, there will be one row of data.
Let's say I have two rows of data in the txt file right now, it makes sense that two tablerows will be generated.
Then, I added a OnLongPressListener which, when called, will delete one row of data from the txt file.
Now's the first question: After deleting data in the txt file, how do I refresh my tablelayout to reflect that change?
Second question is: After I get my first question solved, is it possible to have some kind of animation where one row will fade out or slide out instead of just disappearing outright?
Thanks!

Not sure if you are still looking for answer. But I came across this post facing kinda the same problem. Plus the fact that I was forced to use TableLayout (the amount of code written using TableLayout is huge and it wasn't my call to switch to ListView).
What I ended up doing was to remove all the views from TableLayout using:
`tableLayout.removeAllViews();`
But that's not gonna work if the number of rows after removal changes drastically. I needed to invalidate my view too, using a handler. Here is the rest of my code.
protected static final int REFRESH = 0;
private Handler _hRedraw;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tree_view_activity);
_hRedraw=new Handler(){
#Override
public void handleMessage(Message msg)
{
switch (msg.what) {
case REFRESH:
redrawEverything();
break;
}
}
};
...
}
private void redrawEverything()
{
tableLayout.invalidate();
tableLayout.refreshDrawableState();
}
There is only one part left and that's the part where you send a message to your handler to refresh your view. Here is the code for it:
_hRedraw.sendEmptyMessage(REFRESH);

Why not use a ListView instead? It gives you better control when using an MVC model where there's a dataset tied to a View. So you could have a class that extends BaseAdapter. This class binds your data to the View and this class also has a method: notifyDataSetChanged() which is what should solve your problem.
You may find notifyDataSetChanged example and customListView helpful.
Let me know if you need any help further.

Generally in onCreate() we do all the stuff that shows the ui with text, try to put the code that makes up UI in a private method say setUpTabView() and try to call this from onCreate and even try calling this setUpTabView() when ever the text changed. This kind of approach i did in grid view. worked very well...!

Related

Is it possible to switch the layout to a different layout whilst an activity in running in Java?

Instead of moving views in a current layout, I was wondering if i could instead load a different layout whilst the program is running.
For example in the on create i would use:
setContentView(R.layout.layout1);
and then in an on click listener i would use:
setContentView(R.layout.layout2);
I say this since I am using a custom dialogue which prevents me from producing a dialog to overwrite it. I have attempted it but so far have only received errors. I would really like to know if this is possible.
This is possible, but risky and not recommended for a final product. The problem is, you cannot access already defined views once you have switched views. You need to assign them all again for the new layout. So once you switch the content view, re-initialize all of your views and anything that references the old layout. Calling a view from the old layout will cause a crash or error message.
Like CodeMagic said, it is best to use fragments and the FragmentManager for this. And really, this is not a stable way to produce good code. I recommend using separate fragments and using backstacks and such so that not only will your game work, but you can easily navigate back to the original fragment, rather than use makeshift code that may barely work.
After setContentView(R.id.yourLayout) is called, you need to re-instantiate all of your other views. So like say you used an ImageView view to show the color changes, well you need to instantiate that ImageView after you setContentView(R.id.yourLayout) so that it pulls from the new layout, and does not reference to the original layout.
Example:
ImageView imageView;
public void onCreate(Bundle b){
super.savedInstanceState(b);
setContentView(originalLayout);
//Instatiate all of your original Views.
imageView = (ImageView) R.id.yourImageView;
}
public void onButtonClick(View){
setContentView(newLayout);
imageView = (ImageView) R.id.yourNewImageView;
//All other views
}
If you need an example of the fragment manager solution go here: https://developer.android.com/training/basics/fragments/creating.html
and look through some of their examples on how to properly do what you are trying to do.

Weird behavior with selectAllOnFocus when EditText is in ListView

This problem has been bugging me for a while now. I have a list view and in each row there are three EditText in which user can enter data. When a user selects an edit box, the text should be selected, so when he types the first key, the previous text is removed.
This all works perfectly with selectAllOnFocus option set to true. The problem occurs when I have to call notifyDataSetChanged. There is a button in each row that deletes the row and that is when I call the notifyDataSetChanged method of the adapter. After this, when a user selects an EditBox, it doesn't selects the whole text, but the cursor sets to the left side of the text and an arrow is displayed below the cursor.
I tried setting selectAllOnFocus in code and in layout file, but no luck.
What should I try? Am I missing something?
I tried with disabling the focus changed event, no luck.
Thanks!
What is the expected behavior your trying to achieve?
EDIT:
Why not try firing off a two click events manually. Its a slightly hacky solution but if it works than who cares. What you can do is get the view from your list view that you want to have the text selected in and fire off the two clicks in a post like so
myEdit.post(new Runnable(){
public void run()
{
myEdit.performClick();
myEdit.performClick();
}
}
using the post would make sure that the events got fired after the view had been re-inflated after calling invalidate on your listview adapter
Since this was happening whenever I call adapter.notifyDataSetChanged(), I figured I should try to go around it. And this is what I ended up doing:
adapter.clear();
adapter = new AdapterOfWhateverItemsTypeIs(this, items);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
After this, I got the expected behavior of selectAllOnFocus option of EditText.

Will Android be able to handle a single large activity like this?

I'm making an Android soundboard. I currently have 12 pages for this specific category. Each page has its own activity and each page as 10 buttons.
Someone asked me today why I don't just use one activity and switch the setcontentview (XML) between the pages rather then having so many activities.
I thought about it and it doesn't seem like a bad idea. But I'm curious on if it really would be better.
This means that this one activity would have a 100+ switch case statement for its onclick and onlongclick methods. It would have over 200 variables. I would have to add over 200 listeners (although I think i could set the 10 listeners as I load the specific page rather than all at the beginning. I could also release the current ones before the new XML is displayed).
Will Android be able to handle a single activity like this? I plan on adding more sounds so maybe one day I might have 20 pages or so.
Yes, make it all one activity. Different Activities should only really be used for completely different use cases. Consider using a ListView or a GridView, or a 'ViewPager` inside of one Activity.
If you do end up using a ListView or anything that takes an Adapter, your Activity should look like this:
class MyActivity extends Activity{
int mySounds[] = {R.raw.s1, R.raw.s2}; //etc. could also be a 2d array, one for each page of sounds.
protected void onCreate(Bundle b)
{
setContentView(R.layout.myactivity);
ListView lv = (ListView) findViewById(R.id.myactivity_listView);
lv.setAdapter(new SoundAdapter(this));
}
class SoundAdapter extends BaseAdapter{
Context cont;
SoundAdapter(Context c){
this.cont = c;
}
getView(int id, View convertView){
if(convertView == null){
...
//inflate view to be a row/gridcell
}
convertView.setOnClickListener(new SoundClickListener(mySounds[id]));
}
}
class SoundClickListener extends View.OnClickListener{
int resId;
public SoundClickListener(int resId)
{
this.resId = resId;
}
protected void onClick(View v){
playSoundWithId(resId);
}
}
}
I'll address a few of your points individually:
Will android be able to handle it. Yes. Nevertheless, you should be smart about the way you design things.
Depending on the version you're running, you should look into either TabActivity or Fragments. If you build your activity out of these, you can base which ten or so Buttons have listeners on them at any given time.
Here's an even more radical idea: create a mother class that extends Activity and pass its constructor information on which buttons to display. This way, you have only 10 buttons or so in an Activity, but you have 12 activities.
Even more radical idea: extend button and put it as an inner class of your extension of Activity. This could lead you some interesting places.
If you ask a slightly better question, I could give you a better answer, but here's some ideas, run with it. Good luck.

Android: dynamic text creation?

Let's say I have some strings in Java that I've retrieved from a web script. It doesn't matter really, they're just strings stored in variables.
Now my question is how to dynamically append them to the application (to show the user) and possibly style their position, from Java.
To draw an analogy, I want to do something similar to the following in JavaScript:
var text = document.createElement('div');
text.appendChild(document.createTextNode("some string"));
text.style.position = "whatever";
// etc, more styling
theDiv.appendChild(text); // add this new thing of text I created to the main application for the users to see
I've looked into the TextView, and I don't seem to be using it properly (I don't understand the constructor I guess?). What I want to try right now is to have the user press a button in my application and then have some text dynamically generated. Here's what I've been trying:
search_button = (Button) findViewById (R.id.backSearchButton);
search_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String test = new String("testing!");
TextView test2 = new TextView(this); //constructor is wrong, this line gives me an error
test2.setText(test);
setContentView(test2);
}
});
As you can probably tell, I don't come from much of a Java background. I'm just trying to draw parallels to stuff I would want to do for a web app.
Thanks for the help.
Try:
TextView test2 = new TextView(ParentActivity.this);
replace "ParentActivity" with the name of your activity.
your this pointer is a reference to the OnClickListener that you have anonymously created. You need a pointer to the containing Activity.
You would probably be better constructing your text view in the onCreate of your activity and just setting the text from your onClick method
I'm new to Java and Android, myself. But I'll give your question a try.
You have to decide if you want to create the TextView in XML or dynamically in Java. I think creating it in XML in the layout creator is better.
I don't think you should be using setContentView(test2).
If you create a TextView dynamically, I don't think you need to put anything in its constructor. But you do have to add the TextView to the parent view. In other words, let's say you have a LinearLayout somewhere in your layout. You have to do: linearLayout1.add(myTextView) or something.
The rest of your code seems fine. But then again, I'm still new to this, myself. Let me know how helpful this answer is, I'll try Googling for more help if it's not enough.

Update Android TextView

I have an simple android app that I want to write on the display the value of a single field, that belongs to a different class.
Using the simple text view I can write the initial value of the field but I don't know how to update the text on the display whenever the field has changed value.
Btw, it is my first android app so im still lost
Here is my activity code:
public class findIT extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PositionHolder ph = new PositionHolder();
TextView tv = new TextView(this);
setContentView(tv);
//this.updateText();
tv.setText("You are at " + ph.getPosition());
}
}
You need to create a TextView in an xml layout as following:
<TextView
android:id="#id/textView01"
android:text="Enter your initial text "
android:layout_width="wrap content"
android:layout_height="wrap content"
></TextView>
Then write the following in your class after setContentView() function:
TextView textView = (TextView) findViewById(R.id.textView01);
textView.setText("Enter whatever you Like!");
Chris.... Here is a most basic way to develop an app. Divide up the problem into three pieces. The presentation or view. The algorithm or model. The Controller that responds to user and system events. This creates a "separation of concerns" such that the Controller owns the view and the model. You create the view using xml as in main.xml. You create a separate class to do the work say MyModel.java and of course there is the Controller or the Activity class say MyActivity.java. So the data comes from the model goes to the Controller that updates the view.
So your question is how to get the data from the model and update the view. Naturally this will take place in the controller, your Activity. The simplest way to do this is to put a button in the activity and when the user hits the button, call model.getLatestData() and update the view. This is PULLing data. The next way is for the Controller to check for an update say every minute. This is POLLING for data. The next way is for the Controller to register an interest in changes to the model and sit around waiting for the model to signal a change and then update the view. This is asynchronous PUSHING of data from the model to the controller and could be done with the OBSERVER pattern.
I know this makes no sense to you when you are struggling with trying to just get the code to work, but hopefully I have planted the seed of an idea in your head that will bother you and make sense sometime in the future.
JAL
You're going to need to define a layout in xml, then when you create the TextView, you associate it with its layoutID. Something like:
TextView tv = (TextView) findViewById(R.id.something);
I can explain a little more, if you need, but this will give you a starting place to look for more answers.

Categories

Resources