Updating TextView without losing Spinner focus in busy ListView - android

I sample sensors (That's make the ListView busy) with custom user rate and I show the values in my listview, therefore I have a custom listview with 3 textview (for x, y and z values) and one spinner.
my layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:minWidth="140dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="X:"
android:id="#+id/tvX"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Y:"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/tvY" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Z:"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="#+id/tvZ" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="30dp">
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/spinner"/>
</LinearLayout>
</LinearLayout>
and I using this code as adapter:
public class customListView extends BaseAdapter
{
public Activity context;
ArrayList<MyActivity.SensorProperties> sensorPropertieses;
public String[] spinnerValues;
public LayoutInflater inflater;
public customListView(Activity context, ArrayList<MyActivity.SensorProperties> sensorPropertieses, String[] spinnerArray)
{
super();
this.context = context;
this.sensorPropertieses = sensorPropertieses;
spinnerValues = spinnerArray;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return sensorPropertieses.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
class ViewHolder
{
TextView Xvalue, Yvalue, Zvalue;
Spinner spinner;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup)
{
ViewHolder holder;
if (view == null)
{
holder = new ViewHolder();
view = inflater.inflate(R.layout.custom_layout, null);
holder.Xvalue = (TextView) view.findViewById(R.id.tvX);
holder.Yvalue = (TextView) view.findViewById(R.id.tvY);
holder.Zvalue = (TextView) view.findViewById(R.id.tvZ);
holder.spinner = (Spinner) view.findViewById(R.id.spinner);
// populate spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(view.getContext(), android.R.layout.simple_spinner_item, spinnerValues);
holder.spinner.setAdapter(dataAdapter);
view.setTag(holder);
}
else
holder = (ViewHolder) view.getTag();
// update sensor values
holder.Xvalue.setText(String.valueOf("X: " + sensorPropertieses.get(i).x));
holder.Yvalue.setText(String.valueOf("Y: " + sensorPropertieses.get(i).y));
holder.Zvalue.setText(String.valueOf("Z: " + sensorPropertieses.get(i).z));
return view;
}
}
my main activity code is (the layout contain only a button and listview):
public class MyActivity extends Activity implements SensorEventListener
{
public class SensorProperties
{
public float x = 0, y = 0, z = 0;
}
ListView listView;
ArrayList<SensorProperties> sensorPropertieses = new ArrayList<SensorProperties>();
customListView adapter;
SensorManager sensorManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// let's work only on two nodes
sensorPropertieses.add(new SensorProperties());
sensorPropertieses.add(new SensorProperties());
listView = (ListView) findViewById(R.id.listView);
String[] spinnerValues = new String[] {"A", "B", "C"};
adapter = new customListView(MyActivity.this, sensorPropertieses, spinnerValues);
listView.setAdapter(adapter);
Button btnRegister = (Button) findViewById(R.id.buRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
RegisterToSensors();
}
});
}
void RegisterToSensors()
{
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent)
{
SensorProperties tmp = new SensorProperties();
tmp.x = sensorEvent.values[0];
tmp.y = sensorEvent.values[1];
tmp.z = sensorEvent.values[2];
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
sensorPropertieses.set(0, tmp);
else
sensorPropertieses.set(1, tmp);
adapter.notifyDataSetChanged();
}
///...
}
The question: How should I work with ListView contains TextView and Spinner witout lost spinner focus although I update the TextView frequency?
EDIT:
I change question title and body thanks to mmlooloo comments.
To be clear:
I success to drop-down the spinner if the ListView isn't busy
I success to drop-down spinner when I updating TextView frequency not in ListView
The issue occur only with ListView and only when the listview is busy
EDIT2:
Currently, I'm not sure if the issue is the losing spinner focus or not. for debugging I add sleep of 30ms before I updating sensors value in the textviews (before // update sensor values) in my custom list view:
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
And I success to drop-down the spinner and choose another item while the textview updating (everything is very slow, but works!).
NEW: I have find, right now, probably the issue is with adapter.notifyDataSetChanged() because I cannot drop down the spinner also when I gray-out the textview prints
Bottom line, I'm not really know what is the root cause but I cannot drop-down the spinner when I update text views frequency in listview, and the question is why?
I'll be glad for any help, Thank you!

