I have 2 ListView in my Layout and one common row layout for it and the row is repeating multiple times .I am confused how i will identify on both of ListView which result is selected and how i will get the data of TextViews that are selected in both the listViews.
This is my ListView XML
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/footerLayout"
android:layout_below="#+id/sortFlightLayouts"
android:orientation="horizontal" >
<ListView
android:id="#+id/lvDepartures"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<View
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray" />
<ListView
android:id="#+id/lvArrivals"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
And this is the Row Layout that i am putting in these two ListViews
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/flightLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5sp"
android:layout_marginRight="10sp"
android:layout_marginTop="5sp"
android:layout_marginLeft="5dp"
android:src="#drawable/spicejet" />
<TextView
android:id="#+id/flightCompanyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/flightLogo"
android:text="SpiceJet" />
<TextView
android:id="#+id/flightNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/flightCompanyName"
android:text="9W-123" />
<TextView
android:id="#+id/flightTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="12dp"
android:layout_toRightOf="#+id/flightLogo"
android:gravity="center_vertical"
android:text="6:00 - 7:00" />
<TextView
android:id="#+id/flightStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/flightTime"
android:layout_toRightOf="#+id/flightLogo"
android:layout_marginLeft="12dp"
android:text="1h 35m | Non Stop" />
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/flightStop"
android:layout_below="#+id/flightStop"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/flightCompanyName"
android:text="Rs 20,000" />
</RelativeLayout>
And in MY Activity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
ArrayList<HashMap<String, String>> flightData = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 12; i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(FlightCompanyName, "SpiceJet");
map.put(FlightNumber, "SG-123");
map.put(FlightTime, "6:00 - 7:00");
map.put(FlightStop, "1h 30m | Non Stop");
map.put(FlightCost, "Rs 12,000");
// adding HashList to ArrayList
flightData.add(map);
}
onewayListView=(ListView)findViewById(R.id.lvDepartures);
ReturnListView=(ListView)findViewById(R.id.lvArrivals);
// Getting adapter by passing xml data ArrayList
onewaydata=new OneWayFlightResult(this, flightData);
onewayListView.setAdapter(onewaydata);
returndata=new ReturnFlightResult(this, flightData);
ReturnListView.setAdapter(returndata);
}
A bit more explanation this design is for flight result.So if the user is searching for roundtrip.I will show him 2 result one on left and other on right side .Now what i want that user can select one flight from left list and and one flight from the right side.Once he select the flight .I have to get the data of the flight selected.So how could i do this .Please help me
SparseBooleanArray checked;
lvDialog.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
checked = lvDialog.getCheckedItemPositions();
if(checked.get(arg2))
{
chkText.setTextColor(Color.CYAN);
colorRow ++;
}
else
{
chkText.setTextColor(Color.BLACK);
colorRow--;
}
}
});
hey here is some code...try
Related
//My xml code
<ScrollView
android:id="#+id/scrollview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="35dp">
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/background_light"
android:gravity="center"
android:paddingBottom="5dp" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Venue, Date"
android:textSize="12sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:text="Description"
android:textSize="12sp"
android:paddingBottom="5dp" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="14dp"
android:src="#drawable/demo" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Event of the Week" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView7"
android:layout_marginLeft="14dp"
android:text="Event Name" />
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView6"
android:layout_marginTop="14dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:paddingBottom="5dp"
android:verticalSpacing="10dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Today's Events" />
</RelativeLayout>
</ScrollView>
//MainActivity
class LoadProfile extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EventHome.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
String json = null;
PROFILE_URL = "http://www.example.com/filter_event_android.php?pin="+phone;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PROFILE_URL);
httppost.setEntity(new UrlEncodedFormEntity(params));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
json = EntityUtils.toString(resEntity);
Log.i("All Events: ", json.toString());
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
#SuppressLint("InlinedApi") #Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
pDialog.dismiss();
try{
event = new JSONObject(json);
final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
JSONArray user = event.getJSONArray("events");
String contains=json.toString();
if(contains.contains("id"))
{
for (int i = 0; i < user.length(); i++) {
JSONObject object = user.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", object.getString("id"));
map.put("name", object.getString("name"));
map.put("date_d", object.getString("date_d"));
map.put("location", object.getString("location"));
map.put("images", "http://www.example.com/"+object.getString("images"));
arraylist.add(map);
}
String[] from = {"name", "date_d", "location", "images"};
int[] to = {R.id.textView1, R.id.textView2, R.id.textView3, R.id.iv_flag};
ListAdapter adapters = new MyAdapter(EventHome.this,arraylist,R.layout.list_event_home,from,to);
gv1.setAdapter(adapters);
}
else
{
gv1.setVisibility(View.GONE);
TextView dynamicTextView = new TextView(EventHome.this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dynamicTextView.setText("No events available");
}
gv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(EventHome.this,EventSingle.class);
i.putExtra("event_id", arraylist.get(arg2).get("id"));
startActivity(i);
}
});
}catch(Exception e)
{
e.printStackTrace();
}
}
}
//MyAdapter.java
public class MyAdapter extends SimpleAdapter{
public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
}
public View getView(int position, View convertView, ViewGroup parent){
// here you let SimpleAdapter built the view normally.
View v = super.getView(position, convertView, parent);
// Then we get reference for Picasso
ImageView img = (ImageView) v.getTag();
if(img == null){
img = (ImageView) v.findViewById(R.id.iv_flag);
v.setTag(img); // <<< THIS LINE !!!!
}
// get the url from the data you passed to the `Map`
#SuppressWarnings("rawtypes")
String url = (String) ((Map)getItem(position)).get("images");
// do Picasso
Picasso.with(v.getContext()).load(url).into(img);
// return the view
return v;
}
}
The above layout shows only one row values in my gridview. But i have morethan 5 values for gridview. Why is it not showing the other values. I tried a lot but no use. Does anyone have solution.
This is probably caused by having GridView inside of ScrollView. Since both layouts are scrollable this causes a lot of problems, in your case the height of GridView cannot be properly determined and the scroll events are eaten by the ScrollView.
With ListView you can simply declare the ListView as root element and then add other scrollable content as headers or footers. GridView natively does not support this but fortunately there is subclass implementation of HeaderGridView which solves this problem.
What you should do is put only the HeaderGridView to your xml inflated by the activity.
<?xml version="1.0" encoding="utf-8"?>
<your.package.HeaderGridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp" />
and then implement the RelativeLayout as a header view in different xml (for example header.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/background_light"
android:gravity="center"
android:paddingBottom="5dp">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Venue, Date"
android:textSize="12sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:text="Description"
android:textSize="12sp"
android:paddingBottom="5dp" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="14dp"
android:src="#drawable/demo" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Event of the Week" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView7"
android:layout_marginLeft="14dp"
android:text="Event Name" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Today's Events" />
</RelativeLayout>
In your activity you add the header to your HeaderGridView
HeaderGridView gridView = (HeaderGridView) findViewById(R.id.gridView1);
View header = getLayoutInflater().inflate(R.layout.header, gridView, false);
gridView.addHeaderView(header);
// set adapter AFTER adding headerViews
gridView.setAdapter(MyAdapter(...))
Tried it, worked like a charm. Hope it helps!
The problem and fix I have found is that if the gridview has rows data bound the select statement must have that particular field in my case I am evaluating Grade and this grade was not included in my select statement and the gridview was returning single row no matter what but the moment I included the Grade in my select statement the problem was fixed
Protected Sub GVRos_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GVRos.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim stat As String
'This row evaluates and puts the value in stat as per the Finalised columon result
stat = DataBinder.Eval(e.Row.DataItem, "Grade").ToString()
If stat = "A" Then
e.Row.BackColor = System.Drawing.Color.LightGreen
ElseIf stat = "B" Then
e.Row.BackColor = System.Drawing.Color.LightBlue
ElseIf stat = "C" Then
e.Row.BackColor = System.Drawing.Color.Yellow
ElseIf stat = "D" Then
e.Row.BackColor = System.Drawing.Color.Orange
ElseIf stat = "F" Then
e.Row.BackColor = System.Drawing.Color.Red
Else
e.Row.BackColor = System.Drawing.Color.White
End If
End If
End Sub
code:
{
private ListView l1;
Dbhelper mydb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.e_view);
l1 = (ListView)findViewById(R.id.listView1);
mydb = new Dbhelper(this);
ArrayList<Map<String, String>> list = mydb.getAllexps();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
//adding it to the list view.
l1.setAdapter(adapter);
l1.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),Ex_add.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
#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;
}
}
plz help me out
I need to display amount right side of the list and other things to left side.
U need to create custom Listview
refere this link and u will get idea what i m telling u ;)
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
In the following line you are using android's default list item layout android.R.layout.simple_list_item_2.
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
Instead of using this default layout you can define your own custom layout by putting a XML file into your layout folder. In this custom layout file you can align the content as your need.
you should use this
<?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" >
<LinearLayout
android:id="#+id/pop_up_child_container_topview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="100" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50" >
<TextView
android:id="#+id/pop_up_child_inventoryno_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="#string/str_fullhistory_popup_child_inv_no"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_inventoryno_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/pop_up_child_inventoryno_title"
android:padding="2dp"
android:text="30vuasou3209"
android:textSize="#dimen/subtext_textsize" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="50"
android:gravity="right" >
<TextView
android:id="#+id/pop_up_child_quantity_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="2dp"
android:text="#string/str_fullhistory_popup_child_qty"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_quantity_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/pop_up_child_quantity_title"
android:padding="2dp"
android:text="30vuasou3209"
android:textSize="#dimen/subtext_textsize" />
</RelativeLayout>
</LinearLayout>
<View
android:id="#+id/popup_child_seperator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/pop_up_child_container_topview"
android:background="#color/fleet_guard_seperator_grey" />
<LinearLayout
android:id="#+id/pop_up_child_container_balance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/popup_child_seperator"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:weightSum="100" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="50"
android:gravity="right" >
<TextView
android:id="#+id/pop_up_child_balance_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/pop_up_child_balance_tv"
android:text="#string/str_fullhistory_popup_child_balance"
android:textSize="#dimen/list_title_textsize" />
<TextView
android:id="#+id/pop_up_child_balance_tv"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="12"
android:textSize="#dimen/list_title_textsize" />
</RelativeLayout>
</LinearLayout>
Hi Om please use linearlayout with horizontal and add textview and set custom xml file to your adapter
I have a Listview content form TextView and Edittext, and I want to get the value from each Edittext in this Listview.
My problems are:
When the Listview is created, the focus is not on the first Edittext in Listview.
The second problem, is when I enter a value in Edittext and move list text down in screen the value in Edittext changed or removed, or the value moved to another Edittext on screen.
You can see image in this
link
my code is
ArrayList<HashMap<String, String>> AllStudent ;
AllStudent = new ArrayList<HashMap<String, String>>();
for (int j=0; j<15; j++){
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("id"," " + j);
map.put("name", "A" + j );
// adding HashList to ArrayList
AllStudent.add(map);
}
listadapter = new SimpleAdapter(
sc_grade_group.this, AllStudent,
R.layout.list_item_graed, new String[] { "id","name" },
new int[] { R.id.list_itemGra_t1, R.id.list_itemGra_t2 });
setListAdapter(listadapter);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item_LinearLayout"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#drawable/custom_bg_1"
android:orientation="horizontal"
android:padding="2dp"
android:weightSum="1" >
<TextView
android:id="#+id/list_itemGra_t1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/list_itemGra_t2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="#drawable/custom_bg_2"
android:gravity="left|center_vertical"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#color/black"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/list_itemGra_t4"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_weight="0.28"
android:background="#drawable/custom_bg_2"
android:gravity="left|center_vertical"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#color/gold"
android:textSize="13sp"
android:textStyle="bold"
android:visibility="gone" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:background="#color/cachecolor2"
android:gravity="center_vertical|center_horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/list_itemGra_Degree"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
</LinearLayout>
<TextView
android:id="#+id/list_itemGra_t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.07"
android:background="#color/cachecolor2" >
</LinearLayout>
<TextView
android:id="#+id/list_itemGra_t5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Ok first of all, if you want that entered information should be persistent when scrolled out and scrolled back, you have to make a model that should store what was entered. All information is cleared when you scroll, and scroll back is because ListView re-creates only those view's which are on screen and visible.
Used SimpleAdapter can hook up with ViewBinder interface, though its hard to handle storing in there. My suggestion would be to make a class model and make a custom adapter for this.
Here is a great tutorial on various ListView implementations (http://www.vogella.com/tutorials/AndroidListView/article.html).
Second problem, to prevent the view focusing once you enter the area, you can try setting
seting the attributes android:focusable="true" and android:focusableInTouchMode="true" on EditText, though this should be tested, im not sure it will work.
1, Try in onCreate:
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) { // run when focus is lost
String value = v.getText().toString(); // get the value from the EditText
// Your code to update hashmap
}
}
});
2, When you create the ListView, add a OnFocusChangeListener, like shown below
myList.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
myHandler.postAtFrontOfQueue(new Runnable() {
public void run() {
myList.setSelection(0);
}
});
}
}
});
and if it wont work read Focusable EditText inside ListView
When I first launch my app everything displays and works correctly but when I close and reopen it a spinner and button that sit next to each other at the top of my relative layout are only about half of their usual height and the spinner is unusable. If I click it nothing drops down (but the button works as usual). Other elements below the spinner and but display as usual.
I spent half the day googling and playing with the app code and layout XML and have made little to no progress.
Anyone got a clue as to what could be causing this? It only happens on subsequent runs of the app.
Here is the layout 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" >
<Spinner
android:id="#+id/namespinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/namespinner"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/namespinner"
android:onClick="onEditButtonPressed"
android:text="#string/dob_edit" />
<TextView
android:id="#+id/secondsDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="36dp"
android:text="#string/seconds"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/minutesDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/secondsDisplay"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:text="#string/minutes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/hoursDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/minutesDisplay"
android:layout_marginBottom="27dp"
android:text="#string/hours"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/hourstext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/hoursDisplay"
android:layout_alignBottom="#+id/hoursDisplay"
android:layout_alignLeft="#+id/daystext"
android:text="#string/hours_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/dayDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/hoursDisplay"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="#string/days"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/daystext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dayDisplay"
android:layout_toRightOf="#+id/namespinner"
android:text="#string/days_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/minutesText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minutesDisplay"
android:layout_alignBottom="#+id/minutesDisplay"
android:layout_alignLeft="#+id/hourstext"
android:text="#string/minutes_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/secondstext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/secondsDisplay"
android:layout_alignBottom="#+id/secondsDisplay"
android:layout_alignLeft="#+id/minutesText"
android:text="#string/seconds_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
This should be the relevant parts of the main activity that call it:
public class MainActivity extends Activity {
static Spinner nameSpinner;
ArrayAdapter nameAdapter;
static ArrayList nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onResume() {
super.onResume();
// get spinner and init array if needed
if (nameList == null) {
nameSpinner = (Spinner) findViewById(R.id.namespinner);
nameList = new ArrayList();
nameList.add("( Add )");
}
// populate spinner
nameAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, nameList);
nameSpinner.setAdapter(nameAdapter);
// listen for events/selections on the spinner
nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView parentView,
View selectedItemView, int position, long id) {
// read state
}
}
#Override
public void onNothingSelected(AdapterView parentView) {
// do I want to do anything here?
}
});
}
}
Edited to clean up the code display and add the class def. and class variables.
You have to insert the code wrote in the onresume method in oncreate method!
Setting the spinner in an if statement that didn't really have anything to do with setting the spinner was the cause of the problem. By changing this:
if (nameList == null) {
nameSpinner = (Spinner) findViewById(R.id.namespinner);
nameList = new ArrayList();
nameList.add("( Add )");
}
to
nameSpinner = (Spinner) findViewById(R.id.namespinner);
if (nameList == null) {
nameList = new ArrayList();
nameList.add("( Add )");
}
got it sorted.
A good nights sleep and fresh eyes where all that were needed. :P
I have a Spinner in my class with certain items. I have to display an EditText whose visibility I have set to "Invisible". I need to show this EditText when "Others" item is selected in the spinner.
Here is my code:-
Activity class:-
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maintenance);
ETServiceStation = (EditText)findViewById(R.id.ETServiceStation);
ETServiceType = (EditText)findViewById(R.id.ETServiceType);
ETServiceCost= (EditText)findViewById(R.id.ETServiceCost);
spinServiceType = (Spinner)findViewById(R.id.spinServiceType);
btnMaintenenceSave= (ImageButton)findViewById(R.id.btnMaintenenceSave);
btnMaintenanceClear = (ImageButton)findViewById(R.id.btnMaintenenceClear);
spinServiceType.setOnItemSelectedListener(this);
btnMaintenanceClear.setOnClickListener(this);
btnMaintenenceSave.setOnClickListener(this);
ArrayAdapter<String> serviceTypeAdapter = new ArrayAdapter<String>(getApplicationContext(), R.array.service_type_array, android.R.layout.simple_spinner_item);
serviceTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinServiceType.setAdapter(serviceTypeAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int servicePos, long id)
{
// TODO Auto-generated method stub
spinnerServiceType = spinServiceType.getItemAtPosition(servicePos).toString();
if(spinnerServiceType.matches("Others"))
{
ETServiceType = (EditText)findViewById(R.id.ETServiceType);
ETServiceType.setVisibility(View.VISIBLE);
spinnerServiceType = ETServiceType.getText().toString();
}
serviceType = spinnerServiceType;
}
}
Resource:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="service_type_array">
<item>Full Service</item>
<item value="serviceother">Other</item>
</string-array>
</resources>
maintenance.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:orientation="vertical"
android:padding="30dp">
<TextView
android:id="#+id/txtMTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maintenance"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:textColor="#color/SteelBlue"
android:typeface="sans"/>
<TextView
android:layout_marginTop="60dp"
android:id="#+id/txtServiceStation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service station name:" />
<EditText
android:id="#+id/ETServiceStation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/txtServiceType"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service type: "/>
<Spinner
android:id="#+id/spinServiceType"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/ETServiceType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:visibility="invisible"
android:ems="10" />
<TextView
android:id="#+id/txtServiceCost"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Servicing expence:" />
<EditText
android:id="#+id/ETServiceCost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btnMaintenenceSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:src="#drawable/fuelbtnsaveclick" />
<ImageButton
android:id="#+id/btnMaintenenceClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="#color/white"
android:src="#drawable/fuelbtnclearclick" />
</LinearLayout>
</LinearLayout>
Database:
public long createMainEntry(MaintenanceExpense me)
{
ContentValues mcv = new ContentValues();
mcv.put(KEY_SERVICE_STATION,me.getServiceStation());
mcv.put(KEY_SERVICE_TYPE,me.getServiceType());
mcv.put(KEY_SERVICE_COST,me.getServiceCost());
return otherInfoDatabase.insert(MAINTENANCE_DATABASE_TABLE, null, mcv);
}
I have to display the ETServiceType when "Others" item is clicked.
Thanks for any help.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(spinServiceType.getItemAtPosition(position).toString().equalsIgnorecase("Others")
{
editextObject.setVisibility(View.VISIBLE);
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// TODO Auto-generated method stub
spinnerServiceType = spinServiceType.getItemAtPosition(position).toString();
//if spinnerServiceType 's value is other
//then set visibility of textview visible put text watcher on it so u can get text when user finished ..if u dont want then u can use simple getText then change its visibility
//As u are setting your array value from resource you cant update it programmatically
//so first of all u have to take its array items in list . so u can update this list after user enter the value in edit text
}