Images don't display but TextView contents is correct - android

I am working on an Android application that displays a list of items when a tab is selected, then displays detail information about an item that was selected from the list :
Activity1
Tab1 -(intent)-> Activity 2: item1
item2 -(intent)-> Activity 3: ImageView1
item3 TextView1
... ImageButton1
...
The details are to be displayed using ImageViews, ImageButtons, and TextViews. The data comes from a database query, so the TextView text and ImageView drawables have to be assigned dynamically.
The correct text from the database query gets assigned to each of the Views. I can see everything being added when I step through the code in the onCreate method of Activity3 in the debugger.
BUT, the drawables are only displayed when I select the first item in the ListActivity. If I select another item, all TextViews display the correct information but the drawables are not displayed for the ImageViews or ImageButtons. It looks like only the backgrounds are being displayed.
The ImageButton drawables are not being dynamically assigned, only the ImageView drawables are. The ImageButton drawables are set in the TableLayout XML that is associated with the activity. Here is one entry :
<TableRow android:id="#+id/row11" >
<ImageButton android:id="#+id/phonebutton"
android:maxHeight="50px"
android:maxWidth="50px"
android:background="#drawable/ic_phone"
android:adjustViewBounds="true"
style="?android:attr/buttonStyleSmall"
android:src="#drawable/phoneselector" >
</ImageButton >
<TextView android:id="#+id/phonenumber"/>
(note - Originally, I had android_drawable="#drawable/ic_phone", then tried adding android:background="#drawable/ic_phone", and finally created the following selector (phoneselector.xml) :
- end note)
If I select the first item again, its proper ImageView drawables are displayed along with the proper text in the TextViews. The ImageView drawables that belong to the other items have no problems being displayed in Activity2's ListView.
The code successfully sets an onClickListener for the button (also in Activity3.onCreate). If I click on what is displayed on the spot where the drawable should be, the phone number is dialed.
The code successfully sets an onClickListener for the button (also in DetailActivity.onCreate).
Has anyone seen anything like this before? I would appreciate any help figuring out what is wrong.
Here are more details because I wonder if the problem has to do with the use of the Views :
The Activity1 extends TabActivity and sets a TabHost view that contains a FrameLayout along with the TabWidget. Activity1 creates the tabs dynamically.
Activity2 extends ListActivity and sets the content to its ListView (via setContentView(getListView()).
When ListActivity.onListItemClick is invoked (item in list is selected), a new Intent is created that contains an identifier in a Bundle and Activity3 is started.
The started activity (in Activity3.onCreate...) sets the content to a TableLayout view defined in a .xml file (via setContentView(R.layout.details.xml).
Here is the code from Activity3.onCreate :
Cursor c = null;
DatabaseHelper myDbHelper = null;
ImageButton button = null;
TextView phoneText = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources res = getResources();
setContentView(R.layout.details);
Bundle bundle = this.getIntent().getExtras();
Integer id = (Integer) bundle.getInt("id");
myDbHelper = new DatabaseHelper(this);
c = myDbHelper.getWinery(id);
if (c != null) {
c.moveToFirst();
ImageView image1 = (ImageView) this.findViewById(R.id.iconimage);
String wholeString = c.getString(c.getColumnIndex("PhotoIcon"));
if (wholeString != null) { String[] splitString = wholeString.split(".jpg");
String sString = splitString[0];
int path = res.getIdentifier(sString, "drawable", "com.winerytour" );
Drawable drawImage1 = this.getResources().getDrawable(path);
drawImage1.setVisible(true, true); //added to try to fix problem
try {
image1.setImageDrawable(drawImage1);
image1.refreshDrawableState(););
} catch (RuntimeException e) {
e.getMessage();
}
image1.setAdjustViewBounds(true); }
.. set other TextView text in same way ...
phoneText = (TextView) this.findViewById(R.id.phonenumber);
String phone = c.getString(c.getColumnIndex("Telephone"));
phoneText.setText(phone);
button = (ImageButton) this.findViewById(R.id.phonebutton);
button.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
performDial(); } });
((ImageButton) button).refreshDrawableState(); // attempt to fix problem
} // end c != null
}