Issue here is you are frequently calling adapter.notifyDataSetChanged(); in your onSensorChanged method, so it binds listview every time so does
your getView is called every time and which sets spinner value every time.
What I did is, accessed your listview first and second child in onSensorChanged method and from that I have accessed texviews and update them as frequently as you want
and you can access your spinner too.
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
// SensorProperties tmp = new SensorProperties();
// tmp.x = sensorEvent.values[0];
// tmp.y = sensorEvent.values[1];
// tmp.z = sensorEvent.values[2];
// if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
// sensorPropertieses.set(0, tmp);
// else
// sensorPropertieses.set(1, tmp);
// adapter.notifyDataSetChanged();
Log.i("tag", "ensorEvent.values[0] = " + sensorEvent.values[0]);
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
View childView = (View) listView.getChildAt(0);
TextView xTextView = (TextView) childView.findViewById(R.id.tvX);
xTextView.setText(String.valueOf(sensorEvent.values[0]));
TextView yTextView = (TextView) childView.findViewById(R.id.tvY);
yTextView.setText(String.valueOf(sensorEvent.values[1]));
TextView zTextView = (TextView) childView.findViewById(R.id.tvZ);
zTextView.setText(String.valueOf(sensorEvent.values[2]));
} else {
View childView = (View) listView.getChildAt(1);
TextView xTextView = (TextView) childView.findViewById(R.id.tvX);
xTextView.setText(String.valueOf(sensorEvent.values[0]));
TextView yTextView = (TextView) childView.findViewById(R.id.tvY);
yTextView.setText(String.valueOf(sensorEvent.values[1]));
TextView zTextView = (TextView) childView.findViewById(R.id.tvZ);
zTextView.setText(String.valueOf(sensorEvent.values[2]));
}
}

Run this code it gives you some idea!!
public class MainActivity extends ActionBarActivity {
List<myItem> myItemList;
ListView list;
boolean isTouchEvent = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myItemList = new ArrayList<myItem>();
for (int i = 0; i < 9; i++) {
myItem m = new myItem();
m.setMyText("This is item number "+i);
myItemList.add(m);
}
list = (ListView)findViewById(R.id.list);
list.setAdapter(new MyAdapter(this, R.layout.list_item, myItemList));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, "You have clicked item number "+arg2, Toast.LENGTH_SHORT).show();
}
});
list.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
isTouchEvent = true;
return false;
}
});
}
class MyAdapter extends ArrayAdapter<myItem> {
Context ctx;
public MyAdapter(Context context, int resource, List<myItem> objects) {
super(context, resource, objects);
ctx = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder.txt = (TextView)convertView.findViewById(R.id.TextView_txt);
holder.sp = (Spinner)convertView.findViewById(R.id.Spinner_sp);
List<String> spinnerValues = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
String temp = "Val #" + i;
spinnerValues.add(temp);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx,android.R.layout.simple_spinner_item,spinnerValues);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.sp.setAdapter(adapter);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.sp.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
isTouchEvent = true;
return false;
}
});
holder.sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(!isTouchEvent){
return;
}
String spinnerStringValue = (String)holder.sp.getAdapter().getItem(arg2);
holder.txt.setText("You have selected "+ spinnerStringValue );
holder.txt.invalidate();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
holder.txt.setText(myItemList.get(position).getMyText());
return convertView;
}
}
static class ViewHolder {
public TextView txt;
public Button bt;
public Spinner sp;
}
public class myItem {
private String myText="";
public String getMyText() {
return myText;
}
public void setMyText(String myText) {
this.myText = myText;
}
}
}
activity_main.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
/>
list_item.xml
<?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="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="#+id/TextView_txt"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="16dip" />
<Spinner
android:id="#+id/Spinner_sp"
android:layout_marginLeft="50dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Related

ListView with text and checkbox

