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.
Related
Through out the whole android application I want to capture button click, radio button clicks, link click etc... basically a user interaction in whole android application. Is there any common method to detect which element user click and its values.?
Try using onCickListener() on the buttons.
In Kotlin:
Add id to button in .xml files with android:id="#+id/nameOfButton". Every button needs an unique name.
In .kt file, use setOnClickListener with the id to set up the action when user click the button.
If there are several buttons, just follow step 1 and 2.
Example:
step 1
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
step 2
buttonSave.setOnclickListenter {
//TODO: your code goes here
}
Its very simple you just need to get the text and id of button from the onclick method. Here is java and xml code for it:
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:onclick="getid"
android:text="Save" />
JAVA:
public void getid(View v){
int id = v.getId();
String text = v.getText();
}
As #Muthukumar Lenin asked here is listview in xml and java
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/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#5f65ff"
android:padding="5dp"
android:text="Choose is the best football player?"/>
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textview" />
</RelativeLayout>
JAVA:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final TextView textView = (TextView) findViewById(R.id.textview);
String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
textView.setText("The best football player is : " + selectedItem);
}
});
}
}
Some of these code are from tutorialspoint and some are edited by me.
I want to create a chatapp. But in the recyclerview i only can receive the messages from the usermsg databasereference. I dont know how I should code it that i shows 2 recyclerviews. Does someone got an idea? Thanks
Chat Activity
package highelo.drivetogether;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Chat extends AppCompatActivity {
RecyclerView mFriendMsg;
RecyclerView mUserMsg;
private Button SendMsg;
private EditText EditMsg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
final Intent s = getIntent();
final String userID = s.getStringExtra("UID");
final Intent i = getIntent();
final String post_UserID = i.getStringExtra("FID");
SendMsg = (Button) findViewById(R.id.send_msg);
EditMsg = (EditText) findViewById(R.id.edit_msg);
mUserMsg = (RecyclerView) findViewById(R.id.chat_conr);
mUserMsg.setHasFixedSize(true);
mUserMsg.setLayoutManager(new LinearLayoutManager(this));
mFriendMsg = (RecyclerView) findViewById(R.id.chat_conr);
mFriendMsg.setHasFixedSize(true);
mFriendMsg.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
final Intent s = getIntent();
final String userID = s.getStringExtra("UID");
final Intent i = getIntent();
final String post_UserID = i.getStringExtra("FID");
DatabaseReference FriendMsg = FirebaseDatabase.getInstance().getReference().child("Users").child(userID).child("Chats").child(post_UserID).child("Chat");
DatabaseReference UserMsg = FirebaseDatabase.getInstance().getReference().child("Users").child(post_UserID).child("Chats").child(userID).child("Chat");
final FirebaseRecyclerAdapter<Blog, BlogViewHolder2> firebaseRecyclerAdapter2 = new FirebaseRecyclerAdapter<Blog, BlogViewHolder2>(
Blog.class,
R.layout.inchat_row2,
BlogViewHolder2.class,
UserMsg) {
#Override
protected void populateViewHolder(BlogViewHolder2 viewHolder, Blog model, int position) {
viewHolder.setNachricht(model.getNachricht());
viewHolder.setImage(Chat.this, model.getFImage());
}
};
mUserMsg.setAdapter(firebaseRecyclerAdapter2);
final FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.inchat_row,
BlogViewHolder.class,
FriendMsg) {
#Override
public void populateViewHolder(final BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setNachricht(model.getNachricht());
viewHolder.setImage(Chat.this, model.getFImage());
}
};
mFriendMsg.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder2 extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder2(View itemView) {
super(itemView);
mView = itemView;
}
public void setNachricht(String Nachricht){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.finchat_mess);
post_Nachricht.setText(Nachricht);
}
public void setImage(Context ctx, String FImage){
ImageView post_image = (ImageView)mView.findViewById(R.id.finchat_image);
Picasso.with(ctx).load(FImage).fit().centerCrop().into(post_image);
}
}
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setNachricht(String Nachricht){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.uinchat_mess);
post_Nachricht.setText(Nachricht);
}
public void setImage(Context ctx, String FImage){
ImageView post_image = (ImageView)mView.findViewById(R.id.uinchat_image);
Picasso.with(ctx).load(FImage).fit().centerCrop().into(post_image);
}
}
}
chat_activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="492dp"
android:id="#+id/chat_conr"
android:layout_weight="1.11"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/send_msg"
android:layout_alignEnd="#+id/send_msg"
android:layout_above="#+id/edit_msg" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edit_msg"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/send_msg"
android:layout_toLeftOf="#+id/send_msg" />
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Senden"
android:id="#+id/send_msg"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Inchat_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="54dp"
android:layout_height="54dp"
android:id="#+id/uinchat_image"
app:civ_border_width="1dp"
app:civ_border_color="#ff0000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/uinchat_mess"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingLeft="5dp"
android:textSize="13dp"
android:paddingBottom="4dp"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/uinchat_image"
android:layout_alignBottom="#+id/uinchat_image" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Inchat2_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="54dp"
android:layout_height="54dp"
android:id="#+id/finchat_image"
app:civ_border_width="1dp"
app:civ_border_color="#ff0000"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/finchat_mess"
android:layout_alignParentEnd="false"
android:paddingLeft="5dp"
android:textSize="13dp"
android:paddingBottom="4dp"
android:singleLine="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/finchat_image"
android:layout_alignBottom="#+id/finchat_image" />
</RelativeLayout>
</android.support.v7.widget.CardView>
The usual way to have a chat between two people is to model that chat room in your database:
ChatRooms
ChatRoomBetweenUser1AndUser2
-KLM.......24
name: "User One"
uid: "uidOfUser1"
text: "Hello there. This is your friend User One"
-KMN.......35
name: "User Two"
uid: "uidOfUser2"
text: "Hello User One, this is User Two. Over"
That way you only need to load data from one location and need only one RecyclerView.
For more information on how to efficiently model chat rooms names for this scenario, see Best way to manage Chat channels in Firebase.
To get a good start on building a chat app, follow the Firebase Codelab for Android.
For a good introduction into modeling your data as your app needs it, see this article on NoSQL data modeling.
I'm trying to dynamically set the values in a list. Originally this was taking place in the onCreate event itself. Thinking that perhaps the ListView object hadn't been initialized yet I moved this to a button click event. This simple example blows up nicely every time I tap the button with the "Unfortunately, ListViewTest has stopped" and no exception stack trace to be found. Also worth noting is that the real target app does not blow up until that ListView becomes visible when the container tab is tapped.
I am using a fully updated Android Studio 2.0 with the Nexus (5X API 23) emulator on El Capitan 10.11.4.
Any tips will be appreciated.
MainActivity.java
package com.greencountrytechnical.listviewtest;
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.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onYouTappedMe();
}
}
);
}
private void onYouTappedMe()
{
String[] noob={"why","does","this","not","work","?"};
ListView lv=(ListView) findViewById(R.id.listView);
if(lv != null) {
List<String> lvs = new ArrayList<String>(Arrays.asList(noob));
// lvs is not null and it is filled with the values from the array.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_main, lvs);
if(adapter != null)
{
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
}
Layout:
<?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.greencountrytechnical.listviewtest.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
This is where your problem probably lies;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_main, lvs);
You can fix this by either creating your own list_item_layout or use what android gives you by default:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvs);
I hope this helps!
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.
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);