content to its ListView (via setContentView(getListView()). Don't do that. If you extend listactivity, the view is built it. If you don't want that, don't 'extend listview

Related

Click on first item in a GridView is not working

I have an activity which contains a GridView, each element of the GridView contains an ImageView and when the activity starts I want to perform a click on the first item's ImageView. I have tried several things which did not work:
gridView.performItemClick(gridView.getChildAt(0), 0, gridView.getAdapter().getItemId(0));
and with a performClick() directly.
However I think a found a way to do it by calling a function (performEmptyClick()) inside my custom GridViewAdapter which performs a click on the first item's ImageView.
My problem is that performEmptyClick() is called before the adapter's getView() is called (from debugging) and therefore the onClickListener() of the ImageView is not set up yet. Here is the code:
gridView = (GridView) findViewById(R.id.folderGridView);
folderViewAdapter = new FolderViewAdapter(this, R.layout.folder_item_layout, folderItems);
gridView.setAdapter(folderViewAdapter);
Bundle extras = getIntent().getExtras();
noFolder = extras.getBoolean("noFolder");
if (noFolder) {
folderViewAdapter.performEmptyClick();
}
Can anyone help me fix this?
Try
gridView = (GridView) findViewById(R.id.folderGridView);
folderViewAdapter = new FolderViewAdapter(this,
R.layout.folder_item_layout, folderItems);
gridView.setAdapter(folderViewAdapter);
Bundle extras = getIntent().getExtras();
noFolder = extras.getBoolean("noFolder");
if (noFolder) {
gridView.getChildAt(0).performClick();
}

How to get value of view from the click of other view in the same view parent

I am adding a layout on a Button click that layout contains a TextView and the 2 Spinners name spGrades and spMarks.
User can add multiple layout. So all I want is to get the value of Spinners on the TextView click . well I am getting of values of Spinner but I am facing following problems.
In Case1: Suppose there is just one layout added by the user let say
Student1MarksLayout then on the click of the TextView I am getting
the values of Spinners.
In Case 2: Suppose now user has added and other layout let say
Student2MarksLayout with same 2 spinners and a TextView . Now in
this case on the click listener of TextView I am getting the value
of spinners which are currently added. even though i click on the
TextView of the Student1MarksLayout. It brings the values of
Student2MarksLayout values.
Question:
So in fact I want to get the values of spinners differently from the
same view group in which the TextView click has been originated by the
user.
Please help me and any suggestion about this on how to maintain this .
On the function that creates the layout, add to final variables that hold the 2 Spinners. Then create the onClick function, and reference these 2 final variables. I have written a quick example (in a bit of shorthand)
void createLayout()
{
View v = inflate....
final Spinner a = v.findViewById...
final Spinner b = v.findViewById...
TextView t = v.findById...
t.setOnClickListener( new View.OnClickListener() {
    #Override
     public void onClick(View v) {
int val = a.get....
     }
});
}

android- Appending textView objects to a LinearLayout by clicking on a button from a different page (multiple times)

I have two Activities. Activity 1 is designed to take in user input (EditText), and has a button that (if clicked) will go to activity 2. In activity 2, there is a LinearLayout and a button that will take you back to activity 1. I can currently add one textView (containing the user input from activity 1) to the LinearLayout in activity 2, but I would like to add several textView objects to the LinearLayout. When I try to add user input any time after the first, it simply replaces the textView object that held the information from the user input that was entered the first time.
From Activity 1 (AddExercise):
public class AddExercise extends AppCompatActivity {
private EditText name;
private EditText weight;
private EditText sets;
private EditText reps;
private String deets;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_exercise);
Button button = (Button) findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToAddWorkout();
}
});
}
private void goToAddWorkout() {
Intent intent = new Intent(this, AddWorkout.class);
name = (EditText) findViewById(R.id.name);
weight = (EditText) findViewById(R.id.weight);
sets = (EditText) findViewById(R.id.sets);
reps = (EditText) findViewById(R.id.reps);
deets = name.getText().toString() + "\n\t\tWeight: " + weight.getText().toString() + "\n\t\tSets: " + sets.getText().toString() + "\n\t\tReps: " + reps.getText().toString();
intent.putExtra("details", deets);
startActivity(intent);
}
}
From Activity 2 (AddWorkout):
public class AddWorkout extends AppCompactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_workout);
LinearLayout vBox = (LinearLayout) findViewById(R.id.vBox);
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView tv = new TextView(this);
tv.setText(extras.getString("details").toString());
vBox.addView(tv);
}
Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToAddExercise();
}
});
}
}
You can try a public static List of String each time you can add your text to list and then On your second activity create text-view as per your list count. And add to your linear-layout.
For more click here....
So you want to be able to add multiple entries in your second activity, for every time you add one in the first. I would recommend a different and easier approach.
In your second activity, use a listview/recyclerview, instead of adding new textview. This has the added advantage that once you have added enough entries, scrolling won't be an issue.
Maintain a global list of entries, which you add the entries to. And populate the listview using this list.
you should try something like that as said by roshan-
((ArrayAdapter)listView.getAdapter()).insert(data_object, index);
((ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
use a shared preference for index... every time you come to second activity increment the index... on exit of app just clear the shared pref index variable.
Also you can add textview in your each list view item
There can be several ways to do it according to me, pick the one that suits your relevant application.
have a single activity and host two fragments. So you can share the data between the fragments using single activity. (Recommended way, I guess Fragments will ease the job for you.). Also you can store a local variable in Activity so that each time you start your application you can start it afresh, if its intended!
If not, you can use SharedPreferences. For each button click, add a string to the preference. When you add one more click, append the new data with a separator like ("|", "||").. So in Activity one write to the preference, in activity 2 read from the preference and display it as list view of dynamically create the linear layout and append it to the root layout.
Declare a static ArrayList in your Activity 1 and access it in activity 2. (Really bad way)

Activity not updating properly

I'm new to android, so maybe I'm doing something horribly wrong. I want to have a particular Activity that shows details about an instance of a "Creature" class for a game. Name, damage taken, that sort of thing.
I'm having a problem getting the creature data to be properly shown in the GUI objects. Both at initial creation (where it should copy the creature's name into the name field) and when a damage mark is added (where it doesn't update to show the proper image).
Here's my mini-example of what I have:
public class CreatureDetailActivity2 extends Activity
{
Creature creature;
public void addMark(View v)
{
// connected to the button via android:onClick="addMark" in the XML
creature.getTrack().addDamage(DamageType.Normal, 1);
refreshDisplay();
new AlertDialog.Builder(this).setTitle(creature.getName())
.setMessage(creature.getTrack().toString()).show();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creature_detail);
creature = new Creature("Example");
refreshDisplay();
}
public void refreshDisplay()
{
final View creatureDetailView = this.getLayoutInflater().inflate(
R.layout.activity_creature_detail, null);
final EditText nameField = (EditText) (creatureDetailView
.findViewById(R.id.textbox_creature_name));
nameField.setText(creature.getName());
final ImageView damageBox0 = (ImageView) (creatureDetailView.findViewById(R.id.damageBox0));
damageBox0.setImageResource(R.drawable.__n);
// in the full program this does the same for 0 through 9, but this is a sample
// also, in the full program, this is a dynamic lookup for the correct pic
// but again, this is just a sample version.
}
}
Now the problem is that the app will load up and start, but then none of the widgets will update properly. You can click the button, and it'll show the AlertDialog, and the text of the AlertDialog will change, but the textfield in the activity won't be changed, and the ImageView doesn't change at any point from what it starts as to the one it's supposed to change to.
So I'm very stumped. I can post more about the project's setup if I'm leaving out something important, but I'm not even sure what the problem going on is so I'm not sure what else to include in my question.
final View creatureDetailView = this.getLayoutInflater().inflate(
R.layout.activity_creature_detail, null);
Inflates your Activity's layout into basically nothing, just returning the View it inflated. setContentView is what actually inflates your layout into the Activity's View hierarchy.
Once you inflate your layout you don't need to do it again. Just use findViewById without the reference to a dangling unattached View.
Change your refreshDisplay method to this:
public void refreshDisplay()
{
final EditText nameField = (EditText) findViewById(R.id.textbox_creature_name);
nameField.setText(creature.getName());
final ImageView damageBox0 = (ImageView) findViewById(R.id.damageBox0);
damageBox0.setImageResource(R.drawable.__n);
// in the full program this does the same for 0 through 9, but this is a sample
// also, in the full program, this is a dynamic lookup for the correct pic
// but again, this is just a sample version.
}
Nothing changes because You do it completely wrong.
If You wish to update any view element of current activity You do it like this
View v = findViewById(R.id.element);
v.setText("text");
this is just simple example.
You would need to cast a returned element to correct type like to be able to access all available methods.
What You do wrong is trying to inflate a layout again.

Change background image on user's choice

Guys I want the user to change the background image of all the activities in my app on user selection.
I am able to change the background image of the Activity from where am changing the image
but if I try to change the image of other Activity, I get a NullPointerException!
Yes, I have checked that the id of other activity's Layout !
this is the code.
public class setting extends Activity {
TextView tv;
CheckBox cbS, theme1, theme2;
RelativeLayout rel;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
cbS = (CheckBox) findViewById(R.id.cb);
theme1 = (CheckBox) findViewById(R.id.theme1);
theme2 = (CheckBox) findViewById(R.id.theme2);
// cbW=(CheckBox)findViewById(R.id.cbWordPress);
checkBoxListener = new OnClickListener() {
public void onClick(View v) {
if (cbS.isChecked()) {
// anything
}
if (theme2.isChecked()) {
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rel);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.back_image1);
rel.setBackgroundDrawable(drawable);
// findViewById(R.id.rel).setBackgroundResource(R.drawable.back_image1);
}
}
};
cbS.setOnClickListener(checkBoxListener);
theme2.setOnClickListener(checkBoxListener);
// cbW.setOnClickListener(checkBoxListener);
}
}
you cannot access the UI components which are not yet instantiated. Use Intents to pass information across activities (user's choice, or some custom flags or Strings) and use this "extra" information in the launched activity to change the background accordingly.
Read more about intents in the documentation for better understanding and examples.
You cannot do this.. When you refer a layout file using findViewById(), the android system looks for this in your current ContentView only. (i.e the view which you have set using setContentView() for the current activity). So obvioulsy the sytem will not be able find the resource you are specifying and hence you will get the NullPointerExeption only.
You have to maintain the reference to the backgrounds separately and use it in your other Activity when you actually pass on there.

Categories

Resources