I used RelativeLayout for my ListView.
<?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="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="CheckBox" />
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkBox1"
android:layout_alignBottom="#+id/checkBox1"
android:layout_toRightOf="#+id/checkBox1"
android:text="TextView" />
</RelativeLayout>
Here is my ListView code,
private void displayList(){
ArrayList<String> diyListArray = new ArrayList<String>();
diyListArray.add("1");
diyListArray.add("2");
diyListArray.add("3");
diyListArray.add("4");
diyListArray.add("5");
diyListArray.add("6");
diyListArray.add("7");
listAdapter = new ArrayAdapter<String>(this, R.layout.selectrow, R.id.test, diyListArray);
diyList.setAdapter( listAdapter );
}
We can see here that I used ArrayAdapter with resource id of TextView.
I am able to display the checkboxes and their corresponding texts.
How can I get user selected texts from checkboxes?
Try the following:
Demo10.class:----
public class Demo10 extends AppCompatActivity implements Listener {
private ListView lv;
private boolean[] state_ = new boolean[4];
private String[] text_ = new String[4];
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo11);
for(int i = 0 ; i<4 ; i++) {
state_[i] = false;
text_[i] = "text"+i;
}
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ItemAdapter(Demo10.this , this));// This includes a Listener that listens to onCheckBoxStateChanges.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { // used to handle ListView onItemClicks and how they should affect the corresponding checkBox
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox cb = view.findViewById(R.id.cb);
cb.setChecked(!cb.isChecked());
displayCheckBoxStateText();
}
});
}
private void displayCheckBoxStateText(){
for(int i = 0 ; i<4 ; i++){
Log.e("CheckBox", text_[i]+" : " + state_[i]);
}
}
#Override
public void checkBox1(boolean state) {
state_[0] = state;
displayCheckBoxStateText();
}
#Override
public void checkBox2(boolean state) {
state_[1] = state;
displayCheckBoxStateText();
}
#Override
public void checkBox3(boolean state) {
state_[2] = state;
displayCheckBoxStateText();
}
#Override
public void checkBox4(boolean state) {
state_[3] = state;
displayCheckBoxStateText();
}
}
ItemAdapter.class:-----
public class ItemAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater layoutInflater;
private Listener callback;
public ItemAdapter(Context c , Listener l) {
mContext = c;
callback = l;
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return s.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
CheckBox cb;
View view = convertView;
if (convertView == null) {
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.demo10, null);
}
}
cb = view.findViewById(R.id.cb);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (position) {
case 0:
callback.checkBox1(isChecked);
break;
case 1:
callback.checkBox2(isChecked);
break;
case 2:
callback.checkBox3(isChecked);
break;
case 3:
callback.checkBox4(isChecked);
break;
default:
break;
}
}
});
cb.setText(s[position]);
return view;
}
private String[] s = {
"test1",
"test2",
"test3",
"test4"
};
}
Listener interface:----
public interface Listener {
public void checkBox1(boolean state);
public void checkBox2(boolean state);
public void checkBox3(boolean state);
public void checkBox4(boolean state);
}
demo10.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="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<CheckBox
android:id="#+id/cb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
</RelativeLayout>
demo11.xml:-----
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv">
</ListView>
</android.support.constraint.ConstraintLayout>
Make choice mode first
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
I am making a button by clicking on it you can retrieve all the items you checked
button.setOnClickListener(this);
#Override
public void onClick(View v) {
SparseBooleanArray checked = listview.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
int position = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(listAdapter.getItem(position));
}
}
here you get all the items checked in your selectedItems ArrayList.
You can override getView() of ArrayAdapter and handles views by yourself. Replace:
listAdapter = new ArrayAdapter<String>(this, R.layout.selectrow, R.id.test, diyListArray);
with
listAdapter = new ArrayAdapter<String>(this, R.layout.selectrow, diyListArray){
boolean[] checked = new boolean[diyListArray.size()];
#NonNull
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if(convertView == null) convertView = getLayoutInflater().inflate(R.layout.selectrow, null);
TextView textView = convertView.findViewById(R.id.test);
CheckBox checkBox = convertView.findViewById(R.id.checkBox1);
textView.setText(getItem(position));
checkBox.setChecked(checked[position]);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
checked[position] = !checked[position];
String msg = "Checked Item: ";
if(!checked[position]) msg = "Unchecked Item: ";
Toast.makeText(getContext(), msg + getItem(position), Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
});
return convertView;
}
};
I have blog about ListView: http://programandroidlistview.blogspot.com/
Hope that helps!

Get values spinner in listView(android)

