Activity of android - android

I am working with the xml file ,through which i am accessing a string through a variable passkey.later i am assigning it to strPassword. i am getting error for setListAdapter.Here is my code..
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="passkey">
<item>1234</item>
</string-array>
</resources>
and
public class AdminLogin extends Activity
{
public void onCreate(Bundle bdlActivity)
{
String[] strPassword = getResources().getStringArray(R.array.passkey);
setListAdapter(new ArrayAdapter<String>(this,R.layout.adminlogin,
R.id.Editpassword, strPassword))
//button events
}

You need to extend your class with ListActivty. You cant instantiate ListView in simple activity.

You can do it from a simple Activity too:
say you have a main layout, like:
mylayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="#+id/btn_ok" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="OK" android:layout_alignParentBottom="true" />
<ListView android:id="#+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_above="#id/btn_ok" />
</RelativeLayout>
This would be the layout of your activity, with a list view inside of it (#id/listview)
Your Activity's onCreate method should look like this:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
final ListView listView = (ListView) findViewById(R.id.listview);
listView.setListAdapter(new ArrayAdapter<String>(this, R.layout.adminlogin,
R.id.Editpassword, strPassword))
}

Related

android: get items from string array and show it one by one in text view

as i write in title i need some help here for get items from string array and show it one by one in text view i have code that got them all in list view but i need show them on text view one by one each time random here my code and sorry about my bad english
,thanks for help anyway ...
public class MainActivity extends ListActivity {
String[] mTestArray;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;
mTestArray = getResources().getStringArray(R.array.planets_array);
/* Assign the name array to that adapter and
also choose a simple layout for the list items */
adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
mTestArray);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
XML file:
<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#array/planets_array"
/>
</RelativeLayout>
and string array file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
Ok, with comments I understood what you need and edited my answer for that. You want to show values of your array in a textView randomly.
Use this activity:
public class MainActivity extends Activity {
String[] mTestArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
mTestArray = getResources().getStringArray(R.array.planets_array);
}
#Override
protected void onResume() {
super.onResume();
updateTextView();
}
private void updateTextView() {
TextView textView = (TextView)findViewById(R.id.randomTextView);
Random random = new Random();
int maxIndex = mTestArray.length;
int generatedIndex = random.nextInt(maxIndex);
textView.setText(mTestArray[generatedIndex]);
}
}
Put this layout under res/layout folder and name it sample.xml.
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/randomTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>

Android ListView does not work

I am trying to make a listview in android but i got some troubles.
This is the code:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] items = {"red", "blue", "green"};
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items));
}
}
And this the xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Agregar Tarea"
android:id="#+id/btn" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/list" />
</RelativeLayout>
The logcat says that there is a NullPointerException in line 21, this line:
ListView listView = (ListView) findViewById(R.id.list);
But I dont know why :(
I want to do the list view this way becouse if I use listactivity the header/footers is not fixed.
Add listview id in the xml layout.
Because of id null pointer exception is thrown.
android:id="#+id/list"
instead of
android:layout_below="#+id/list"
and also change above line like this
android:layout_below="#+id/btn"
Try change this
android:layout_below="#+id/list"
to this:
android:id="#+id/list"
Your listview id doesn't exists
android:id="#+id/list"
android:layout_below="#+id/btn"
You have not added property
android:id=#"+id/list"

View is null even after initialization

I have a listview in my code and I want to set adapter on it.
But the issue is, even after initializing the listview, it is still null resulting into nullPointerException. (I checked it by logging and debugging)
I'm not able to access any view from that xml.
What am I missing? Any help appreciated.
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFF"
android:dividerHeight="2dp" />
<ListView
android:id="#+id/lvTomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lvToday"
android:divider="#FFF"
android:dividerHeight="2dp" />
</RelativeLayout>
</ScrollView>
Activity file
public class DashboardActivity extends Activity {
ListView lvToday, lvTomorrow;
TextView lblCall;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
init();
}
private void init() {
lvToday = (ListView) findViewById(R.id.lvToday);
lvTomorrow = (ListView) findViewById(R.id.lvTomorrow);
TodayAppAdapter adapter = new TodayAppAdapter(DashboardActivity.this,
DashboardActivity.this);
// This line is giving NULL
lvToday.setAdapter(adapter);
TomorrowAppAdapter adapter1 = new TomorrowAppAdapter(
DashboardActivity.this, DashboardActivity.this);
// This line is giving NULL
lvTomorrow.setAdapter(adapter1);
}
}
1) test eclipse menu: Project -> Clean...
2) if you have more than one version for your xml layout (example layout-large, layout-xlarge,...), check if all of them have your view.
You are missing a TextView in your layout
Put a String []
along with name of your Adapter in your
OnDraw()
Method

Android Naming Layout Scheme

I have a piece of code
public class Radio_groupActivity extends ListActivity {
TextView selection;
String[] items={"One", "Two", "Three"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1));
}
}
Now I have the main.xml in the folder but I don't have any xml file called simple_list_item_1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/selection"
/>
<ListView android:layout_height="fill_parent"
android:id="#+id/list"
android:layout_width="fill_parent"></ListView>
</LinearLayout>
So, what's the relationship of the listview with #id/list and that android.R.layout.simple_list_item_1?
The android. package qualifier in android.R.layout.simple_list_item_1 means you're referring to an Android framework resource rather than a resource from your app. You may not have a layout by that name, but the Android framework does.
ListActivity looks for a ListView with the id #android:id/list when using setListAdapter.
ArrayAdapter(Context context, int textViewResourceId)
android.R.layout.simple_list_item_1 is system built-in text view for list item displaying

how to change the string values of sting.xml file

i hava a layout file like
test.xml
<?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">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/test_string" />
</RelativeLayout>
and java file like this.
test1.java
public class test1 extends Activity {
String temp="This is String"; //which change dynamically
TextView tempview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tempview=(TextView)findViewById(R.id.test_string);
tempview.setText(temp);
setContentView(R.layout.test);
}
}
what i am trying to do is show the value of temp in activity as above code. but it throws an NullPointerException. so how to do. how to set the values of sting.xml???
thanks in advance..
setContentView(R.layout.test); should come before (TextView)findViewById(R.id.test_string);

Categories

Resources