Show data from string.xml in a list view [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this questionI am a little new to android and was working over a lab assignment
https://sites.google.com/site/androidcoursearchive/labs/lab-2-1
well I tried a lot but found that, to create a list view
in activity we have to extend listactivity
. but in this assignment, I cant extend listActivity. kindly see the link and tell me the code how to implement this assignment.
check this tutorial link
http://www.vogella.de/articles/AndroidListView/article.html
here is the link for list view which extends the Activity class
http://android-codes-examples.blogspot.com/2011/03/multiple-listview-and-custom-listview.html
http://appfulcrum.com/?p=311
http://ykyuen.wordpress.com/2010/01/03/android-simple-listview-using-simpleadapter/
Friends i got the answer to my ques for a start we can proceed this way here is my xml-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/addjoke"
android:text="@string/addJoke"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
></Button>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="@string/enterJokesHere"
android:id="@+id/jokebox"
android:inputType="textMultiLine"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/addjoke">
</EditText>
<ListView
android:layout_width="fill_parent"
android:layout_below="@id/jokebox"
android:id="@+id/jokelist"
android:layout_height="fill_parent">
</ListView>
</RelativeLayout>
and this is my string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AssignmentsActivity!</string>
<string name="app_name">Assignments</string>
<string name="addJoke">Add Jokes</string>
<string name="enterJokesHere">Enter the jokes here</string>
<string name="plus">+</string>
<string-array name="jokeListStr">
<item>Cartoonist found dead in home. Details are sketchy.</item>
<item>I wondered why the baseball was getting bigger. Then it hit me.</item>
<item>A small boy swallowed some coins and was taken to a hospital.
When his grandmother telephoned to ask how he was a nurse said,
No change yet.</item>
</string-array>
and here is my java code:-
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;import android.widget.EditText;
import android.widget.ListView;
public class SimpleJokeList extends Activity {
ListView lv;
EditText jokeBox;
Button addJoke;
MyAdapter adapter;
private ArrayAdapter<String> mAdapter;
private ArrayList<String> mStrings = new ArrayList<String>();
String jokesToBeAdded;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simplejokeui);
Resources res = getResources();
String[] jok = res.getStringArray(R.array.jokeListStr);
lv=(ListView)findViewById(R.id.jokelist);
addJoke=(Button)findViewById(R.id.addjoke);//these are also in my XML but
//not shown here in XML
jokeBox=(EditText)findViewById(R.id.jokebox);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
for(int i=0;i<jok.length;i++){
lv.setAdapter(mAdapter);
mAdapter.add(jok[i]);
}
addJoke.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
jokesToBeAdded=jokeBox.getText().toString();
lv.setAdapter(mAdapter);
mAdapter.add(jokesToBeAdded);
jokeBox.setText(null);
}
});
}
enter code here
精彩评论