I apologize for my English. I thought a few days how to make a spinner in the ListView, and I was able to do it. But now I do not know how to take data from the spinner. I ask please tell me how to take the data from each spinner in ListView
activity_main.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=".MainActivity">
<ListView
android:id="#+id/lvCustomList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
loyout_list_item.xml
<?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:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
MainActivity
ListView lvDetail;
Button btn;
Context context = MainActivity.this;
ArrayList myList = new ArrayList();
String[] nameQuestions = new String[] {
"question1", "question2", "questions3",
"questions4", "questions5"
};
ArrayList<HashMap<String, String>> listArray = new ArrayList<HashMap<String, String>>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvDetail = (ListView) findViewById(R.id.lvCustomList);
btn = (Button) findViewById(R.id.sendQuestions);
getDataInList();
lvDetail.setAdapter(new MyBaseAdapter(context, myList));
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//how get value all spinners?
}
});
}
private void getDataInList() {
for(int i=0;i<4;i++) {
ListData ld = new ListData();
ld.setQuestions(nameQuestions[i]);
myList.add(ld);
}
}
}
ListData
public class ListData {
String questions;
public String getQuestions() {
return questions;
}
public void setQuestions(String questions) {
this.questions = questions;
}
}
MyBaseAdapter
public class MyBaseAdapter extends BaseAdapter {
ListData ld = new ListData();
ArrayList<ListData> myList = new ArrayList<ListData>();
LayoutInflater inflater;
Context context;
String cardStatusString;
ArrayList<String> spinnerArray = new ArrayList<String>();
public MyBaseAdapter(Context context, ArrayList myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);
spinnerArray.add("1");
spinnerArray.add("2");
spinnerArray.add("3");
spinnerArray.add("4");
spinnerArray.add("5");
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, null);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.sp = spinnerDetails(convertView, R.id.spinner, myList.get(position).getQuestions());
mViewHolder.nameText = nameDetails(convertView, R.id.nameText, myList.get(position).getQuestions());
return convertView;
}
private TextView nameDetails(View v, int resId, String text) {
TextView nameText = (TextView) v.findViewById(resId);
nameText.setText(text);
return nameText;
}
//this is create spinner
private Spinner spinnerDetails(View v, int resId, String text) {
Spinner mySpinner = (Spinner) v.findViewById(resId);
#SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(context,
android.R.layout.simple_spinner_dropdown_item,
spinnerArray);
mySpinner.setAdapter(spinnerArrayAdapter);
return mySpinner;
}
private class MyViewHolder {
Spinner sp;
TextView nameText;
}
}
You can iterate over your ListView and collect all the selections into a List as
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
List<String> selections = new ArrayList<String>();
for (int i = 0; i < lvDetail.getChildCount(); i++) {
// Get row's spinner
View listItem = lvDetail.getChildAt(i);
Spinner spinner = (Spinner) listItem.findViewById(R.id.spinner);
// Get selection
String selection = (String) spinner.getSelectedItem();
selections.add(selection);
}
// process selections
}
});
I would say add a method on the Adapter that returns that value to the activity its in, since there is no other way to get the value otherwise, except with getChildAt as Ravi pointed out. But this may need further tweaking too since for ListViews the elements that are not visible in the screen are not loaded (and the spinner value will reset to default), but that depends on what you want this for

android edittext custom listview

