I have a project in which i wanted to set my custom View, not simple Android GridView.
I did that, now i want to set RadioButton for each row.
I saw this
But i could not specify the Boolean Array in this Reply.
Here is my Code.
First Items Layout.
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_marginTop="30dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Small Text"
android:id="#+id/textView2"
android:layout_x="12dp"
android:layout_y="2dp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginLeft="20dp"
android:id="#+id/textView"
android:layout_x="157dp"
android:layout_y="0dp"
android:textColor="#000"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radiogroup"></RadioGroup>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButton"
android:layout_x="302dp"
android:layout_y="0dp"
android:checked="false" />
</AbsoluteLayout>
And simple GridView with a Button.
<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=".DisplayActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1"
android:layout_marginTop="30dp">
<GridView
android:layout_width="wrap_content"
android:layout_height="387dp"
android:id="#+id/gridView"
android:background="#abc"
android:layoutDirection="ltr"
android:choiceMode="singleChoice"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:id="#+id/button" />
</LinearLayout>
In my Adapter Class :
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
import android.widget.RadioButton;
public class CarAdapter extends ArrayAdapter<Car> {
RadioButton radioButton;
RadioGroup radioGroup;
ArrayList<Boolean> booleans = new ArrayList<Boolean>();
public CarAdapter(Context context, int resource, ArrayList<Car> carArrayList, ArrayList<Boolean> mlist) {
super(context, resource, carArrayList);
this.booleans = mlist;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Car car = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_car, parent, false);
}
RadioGroup radioGroup = (RadioGroup)convertView.findViewById(R.id.radiogroup);
radioButton = (RadioButton)convertView.findViewById(R.id.radioButton);
TextView carName = (TextView) convertView.findViewById(R.id.textView2);
TextView carSIM = (TextView) convertView.findViewById(R.id.textView);
carName.setText(car.getName());
carSIM.setText(car.getSIM());
return convertView;
}
RadioButton.OnCheckedChangeListener mListener = new RadioButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
booleans.set((Integer) buttonView.getTag(), isChecked); // get the tag so we know the row and store the status
}
};
}
I tried this in getView Method :
radioButton.setTag(Integer.valueOf(position));
radioButton.setChecked(booleans.get(position));
radioButton.setOnCheckedChangeListener(mListener);
And mListener is :
RadioButton.OnCheckedChangeListener mListener = new RadioButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
booleans.set((Integer) buttonView.getTag(), isChecked); // get the tag so we know the row and store the status
}
};
But it gives me
java.lang.IndexOutOfBoundsException at line `radioButton.setChecked(booleans.get(position));`
Here is Main Activity :
import android.app.Activity;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
public class DisplayActivity extends Activity {
CarAdapter carAdapter;
Car car;
Car car2;
ArrayList<Car> carArrayList;
GridView gridView;
Button test;
ArrayList<Boolean> booleans;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
carArrayList = new ArrayList<Car>();
car = new Car("Davie", "Smith");
car2 = new Car("Enrique", "Wall");
carArrayList.add(car);
carArrayList.add(car2);
booleans = new ArrayList<Boolean>(carArrayList.size());
carAdapter = new CarAdapter(getApplicationContext(), android.R.layout.simple_list_item_checked, carArrayList, booleans);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(carAdapter);
test = (Button) findViewById(R.id.button);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (gridView.isItemChecked(gridView.getCheckedItemPosition())) {
String name = (carAdapter.getItem(gridView.getCheckedItemPosition())).getName();
Toast.makeText(getApplicationContext(), "Here is " + name, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "This is not true", Toast.LENGTH_SHORT).show();
}
}
});
}
}
In your CarAdapter modify this...
public CarAdapter(Context context, int resource, ArrayList<Car> carArrayList, ArrayList<Boolean> mlist) {
super(context, resource, carArrayList);
mlist = this.booleans;
}
to
public CarAdapter(Context context, int resource, ArrayList<Car> carArrayList, ArrayList<Boolean> mlist) {
super(context, resource, carArrayList);
this.booleans= mlist ;
}
You are assigning booleans ArrayList to mlist ArrayList which can be empty and so you are getting IndexOutOfBoundsException..you have to assign mlist to booleans ArrayList
mlist = this.booleans;
should be..
this.booleans = mlist;
in your CarAdapter constructor.
Related
Friends, I am a new developer in the field of android and I want to develop an application like that it will sort the Gridview by using a spinner, I had applied the XML array for Spinner and Custom adapter also I tried various solution but it didn't work so please help me
Moodel Class for adapter of GridAdapter
package braniacs.com.gridupdaterapp;
/**
* Created by grimReaper on 12/4/2017.
*/
public class GridModel {
String thingName;
public String getThingName() {
return thingName;
}
public void setThingName(String thingName) {
this.thingName = thingName;
}
}
MainActvityCode along adapter in it!
package braniacs.com.gridupdaterapp;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
GridView mGridViewA;
Spinner mspinnerColony;
ArrayList<GridModel> ColonyArray;
String[] ColonyArraySpinnerWali = {"hie", "wasteage", "short", "Lovely", "RUSSIA", "ASIA", "KOREA"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridViewA = findViewById(R.id.mgridViewApp);
mspinnerColony = findViewById(R.id.spnr_colony);
ColonyArray = new ArrayList<>();
ArrayAdapter mArrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, ColonyArraySpinnerWali);
mspinnerColony.setAdapter(mArrayAdapter);
mspinnerColony.setSelection(0);
mspinnerColony.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (i != 0) {
GridModel mgRIDmODEL = new GridModel();
TextView mTitle = (TextView) view;
mgRIDmODEL.setThingName(mTitle.getText().toString());
ColonyArray.add(mgRIDmODEL);
GridVuCustionAdapter adapter = new GridVuCustionAdapter(MainActivity.this,
R.layout.custom_gridvu_design, ColonyArray);
mGridViewA.setAdapter(adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
mGridViewA.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// CLICK LISTENER AREA
}
});
}
public static class GridVuCustionAdapter extends ArrayAdapter<GridModel> {
LayoutInflater mLayoutInflater;
ArrayList<GridModel> objects;
Context context;
int resource;
public GridVuCustionAdapter(#NonNull Context context, int resource, #NonNull ArrayList<GridModel> objects) {
super(context, resource, objects);
mLayoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
this.objects = objects;
this.context = context;
this.resource = resource;
}
#Override
public int getCount() {
return objects.size();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
convertView = mLayoutInflater.inflate(resource, parent, false);
TextView txtVuTitle = convertView.findViewById(R.id.txt_vu_name);
ImageView imgVuCancel = convertView.findViewById(R.id.img_vu_cancel);
txtVuTitle.setText(objects.get(position).getThingName());
return convertView;
}
}
}
the xml file for custom design for the design is simple here
<LinearLayout 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="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/txt_vu_name"
android:layout_weight="1.7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
<ImageView
android:layout_weight="0.2"
android:id="#+id/img_vu_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_dialog" />
</LinearLayout>
the xml file is here for the activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="braniacs.com.gridupdaterapp.MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mgridViewApp"
android:layout_width="0dp"
android:layout_height="200dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Spinner
android:id="#+id/spnr_colony"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="45dp" />
</android.support.constraint.ConstraintLayout>
i want to display list of juice object.but here am getting only one object and displaying only one object.getcount() is calling 5 times.but it is not displaying.please help me.
i want to display list of juice object.but here am getting only one object and displaying only one object.getcount() is calling 5 times.but it is not displaying.please help me.
//FoodHome.java
package com.example.hotelmenucard;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FoodHome extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_home);
populateListView();
registerClickCallBack();
}
private void populateListView() {
String[] items={"All Day BreakFast","Salads","Sandwiches","Hot Drinks","Juices"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this, //Context for the activity
R.layout.text_view, //Layout to use(create)
items); //Items to be displayed
ListView list=(ListView)findViewById(R.id.listView1);
list.setAdapter(adapter);
}
private void registerClickCallBack() {
ListView list=(ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*TextView textview=(TextView)view;
String msg="hai"+textview.getText();
Toast.makeText(FoodHome.this, msg, Toast.LENGTH_LONG).show();*/
Intent i = new Intent(FoodHome.this, ItemList.class);
startActivity(i);
}
});
}
}
//ItemList.java
package com.example.hotelmenucard;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ItemList extends Activity {
private List<Juice> juices = new ArrayList<Juice>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
populateJuiceList();
populateJuiceListView();
Log.i(juices.toString(),"list of juice");
}
private void populateJuiceList() {
juices.add(new Juice("Lemon", 15, "lemon*****", R.drawable.lemon));
juices.add(new Juice("Strawberry", 15, "Strawberry*****",
R.drawable.strawberry));
juices.add(new Juice("Watermelon", 15, "Watermelon*****",
R.drawable.watermelon));
juices.add(new Juice("Pear", 15, "pear*****", R.drawable.pear));
juices.add(new Juice("Pomegranate", 15, "Pomegranate*****",
R.drawable.pomegranate));
System.out.println(juices+"juices in list");
}
private void populateJuiceListView() {
ArrayAdapter<Juice> adapter = new MyListAdapter(R.layout.activity_item_list);
ListView list = (ListView) findViewById(R.id.carsListV);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Juice> {
public MyListAdapter(int activityItemList) {
super(ItemList.this, activityItemList, juices);
}
public int getCount() {
System.out.println(juices.size()+"in getcount");
return juices.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout itemView =(RelativeLayout) convertView;
if (itemView == null) {
itemView = (RelativeLayout)getLayoutInflater().inflate(R.layout.item_list,
parent, false);
}
Juice juice = juices.get(position+1);
ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView1);
imageView.setImageResource(juice.getId());
TextView txt=(TextView)itemView.findViewById(R.id.textView1);
txt.setText(juice.getName());
TextView text1=(TextView)itemView.findViewById(R.id.textView2);
text1.setText(juice.getUsing());
itemView.bringToFront();
TextView t=(TextView)itemView.findViewById(R.id.textView3);
t.setText(Integer.toString(juice.getRate()));
System.out.println(itemView.getVisibility());
return itemView;
// return super.getView(position, convertView, parent);
}
}
}
//activity_item_list.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:background="#drawable/bg"
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.hotelmenucard.ItemList" >
<ListView
android:id="#+id/carsListV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
//item_list.xml (typo mistake changed from item_view.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:gravity="top"
android:textColor="#B2F8F8" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="#drawable/lemon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
instead of ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,R.layout.text_view,items);
use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, items);
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();
}
Like the title says, my setOnItemClickListener isn't working. I looked through everything I've seen so far on SO and couldn't find my error.
This is the code:
This is the problematic class. It isn't the main class, but is called from an intent:
package...;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
public class GroupActivity extends Activity {
String group_id = "";
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
context = this;
Intent intent = getIntent();
group_id = intent.getStringExtra("group_id");
Log.i("bla", "Launched GroupActivity for group_id " + group_id);
final SubAdapter adapter = new SubAdapter(this, group_id);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setClickable(true);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int idx, long lidx) {
Log.i("print here", "blaa " + idx);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is the SubAdapter class:
package...;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class SubAdapter extends BaseAdapter {
private static final int thumb_width = 128;
private static final int thumb_hight = 128;
private static Bitmap default_thumb = null;
private static LayoutInflater inflater = null;
private final String group_id_;
private final Activity activity;
private final SubAdapter t = this;
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public SubAdapter(Activity a, final String group_id) {
Drawable d = a.getResources().getDrawable(R.drawable.ic_contact_picture);
default_thumb = ((BitmapDrawable) d).getBitmap();
activity = a;
group_id_ = group_id;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int idx, View convertView, ViewGroup parent) {
Log.i("bla2", "getting view with index " + idx);
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.sub_list_item, null);
vi.setClickable(true);
}
TextView tv1 = (TextView)vi.findViewById(R.id.textView1);
TextView tv2 = (TextView)vi.findViewById(R.id.textView2);
ImageView img = (ImageView)vi.findViewById(R.id.imageView1);
tv1.setText("first name" + " " + "last name");
tv2.setText("some date");
// put thumbnail
Bitmap thumb = default_thumb;
img.setImageBitmap(thumb);
Log.i("bla3", "index is " + idx + "selected index is " + selectedPos);
if(selectedPos == idx){
Log.i("bla4", "inside if");
}
return vi;
}
public void setSelectedPosition(int pos){
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
This is the activity_group 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=".GroupActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusableInTouchMode="false"
android:focusable="false" >
</ListView>
</RelativeLayout>
This is the sub_list_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:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false">
<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_margin="6dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="6dp"
android:layout_toLeftOf="#+id/toggleButton1"
android:layout_toRightOf="#+id/imageView1"
android:text="some text"
android:textAppearance="?android:attr/textAppearance" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_toLeftOf="#+id/toggleButton1"
android:text="more text"
android:textAppearance="?android:attr/textAppearance" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="6dp"
android:text="present" />
</RelativeLayout>
Apart from the click listener, everything else seems to work. The SubAdapter opens properly and the list is populated.
Basically what I'm looking for is to get the message "Log.i("print here", "blaa " + idx);" to print - it is the log from the ListView's setOnItemClickListener (look above)
Please let me know if there is any other relevant code missing
Thanks!!!
In your GroupActivity class please add the following code before findViewById method:
now you get the view same and change your code
ListView lv = (ListView) findViewById(R.id.listView1);
to
final View v = inflater.inflate(R.layout.rescuer_no_dialog, null);
ListView lv = (ListView)v.findViewById(R.id.listView1);
You should probably remove the call to
vi.setClickable(true); in your SubAdapter class, because your convertView will consume the click before you onItemClick listener.
Change this
ListView lv = (ListView) findViewById(R.id.listView1);
to
lv = getListView()
Extend ListActivity and declare lv outside methods.
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 :)