I'm creating an activity which will consist of a listview. Each element of the listview will contain one image and one text. When I'm running the app for example with just 10 elements it works just fine. But when I have 90 elements, my app stops working. Have you got any solutions to this problem?
PS. My app will not use internet.
My activity 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"
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=".Testlistview" >
<ListView
android:id="#+id/list66"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:fastScrollEnabled="true"
android:persistentDrawingCache="scrolling"
android:scrollingCache="false">
</ListView>
</RelativeLayout>
My adapter:
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class customadaptertest extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public customadaptertest(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.listtestitem, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.listtestitem, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
My activity:
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class Testlistview extends AppCompatActivity {
ListView list66;
String[] web = {
"some text"
} ;
Integer[] imageId = {
R.drawable.some_item_image
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testlistview);
customadaptertest adapter = new
customadaptertest(Testlistview.this, web, imageId);
list66=(ListView)findViewById(R.id.list66);
list66.setAdapter(adapter);
}
}
My item:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
You should use ViewHolder pattern.
Take a look at this : Making ListView Scrolling Smooth.
I am using GridView first time. Everything is fine but the image height is less. I searched a lot but couldn't get the method of increasing the height of image through XML. Here is my code :
activity_select_images.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbarThumbVertical="#drawable/custom_scroll_style"
android:fillViewport="false">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectImagesActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_marginTop="8dp"
android:text="Please Select the pictures you like"
android:id="#+id/selectTextview"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_below="#+id/selectTextview"
android:layout_height="match_parent">
<GridView
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
<Button
android:id="#+id/submit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="18dp"
android:textAllCaps="false"
android:textColor="#ffffff"
android:layout_below="#+id/frameLayout"
android:background="#drawable/my_button"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</RelativeLayout>
</ScrollView>
gridview_layout.xml
<?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">
<ImageView android:id="#+id/thumbImage"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbImage"
android:layout_alignLeft="#+id/thumbImage"
android:layout_alignStart="#+id/thumbImage"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp"
android:layout_marginBottom="61dp" />
</RelativeLayout>
SelectImagesActivity.java
package com.houssup.userapp;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class SelectImagesActivity extends AppCompatActivity {
private int count = 0;
Button btn;
private Drawable marker, marker1;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_images);
GridView imagegrid = (GridView) findViewById(R.id.gridView);
btn = (Button) findViewById(R.id.submit);
marker = getResources().getDrawable(R.drawable.pic_five);
marker1 = getResources().getDrawable(R.drawable.pic_two);
imagegrid.setAdapter(new ImageAdapter(this, marker, marker1));
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (count < 5)
Toast.makeText(SelectImagesActivity.this, "Please " +
"select total 5 images", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private Drawable drawable1, drawable2;
private LayoutInflater mInflater;
int positionID[];
public ImageAdapter(Context context, Drawable drawable1,
Drawable drawable2) {
this.context = context;
this.drawable1 = drawable1;
this.drawable2 = drawable2;
}
public int getCount() {
return 6;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml ( Defined Below )
gridView = inflater.inflate(R.layout.gridview_layout, null);
ImageView imageView = (ImageView) gridView.findViewById(R.id.thumbImage);
CheckBox checkBox = (CheckBox) gridView.findViewById(R.id.itemCheckBox);
if (position < 5) {
imageView.setImageDrawable(marker);
} else {
imageView.setImageDrawable(marker1);
}
// set image based on selected text
} else {
gridView = (View) convertView;
}
return gridView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:padding="10dp"
android:src="#drawable/arteta"/>
<TextView
android:id="#+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="27dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#id/imageView1"
android:text="name"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:padding="10dp"
android:id="#+id/imageView1"
android:src="#drawable/arteta"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="27dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imageView1"
android:text="Name"
android:id="#+id/nameTv" />
</RelativeLayout>
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:id="#+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="setContentView" />
</RelativeLayout>
I have made a ListView with a DialogFragment and I am trying to display this in full screen.
However, when I run it on a device, there is always a border around the edges of the ListView, like a box within a box and cant seem to set it to fill the screen!
I have tried many routes to make this ListView cover the entire screen to no avail.
I would really like and appreciate some help with this.
Layout
<?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:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView1"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
></ListView>
</RelativeLayout>
MainActivity
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.FragmentManager;
public class MainActivity extends FragmentActivity {
Button showBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
final PlayersFragment p = new PlayersFragment();
showBtn = (Button) findViewById(R.id.button1);
showBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
p.show(fm, "Players Fragment");
}
});
}
}
PlayersFragment
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class PlayersFragment extends android.support.v4.app.DialogFragment {
ListView lv;
String[] players = {"micheal arteta", "deago costa", "andy reid", "scum degea", "scum rooney", "john terry"};
int[] images = {R.drawable.arteta, R.drawable.costa, R.drawable.reid, R.drawable.degea,
R.drawable.rooney, R.drawable.terry};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog, container, false);
//initialize listview
lv = (ListView) rootView.findViewById(R.id.listView1);
//set dialog title
getDialog().setTitle("Soccer SuperStars");
//create adapter obj and set list view to it
Adapter adapter = new Adapter(getActivity(), players);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class Adapter extends ArrayAdapter<String>{
Context c;
String[] players;
int[] images;
LayoutInflater inflater;
public Adapter(Context context, String[] players) {
super(context, R.layout.rowmodel,players);
this.c=context;
this.players = players;
this.images = images;
}
#Override
public View getView(int position,View convertView, ViewGroup parent){
if(convertView==null)
{
inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.rowmodel,null);
}
TextView nameTv = (TextView) convertView.findViewById(R.id.nameTv);
ImageView img = (ImageView) convertView.findViewById(R.id.imageView1);
nameTv.setText(players[position]);
img.setImageResource(images[position]);
return convertView;
}
}
I think that you should use a FrameLayout in your main_activity layout. Something like this:
<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">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:id="#+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="setContentView" />
</FrameLayout>
</RelativeLayout>
Then, in your MainActivity, try this:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
Button showBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
final PlayersFragment p = new PlayersFragment();
showBtn = (Button) findViewById(R.id.button1);
showBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
showBtn.setVisibility(View.GONE);
transaction.replace(R.id.fragment_container, p);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
#Override
public void onBackPressed() {
if (showBtn != null) {
showBtn.setVisibility(View.VISIBLE);
}
super.onBackPressed();
}
}
Your adapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class Adapter extends ArrayAdapter<String>{
Context c;
String[] players;
int[] images;
LayoutInflater inflater;
public Adapter(Context context, String[] players, int[] images) {
super(context, R.layout.simplerow,players);
this.c=context;
this.players = players;
this.images = images;
}
#Override
public View getView(int position,View convertView, ViewGroup parent){
if(convertView==null)
{
inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.simplerow,null);
}
TextView nameTv = (TextView) convertView.findViewById(R.id.nameTv);
ImageView img = (ImageView) convertView.findViewById(R.id.imageView1);
nameTv.setText(players[position]);
img.setImageResource(images[position]);
return convertView;
}
}
Your dialog layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Soccer SuperStars"
android:layout_gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#android:color/black"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView1"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
></ListView>
</LinearLayout>
And your PlayerFragment:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class PlayersFragment extends android.support.v4.app.DialogFragment {
ListView lv;
String[] players = {"micheal arteta", "deago costa", "andy reid", "scum degea", "scum rooney", "john terry"};
int[] images = {R.drawable.arteta, R.drawable.costa, R.drawable.reid, R.drawable.degea,
R.drawable.rooney, R.drawable.terry};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog, container, false);
//initialize listview
lv = (ListView) rootView.findViewById(R.id.listView1);
//create adapter obj and set list view to it
Adapter adapter = new Adapter(getActivity(), players, images);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
I removed some come (paddings) from your main_activity layout and included some code show the "dialog" (that is now a fragment) full screen.
1) First method
public class PlayersFragment extends DialogFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_NoTitleBar_Fullscreen);
}
}
2) Second method
public class PlayersFragment extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myCustomStyle);
}
}
in values add style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="GalleryDialogStyle" parent="AppTheme">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
I have an EditText inside ListView and I want to enter some integer values in all the EditTexts. I have one Button outside the ListView onClicking. From this button I want to get the data from all the EditText from ListView and save in array to show in toast. Also my EditText id is same for all the EditText. Can any buddy give so sample code so I can proceed to next step. Thanks in advance.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listGejalaPilih"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp" >
</ListView>
<Button
android:id="#+id/btnDiagnosis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Diagnosis Penyakit" >
</Button>
</LinearLayout>
Item.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"
android:orientation="vertical" >
<TextView
android:id="#+id/textGejalaPilih"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/editPersenYakin"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="3"
android:layout_alignParentRight="true" >
</EditText>
</RelativeLayout>
Main.java
package id.app.pakarsawit;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class DetailDiagnosis extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] pilihan = {"pilihan 1","pilihan 2","pilihan 3", "pilihan 4"};
ListView listView = (ListView)findViewById(R.id.listGejalaPilih);
Button btn = (Button)findViewById(R.id.btnDiagnosis);
ArrayAdapter<String> adapterPilih = new LVAdapterDetailDiagnosis(this, pilihan);
listView.setAdapter(adapterPilih);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
}
LVAdapterDetailDiagnosis.java
package id.app.pakarsawit;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class LVAdapterDetailDiagnosis extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public LVAdapterDetailDiagnosis(Context context, String[] values) {
super(context, R.layout.item);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.textGejalaPilih);
textView.setText(values[position]);
return rowView;
}
}
try this:
for (int a = 0; a < listGejalaPilih.getCount(); a++) {
EditText et = (EditText) listGejalaPilih.getChildAt(a).findViewById(R.id.editPersenYakin);
arraylist.add(et.getText().toString());
}
Or if you want Array
String string[] = new String[listGejalaPilih.getCount()];
for (int a = 0; a < listGejalaPilih.getCount(); a++) {
EditText et = (EditText) listGejalaPilih.getChildAt(a).findViewById(R.id.editPersenYakin);
string[a] = et.getText().toString();
}
I am developing the program that used ListActivity. So, when i scroll the listview to down, it cause to restart application and start from first activity. My code is as follows :
package com.daarkoob.food;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Window;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class FoodListActivity extends ListActivity {
private ArrayList<Food> M_foods = new ArrayList<FoodListActivity.Food>();
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.foodlist);
SQLiteDatabase sqliteDatabase = SQLiteDatabase.openOrCreateDatabase(
"/data/data/" + this.getPackageName() + "/data.db", null);
Cursor cursor = sqliteDatabase.rawQuery("SELECT * FROM FOOD", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
M_foods.add(new Food(cursor.getString(2), cursor.getString(1)));
cursor.moveToNext();
}
FoodAdapter adapter = new FoodAdapter(this);
setListAdapter(adapter);
cursor.close();
sqliteDatabase.close();
}
public class Food {
public Food(String foodName, String foodType) {
this.M_foodName = foodName;
this.M_foodType = foodType;
}
String M_foodName;
String M_foodType;
}
public class FoodAdapter extends ArrayAdapter<Food> {
private Context M_context = null;
public FoodAdapter(Context context) {
super(context, R.layout.foodlist_view_item, M_foods);
this.M_context = context;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) M_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(
R.layout.foodlist_view_item, parent, false);
}
TextView textView = (TextView) convertView
.findViewById(R.id.foodlist_view_item_textview);
textView.setText(M_foods.get(position).M_foodName);
return convertView;
}
}
}
And the foodlist_view_item.xml is :
<?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/border"
android:orientation="vertical" >
<TextView
android:id="#+id/foodlist_view_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right|center_vertical"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/black"
android:textStyle="bold" />
</RelativeLayout>
and the foodlist.xml is as follows :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/foodlist_title_bar"
android:layout_width="match_parent"
android:layout_height="40dip"
android:background="#drawable/textlines"
android:gravity="right|center_vertical"
android:text="#string/foodlist_titlebar_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:dividerHeight="1dip" >
</ListView>
</LinearLayout>
What thing is wrong ? Why restarting occurs ?
Thanks in advance :)