I have a custom listview with textview and edittext.there is no problem when listview has 6 item or less but more than 6 item listview like this video .How can I solve this problem?
public class Form2 extends ListActivity {
CustomListAdapter cla;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form2);
cla = new CustomListAdapter(this);
setListAdapter(cla);
}
my custom adapter is
public class CustomListAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private ArrayList<Siparis> notlar;
public CustomListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
notlar = new ArrayList<Siparis>();
DBHandler db = new DBHandler(context);
db.getWritableDatabase();
notlar=db.getSiparis();
db.close();
}
#Override
public int getCount() {
return notlar.size();
}
#Override
public Siparis getItem(int position) {
return notlar.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_kap, null);
holder = new ViewHolder();
holder.mKonu = (TextView) convertView.findViewById(R.id.malzeme);
holder.mTarih2 = (EditText) convertView.findViewById(R.id.miktar);
}
else {
holder = (ViewHolder) convertView.getTag();
}
//holder.mTarih2.setText(String.valueOf(notlar.get(position).miktar));
holder.mKonu.setText(notlar.get(position).malzeme_adi);
convertView.setTag(holder);
return convertView;
}
public class ViewHolder {
TextView mKonu;
EditText mTarih2;
}
}
You need to implement the ViewHolder pattern.
See this blog post for more details: http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_kap, null);
holder = new ViewHolder();
holder.mKonu = (TextView) convertView.findViewById(R.id.malzeme);
holder.mTarih2 = (EditText) convertView.findViewById(R.id.miktar);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTarih2.setText(String.valueOf(notlar.get(position).miktar));
holder.mKonu.setText(notlar.get(position).malzeme_adi);
return convertView;
}
public static class ViewHolder {
TextView mKonu;
EditText mTarih2;
}
hi this is my solution handmade listview
xml file:
<?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:layout_weight="1"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/mylinear"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/group4ff"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/vazgecff"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:onClick="cagir"
android:text="vazgec" />
<Button
android:id="#+id/kaydetff"
android:layout_width="0dp"
android:layout_weight=".70"
android:layout_height="wrap_content"
android:onClick="cagir"
android:text="kaydet" />
</LinearLayout>
</LinearLayout>
java code:
public class Form3 extends Activity {
LinearLayout[] llx ;
TextView[] tx ;
EditText[] ex ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form3);
DBHandler db = new DBHandler(this);
ArrayList<Siparis> listem = db.getSiparis();
db.close();
LinearLayout ll = (LinearLayout) findViewById(R.id.mylinear);
llx = new LinearLayout[listem.size()];
tx = new TextView[listem.size()];
ex = new EditText[listem.size()];
for (int i = 0; i < listem.size(); i++) {
llx[i] = new LinearLayout(this);
tx[i] = new TextView(this);
ex[i] =new EditText(this);
tx[i].setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,0.8f));
ex[i].setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,0.2f));
tx[i].setText(listem.get(i).getMalzeme_adi());
ex[i].setInputType(InputType.TYPE_CLASS_NUMBER);
llx[i].setId(i);
llx[i].setClickable(true);
final int j = i;
llx[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
msg(tx[j].getText().toString());
}
});
llx[i].addView(tx[i]);
llx[i].addView(ex[i]);
ll.addView(llx[i]);
}
}
private void msg(String x){
Toast.makeText(this, x, Toast.LENGTH_LONG).show();
}
public void cagir(View view){
switch (view.getId()) {
case R.id.kaydetff:
for (int i = 0; i < ex.length; i++) {
if(!ex[i].getText().toString().equals("")){
Log.e(tx[i].getText().toString(),ex[i].getText().toString());
}
}
break;
default:
break;
}
}
}
So the problem is when getView() is called you need to set the value of the EditText just like you set the value of the TextView
So each time the user types something, you put the value in the corresponding Siparis object. At the time of getView() you pick out the value from the object and set it in the view.
This is what you are doing with your TextView (Item1, Item2.. etc) They are never displayed wrongly. But with the edit text, since the value changes, you must update the miktar variable of the corresponding Siparis object. And bind it on getView()
...
...
holder.mKonu.setText(notlar.get(position).malzeme_adi);
//every time the user changes the edit text, save
//the current value of the edit text to retrieve later
holder.mTarih2.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable editable) {
notlar.get(position).miktar = Integer.parseInt(editable.toString());
}
....
};
holder.mTarih2.setText(String.valueOf(notlar.get(position).miktar));
...
Take a look at this similar question. The answer recommends exactly the same thing.

Custom List View with OnClickListener

I have custom ListView layout with a TextView and CheckBox. Everything works fine.
What I want is, when I click on the CheckBox or TextView (on the single View from ListView) both should behave like one object. (I can click on the CheckBox and it does not effect the TextView and TextView has no effect on CheckBox.) Code has no problem.
I have implemented all possible solutions but problem is still there. (One single click on every object of list should consider ONE COMPLETE CLICK for complete row.) I hope I explained very well.
MAIN ACTIVITY
package com.example.smsplanner;
public class SMSPlanner extends ListActivity {
ListView contactsListView;
private String TAG = "SMSPlanner"; CheckBox check;
int count;
List<ContactInfo> list = new ArrayList<ContactInfo>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ph = new String[3];
phType = new String[3];
LoadContactListFromPhone();
ContactsAdapter contactadAdapter = new ContactsAdapter(this, list);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(contactadAdapter);
}
#Override
public void onListItemClick(ListView parent, View v, int position, long id)
{
TextView tx =(TextView)v.findViewById(R.id.firstname);
TextView ph =(TextView)v.findViewById(R.id.phone);
Toast.makeText(this, tx.getText().toString() + " " + ph.getText().toString() + " " + Integer.toString(count), Toast.LENGTH_SHORT).show();
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
void LoadContactListFromPhone()
{
loadlistandreturns();
}
void call()
{
Toast toast = Toast.makeText(this, "Called...",Toast.LENGTH_LONG);
toast.show();
}
}
CUSTOM ADAPTER
public class ContactsAdapter extends ArrayAdapter<ContactInfo>
{
private final Activity context;
int resourceid;
List<ContactInfo> list = null;
public ContactsAdapter(Activity context, List<ContactInfo> list) {
super(context, R.layout.contactrow, list);
this.context = context;
this.list = list;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
final class ContactHolder{
TextView txtviewfirstname;
CheckBox chkselected;
TextView txtviewphone;
}
}
LAYOUT
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<RadioGroup
android:id="#+id/rgStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="15"
android:orientation="vertical" >
<TextView
android:id="#+id/firstname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RadioGroup>
<RadioGroup
android:id="#+id/rgStyle2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="85"
android:orientation="vertical" >
<CheckBox
android:id="#+id/check"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>
</RadioGroup>
</LinearLayout>
1. Bydefault all the Rows of the ListView are enabled to listen to click....
You must implement onItemClickListener() for the ListView....
See this example:
http://www.mkyong.com/android/android-listview-example/
you can use a CheckedTextView, or you can create a Checkable Layout, like this one :
http://tokudu.com/2010/android-checkable-linear-layout/
i think you should make adapter as
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}

