I'm trying to get a ListView to show the contents of a simple array but my program crashes at the setAdapeter line. As I understand this is supposed to be because the final value comes out as null but I don't see why that would be the case or how to fix it. I have already tried using ArrayAdapeter instead as well as catching the null exception with a try block. The former changes nothing and the latter simply shows a blank activity with no list to speak of. Please help.
Here's Main Activity
package ballton.fowdeckbuilder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDecks = (Button) (findViewById(R.id.btnDecks));
Button btnCards = (Button) (findViewById(R.id.btnCards));
btnDecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DeckCatalogue.class));
}
});
btnCards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CardCatalogue.class));
}
});
}
}
Here's the activity with the Problem
package ballton.fowdeckbuilder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class DeckCatalogue extends AppCompatActivity {
ListView Ldecks;
String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_catalogue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Ldecks = (ListView) (findViewById(R.id.DeckList));
String Decks[] = new String[] {"Faria","Melgis"};
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, Decks));
Ldecks.setAdapter(feed);
}
}
Here's the Layout for that activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ballton.fowdeckbuilder.DeckCatalogue"
tools:showIn="#layout/activity_deck_catalogue">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DeckList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And Here's the layout XML I use for the list times
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/Deck" />
</LinearLayout>
Use this code instead of yours:
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, R.id.Deck, Decks));
This happened because you must supply the resource ID (as your xml: R.id.Deck) for the TextView you used for ListView.
Related
I am creating a ListView in which contents of an ArrayList(Declared Globally) is to be displayed. I am using a custom layout here. Everything looks good but the ListView is just not showing on the output screen. While the same coding used on some other project is giving the desired output. Please tell me if there is any problem with the code or some other thing.
Globalvar.java //where the ArrayList is declared
package com.example.tale;
import java.util.ArrayList;
import java.util.List;
public class Globalvar
{
public static int value=0;
public static List<String> list = new ArrayList<String>();
}
activity_cart2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".cart2">
<ListView
android:id="#+id/lvl"
android:layout_width="match_parent"
android:layout_height="404dp">
</ListView>
cart2.java
package com.example.tale;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class cart2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart2);
Globalvar.list.add("Example 1");
ListView listView = (ListView)findViewById(R.id.lvl);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.forcart2,Globalvar.list);
listView.setAdapter(adapter);
}
}
forcart2.xml //custom layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp">
</TextView>
I am having a similar issue in which I have my files as follow in which I want to be able to view all child items and add new child items.
I understand that you will have to modify the code in the Java file, but I am having trouble understanding how to manipulate my own code and I tried to follow the confusing documentation provided by Firebase.
Below is the structure of my database followed by my code. Any help would be greatly appreciated.
The only problem I have now is how to properly populate the listview with the values of the following structure of my database.
It is as follows: Messages --> Message --> value of which has concatenated string
I hope this isn't confusing
Here is my MainActivity.java
package com.example.sean.messengerappsean;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
import com.firebase.client.collection.ArraySortedMap;
import com.firebase.client.realtime.util.StringListReader;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//TextView mTextFieldCondition;
EditText editUsername, editValue;
ArrayAdapter arrayAdapter;
Button btnSend;
Firebase mRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
//mTextFieldCondition = (TextView) findViewById(R.id.textViewCondition);
editUsername = (EditText) findViewById(R.id.editUsername);
editValue = (EditText) findViewById(R.id.editValue);
btnSend = (Button) findViewById(R.id.btnSend);
mRef = new Firebase("https://androidmessagetest.firebaseio.com/"); //CAN USE THE CODE HERE TO PUBLISH TO SPECIFIC LOCATION OR DB
//TRYING TO SET LISTVIEW
final ListView listView = (ListView) findViewById(R.id.listview);
final ArrayList<String> msgsArray = new ArrayList<String>(); //USE THIS FOR POPULATING THE VIEW
final ValueEventListener valueEventListener = mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//----------CONDITION FOR LISTING DATA----------------
//MIGHT NEED TO PUT VALUES INTO ARRAY AND READ THEM OUT IN ORDER OF PUBLISH
//String text = dataSnapshot.child("Message").child("Enter Username3").getValue(String.class);
//mTextFieldCondition.setText(text);
//POPULATE LISTVIEW
for (DataSnapshot Datasnapshot : dataSnapshot.getChildren()) {
msgsArray.add(Datasnapshot.child("Messages").child("Message").getKey());
}
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, msgsArray);
listView.setAdapter(arrayAdapter);
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//NEED TO DYNAMICALLY CHANGE VALUE TO USERNAME: MESSAGE
String textoutput = String.valueOf(editUsername.getText());
String MessageText = String.valueOf(editValue.getText());
//FIXED MESSAGE ADD CHILD AND HOW TO ADD VALUE TO MESSAGE
//TRY TO USE BELOW CODE FOR MESSAGES LATER ON
mRef.child("Messages").child("Message").push().setValue(textoutput + ": " + MessageText);
}
});
//ADD FUNCTION TO LOOK FOR DATA
}
}
Here is my XML file activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.sean.messengerappsean.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--<TextView
android:id="#+id/textViewCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/mTextFieldCondition"
android:layout_centerHorizontal="true"
android:layout_marginBottom="89dp"
android:text="Condition" />-->
//ENTER IN CODE BELOW FOR VIEWING OUTPUT OF DATA
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Send" />
<EditText
android:id="#+id/editValue"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/btnSend"
android:layout_toLeftOf="#+id/btnSend"
android:layout_toStartOf="#+id/btnSend"
android:backgroundTint="#color/colorPrimaryDark"
android:inputType="textMultiLine"
android:text="Enter Message Here" />
<EditText
android:id="#+id/editUsername"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/btnSend"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:background="#color/colorAccent"
android:inputType="textCapWords"
android:text="Enter Username" />
</RelativeLayout>
These imports are for classes in the legacy SDK:
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.Query;
import com.firebase.client.ValueEventListener;
These are for the new SDK:
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
You cannot mix the two SDKs. You should use the new SDK only. Remove this statement from your build.gradle dependencies:
compile 'com.firebase:firebase-client-android:2.x.x'
and make the approriate code changes to use the new SDK. Documentation and examples are here. There is also an Upgrade Guide.
Please help me figure out why my string isn't passing between my activities in android studio. I ran this script on my phone but the string I'm passing from my main activity is passing to my secondary activity. I'm going through an android teach yourself book and I believe the issue is in my XML.
Here's my XML code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.owner.hour3application.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
activity_secondary.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.example.owner.hour2application.SecondaryActivity">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.design.widget.CoordinatorLayout>
Main Activity
package com.example.owner.hour2application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button activityButton = (Button) findViewById(R.id.Button);
if (activityButton != null) {
activityButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent startIntent = new Intent(MainActivity.this, SecondaryActivity.class);
startIntent.putExtra("com.example.owner.MESSAGE", "Hello SecondaryActivity");
startActivity(startIntent);
}
});
}
}
}
Secondary Activity
package com.example.owner.hour2application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class SecondaryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondary);
Intent intent = getIntent();
String message = intent.getStringExtra("com.example.owner.MESSAGE");
TextView messageTextView = (TextView) findViewById(R.id.message);
if (messageTextView != null) {
messageTextView.setText(message);
}
}
}
Thanks guys sorry I posted the wrong main activity. The issue was in string.xml the string name was wrong. Thanks!
JAVA:
package com.example.scott.testapplication;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HomeScreen extends Activity {
RelativeLayout Backround;
Button InitButton;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Backround = (RelativeLayout) findViewById(R.id.Backround);
InitButton = (Button) findViewById(R.id.InitButton);
image = (ImageView) findViewById(R.id.image);
InitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*insert code here*/
}
});
}
}
In order to insert the image, i'm thinking that I need to use a command like ImageView image = new (image), and then use another command to draw it on the display. I dont know which commands I should use.
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Backround"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ImageView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:scaleType="fitXY"
android:src="#drawable/DESERT2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Initiate"
android:id="#+id/InitButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical" />
</RelativeLayout>
I would like to create the image on the event of the click of a button, I also created the ImageView in the xml file. Not sure if I should change something there.
you can use
image.setImageResource(R.drawable.yourDesiredImageName);
When I launch the application and click the button to go to the Activity, I suddenly get a Force Close. I've tried to erase it all and write it again, but it is still there. What could be the problem?
package com.alexgascon.formuladora;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Matematicas extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
BtnMCD.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent MCDintent = new Intent(Matematicas.this,Maximocomun.class);
startActivity(MCDintent);
}
});
}
}
And here is the layout code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pizarramatesverde"
android:gravity="center" >
<Button
android:id="#+id/ecsegundogrado"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/ecsegundo"
android:textColor="#color/Negro"
/>
<Button
android:id="#+id/fracciones"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/fracciones"
android:layout_below="#id/ecsegundogrado"
android:textColor="#color/Negro"
/>
<Button
android:id="#+id/maximocomunBoton"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/maximocomun"
android:layout_below="#id/fracciones"
android:layout_marginBottom="80sp"
android:textColor="#color/Negro"
/>
</RelativeLayout>
You need to call setContentView before findViewById:
setContentView(R.layout.yourlayoutxmlname);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
You need to call setContentView(R.layout.yourxmlLayout); to have the layout being referenced on your activity class.
Try to pop a toast from the click listener and see if it works. If it does - then the problem is in the activity that you want to launch.
You need to add the setContentView(R.layout.yourlayoutxmlname);.
Try the following code:
package com.alexgascon.formuladora;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Matematicas extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayoutxmlname);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
BtnMCD.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent MCDintent = new Intent(Matematicas.this,Maximocomun.class);
startActivity(MCDintent);
}
});
}
}