Spinner in ListView preventing highlighting in android

I'm trying to place a spinner inside a list view item in android. The problem is this seems to prevent the row from being highlighted during a press and the onItemClickListener is not getting called by the ListView. Basically I am trying to mimic the functionality in the Google Music app. Does anyone know how to do this? This is the code I have been trying (I know it may not be the best... just throwing together a quick sample which shows the problem):
Activity:
public class ListViewTestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView1);
DataHolder data = new DataHolder(this);
DataHolder data1 = new DataHolder(this);
DataHolder data2 = new DataHolder(this);
DataHolder data3 = new DataHolder(this);
DataHolder data4 = new DataHolder(this);
DataAdapter d = new DataAdapter(this, R.layout.rowview, new DataHolder[] { data, data1, data2, data3, data4 });
listView.setAdapter(d);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.e("ERROR", "look at me!");
}
});
}
}
public class DataHolder {
private int selected;
private ArrayAdapter<CharSequence> adapter;
public DataHolder(Context parent) {
adapter = ArrayAdapter.createFromResource(parent, R.array.choices, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public ArrayAdapter<CharSequence> getAdapter() {
return adapter;
}
public String getText() {
return (String) adapter.getItem(selected);
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}
This is the list adapter:
public class DataAdapter extends ArrayAdapter<DataHolder> {
private Activity myContext;
public DataAdapter(Activity context, int textViewResourceId, DataHolder[] objects) {
super(context, textViewResourceId, objects);
myContext = context;
}
// We keep this ViewHolder object to save time. It's quicker than findViewById() when repainting.
static class ViewHolder {
protected DataHolder data;
protected TextView text;
protected Spinner spin;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
// Check to see if this row has already been painted once.
if (convertView == null) {
// If it hasn't, set up everything:
LayoutInflater inflator = myContext.getLayoutInflater();
view = inflator.inflate(R.layout.rowview, null);
// Make a new ViewHolder for this row, and modify its data and spinner:
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.text);
viewHolder.data = new DataHolder(myContext);
viewHolder.spin = (Spinner) view.findViewById(R.id.spin);
viewHolder.spin.setAdapter(viewHolder.data.getAdapter());
// Used to handle events when the user changes the Spinner selection:
viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
viewHolder.data.setSelected(arg2);
viewHolder.text.setText(viewHolder.data.getText());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
Log.d("DBGINF", "asdf");
}
});
// Update the TextView to reflect what's in the Spinner
viewHolder.text.setText(viewHolder.data.getText());
view.setTag(viewHolder);
Log.d("DBGINF", viewHolder.text.getText() + "");
} else {
view = convertView;
}
// This is what gets called every time the ListView refreshes
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(getItem(position).getText());
holder.spin.setSelection(getItem(position).getSelected());
return view;
}
}
Main layout for activity
<?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">
<ListView android:id="#+id/listView1" android:layout_height="match_parent" android:layout_width="match_parent" />
</LinearLayout>
Layout for a row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content" android:weightSum="1" >
<TextView android:layout_width="wrap_content"
android:layout_height="match_parent" android:id="#+id/text"
android:layout_weight="0.5" android:textSize="25sp" />
<Spinner android:layout_width="0dp" android:layout_height="wrap_content"
android:id="#+id/spin" android:prompt="#string/choice_prompt"
android:layout_weight="0.5" />
</LinearLayout>
Thanks for any help!
I had the same problem with ImageButton, and what was needed was to do
mImageButton.setFocusable(false);
I can't garantee it's the same for Spinner, but my money is on it.
Best regards.

Categories

Resources