I am fetch data using json and set my spinner and I am also want to add radio button in my operator spinner but radio button not visible and not working I want visible button some Ids but its not visible.
MyXML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rount_corner1"
android:layout_margin="#dimen/activity_horizontal_margin"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginTop="25dp"
android:inputType="number"
android:maxLength="10"
android:textColor="#000000"
android:layout_marginRight="40dp"
android:hint="Enter Mobile number"
android:textColorHint="#000000"
android:layout_marginLeft="40dp"
android:clickable="true"
android:id="#+id/prenumber"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginTop="7dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:clickable="true"
android:hint="Enter Recharge Amount"
android:textColorHint="#000000"
android:textColor="#000000"
android:maxLength="4"
android:inputType="number"
android:id="#+id/rechergpre"
/>
<Spinner
android:id="#+id/operator_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="left"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="7dp"
android:prompt="#string/appbar_scrolling_view_behavior">
</Spinner>
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="38dp"
android:visibility="gone"
>
<RadioButton
android:id="#+id/_TopUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TopUp "
/>
<RadioButton
android:id="#+id/Special"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Special(2G,3G,SMS,VOICE )"
/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:id="#+id/prepaid_submit"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="Submit"
android:textStyle="bold"
android:layout_marginTop="60dp"
/>
</LinearLayout>
</RelativeLayout>
Mycode in Fragment
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
MyApplication myOpt = (MyApplication)getActivity().getApplicationContext();
protected Void doInBackground(Void... params) {
json_data = new ArrayList<Json_Data>();
datalist = new ArrayList<String>();
oprList = new ArrayList<String>();
jsonobject = JSONfunctions
.getJSONfromURL("http://www.example.com");
Log.d("Response: ", "> " + jsonobject);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Json_Data opt_code = new Json_Data();
opt_code.setName(jsonobject.optString("name"));
opt_code.setId(jsonobject.optString("ID"));
json_data.add(opt_code);
datalist.add(jsonobject.optString("name"));
oprList.add(jsonobject.getString("ID"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
final Spinner mySpinner = (Spinner)getView().findViewById(R.id.operator_spinner);
mySpinner
.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item,
datalist));
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// get Operaotor cod in opt_code field
String opt_code = oprList.get(position);
String selectedItem = arg0.getItemAtPosition(position).toString();
Log.d("Selected operator is==", "======>" + selectedItem);
Log.d("Selected Value is======", "========>" + position);
Log.d("Selected ID is======", "========>" + opt_code);
if(opt_code=="8" || opt_code=="14"|| opt_code=="35"||opt_code=="36"||opt_code=="41"||opt_code=="43")
{
_RadioGroup = (RadioGroup)getView().findViewById(R.id.radioGroup);
_RadioGroup.setVisibility(View.VISIBLE);
mySpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int selectedId = _RadioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
RadioButton _RadioSex = (RadioButton)getView().findViewById(selectedId);
Toast.makeText(getActivity(),
_RadioSex.getText(), Toast.LENGTH_SHORT).show();
}
});
}
String user1 = myOpt.setOperator(opt_code);
String opt_name = myOpt.setOpt_provider(selectedItem);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
Image like:
Please help me, thanks.
Chenge xml like this
<?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" >
<Spinner
android:id="#+id/operator_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="7dp"
android:gravity="left"
android:textAlignment="center" >
</Spinner>
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:paddingLeft="38dp"
android:visibility="gone" >
<RadioButton
android:id="#+id/_TopUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TopUp " />
<RadioButton
android:id="#+id/Special"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Special(2G,3G,SMS,VOICE )" />
</RadioGroup>
</LinearLayout>
Related
I have a list view and I want to show some values inside it, the connection is made without any error, but my list view doesn't show those values, I don't know what I did wrong, my code is below please help me. I think I have done some wrong in the customeadapter.. please check my custom adapter CustomAdapterViewDriver.
My Activity
public class PerformBargain extends ActionBarActivity {
ListView UserDetails;
JSONArray jsonArray;
SendPostRequest mSendPostRequest;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perform_bargain);
UserDetails=(ListView)findViewById(R.id.UserDetails);
mSendPostRequest= new SendPostRequest();
mSendPostRequest.execute();
}
class SendPostRequest extends AsyncTask<Void, Void, JSONArray>
{
String postData = "";
HttpURLConnection httpURLConnection= null;
#Override
protected JSONArray doInBackground(Void... params) {
try {
Bundle bundle=getIntent().getExtras();
String id=bundle.getString("ID");
httpURLConnection= (HttpURLConnection) new URL("http://192.168.0.14:8080/GoodsServer/ViewRideDetails.jsp").openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream outputStream= new
DataOutputStream(httpURLConnection.getOutputStream());
outputStream.writeBytes("id=" +id);
outputStream.flush();
outputStream.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char currentData = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
postData += currentData;
}
jsonArray=new JSONArray(postData.trim());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpURLConnection!= null) {
httpURLConnection.disconnect();
}
}
return jsonArray;
}
#Override
protected void onPostExecute(JSONArray s) {
Toast.makeText(getApplicationContext(),postData,Toast.LENGTH_LONG).show();
try{
CustomAdapterViewDriver customAdapterViewDriver=new CustomAdapterViewDriver(PerformBargain.this,s);
UserDetails.setAdapter(customAdapterViewDriver);
}catch (Exception e)
{
System.out.print(e);
}}
}
CustomAdapterViewDriver Class
class CustomAdapterViewDriver extends BaseAdapter implements ListAdapter {
String ids;
TextView name,email,phone,Source,destination;
private final Activity activity;
private final JSONArray jsonArray;
public CustomAdapterViewDriver(Activity activity, JSONArray jsonArray) {
assert activity != null;
assert jsonArray != null;
this.jsonArray = jsonArray;
this.activity = activity;
}
#Override public int getCount() {
return jsonArray.length();
}
#Override public JSONObject getItem(int position) {
return jsonArray.optJSONObject(position);
}
#Override public long getItemId(int position) {
JSONObject jsonObject = getItem(position);
return jsonObject.optLong("id");
}
#Override public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = activity.getLayoutInflater().inflate(R.layout.userviewforbargain, null);
JSONObject jsonObject = getItem(position);
name=(TextView)view.findViewById(R.id.namevu);
phone =(TextView)view.findViewById(R.id.phone);
email=(TextView)view.findViewById(R.id.email);
Source=(TextView)view.findViewById(R.id.sourceadderss);
destination=(TextView)view.findViewById(R.id.destinationaddress);
try{
name.setText(jsonObject.getString("Name"));
phone.setText(jsonObject.getString("Phone"));
email.setText(jsonObject.getString("Email"));
Source.setText(jsonObject.getString("Source"));
destination.setText(jsonObject.getString("DestAdd"));
}catch(Exception e)
{
System.out.print(e);
}
return view;
}
}
userviewforbargain Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Name"
android:textColor="#000"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Phone"
android:textColor="#000"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Email"
android:textColor="#000"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="SourceAdderss"
android:textColor="#000"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Destination"
android:textColor="#000"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="50dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="#+id/namevu"
android:hint="Name"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="Phone"
android:id="#+id/phone"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="Email"
android:id="#+id/email"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="sourceadderss"
android:id="#+id/sourceadderss"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="Destination"
android:id="#+id/destinationaddress"
android:textColor="#000"
/>
</LinearLayout>
</LinearLayout>
the problem was with my XML file... I just add a relative layout to my XML and its working fine now.
code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="com.example.user.good_carriermacfast.PerformBargain"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="110dp"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Name"
android:textColor="#000"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Phone"
android:textColor="#000"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Email"
android:textColor="#000"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="SourceAdderss"
android:textColor="#000"
/>
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Destination"
android:textColor="#000"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="110dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="#+id/namevu"
android:hint="Name"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="Phone"
android:id="#+id/phone"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="Email"
android:id="#+id/email"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="sourceadderss"
android:id="#+id/sourceadderss"
android:textColor="#000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:hint="Destination"
android:id="#+id/destinationaddress"
android:textColor="#000"
/>
</LinearLayout>
</RelativeLayout>
How to show listviewitem when i input a text into Edittext. I searched already from the net but i dont have an idea on how to do it.
This is the code
MainActivity.java
public class MainActivity extends ListActivity {
private ProgressDialog pDialog;
private EditText et,search;
private static final String URL = "http://192.168.254.101/productlist.php";
JSONParser jsonParser = new JSONParser();
private static final String Success ="success";
private static final String TAG_POSTS = "message";
private static final String TAG_BRAND = "Brand";
private static final String TAG_CATEGORY = "Category";
private static final String TAG_DESCRIPTION = "Description";
private static final String TAG_CODE = "Code";
private static final String TAG_QUANTITY = "Quantity";
private static final String TAG_UNIT = "Unit";
private static final String TAG_UNITPRICE = "Unitprice";
private JSONArray mComments = null;
//manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
search = (EditText)findViewById(R.id.EditText01);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//loading the comments via AsyncTask
new LoadComments().execute();
}
public void updateJSONdata() {
int success;
String searchdesc = search.getText().toString();
mCommentList = new ArrayList<HashMap<String, String>>();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", searchdesc));
JSONObject json = jsonParser.makeHttpRequest(
URL, "POST", params);
success = json.getInt(Success);
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
//gets the content of each tag
String brand = c.getString(TAG_BRAND);
String category = c.getString(TAG_CATEGORY);
String description = c.getString(TAG_DESCRIPTION);
String code = c.getString(TAG_CODE);
String quantity = c.getString(TAG_QUANTITY);
String unit = c.getString(TAG_UNIT);
String unitprice = c.getString(TAG_UNITPRICE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_BRAND, brand);
map.put(TAG_CATEGORY, category);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_CODE, code);
map.put(TAG_QUANTITY, quantity);
map.put(TAG_UNIT, unit);
map.put(TAG_UNITPRICE, unitprice);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_BRAND, TAG_CATEGORY,
TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{ R.id.Brand, R.id.Category,
R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price
});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Products...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
}
activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<LinearLayout
android:id="#+id/top_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/EditText01"
android:layout_gravity="center_horizontal"
android:hint="Search.." />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_layover"
android:layout_below="#+id/top_layover"
android:background="#fff"
android:divider="#android:color/transparent"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/bottom_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
a
single_post.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp"
android:background="#ffffff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Brand: ">
</TextView>
<TextView
android:id="#+id/Brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Category: " >
</TextView>
<TextView
android:id="#+id/Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Description: " >
</TextView>
<TextView
android:id="#+id/Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Code: " >
</TextView>
<TextView
android:id="#+id/Code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Quantity: ">
</TextView>
<TextView
android:id="#+id/Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Unit: " >
</TextView>
<TextView
android:id="#+id/Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Price: " >
</TextView>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
this is the php code search.php
<?php
try{
$handler= new PDO('mysql:host=localhost;dbname=account','root','');
$handler->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(Exception $e){
echo $e->getMessage();
die();
}
$description = $_POST['search'];
$query =$handler->query( "Select * from tblproducts where Description like '%$description%' ");
$records= array();
$records =$query->fetchAll(PDO::FETCH_ASSOC);
$json["success"]=1;
$json["message"]=$records;
echo json_encode($json);
?>
ok i edit my answer with your case
please put this line in onCreate method:
setContentView(R.layout.activity_main);
on top of this line
search = (EditText)findViewById(R.id.EditText01);
after that call this method
search = (EditText) findViewById(R.id.EditText01);
txt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
updateJSONdata(s.toString());
}
});
after that change updateJSONdata method first line to this
public void updateJSONdata(String searchdesc){
and comment this line
String searchdesc = search.getText().toString();
maybe this help
This is the code i just want to show the listview item in SlidingTab fragment.
Here sir i already post the single post.xml
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.productlist,container,false);
return v;
}
}
this is for the getting the listviewitem
public class productlist extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
private static final String READ_COMMENTS_URL = "http://192.168.254.101/productlist.php";
private static final String TAG_POSTS = "message";
private static final String TAG_BRAND = "Brand";
private static final String TAG_CATEGORY = "Category";
private static final String TAG_DESCRIPTION = "Description";
private static final String TAG_CODE = "Code";
private static final String TAG_QUANTITY = "Quantity";
private static final String TAG_UNIT = "Unit";
private static final String TAG_UNITPRICE = "Unitprice";
private JSONArray mComments = null;
//manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//loading the comments via AsyncTask
new LoadComments().execute();
}
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
//gets the content of each tag
String brand = c.getString(TAG_BRAND);
String category = c.getString(TAG_CATEGORY);
String description = c.getString(TAG_DESCRIPTION);
String code = c.getString(TAG_CODE);
String quantity = c.getString(TAG_QUANTITY);
String unit = c.getString(TAG_UNIT);
String unitprice = c.getString(TAG_UNITPRICE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_BRAND, brand);
map.put(TAG_CATEGORY, category);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_CODE, code);
map.put(TAG_QUANTITY, quantity);
map.put(TAG_UNIT, unit);
map.put(TAG_UNITPRICE, unitprice);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_BRAND, TAG_CATEGORY,
TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{ R.id.Brand, R.id.Category,
R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(productlist.this);
pDialog.setMessage("Loading Products...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
}
productlist.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<LinearLayout
android:id="#+id/top_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<TextView
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Products"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_layover"
android:layout_below="#+id/top_layover"
android:background="#fff"
android:divider="#android:color/transparent"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/bottom_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
single_post.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp"
android:background="#ffffff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Brand: ">
</TextView>
<TextView
android:id="#+id/Brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Category: " >
</TextView>
<TextView
android:id="#+id/Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Description: " >
</TextView>
<TextView
android:id="#+id/Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Code: " >
</TextView>
<TextView
android:id="#+id/Code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Quantity: ">
</TextView>
<TextView
android:id="#+id/Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Unit: " >
</TextView>
<TextView
android:id="#+id/Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Price: " >
</TextView>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Your tab should extend ListFragment.
public class Tab extends ListFragment{
//your existing code...
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new LoadComments().execute();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.productlist,container,false);
return v;
}
//your existing code...
}
I didn't test the code but it should work.
I am trying to implement view pager,in my view pager images comes from server,I am able to display and scroll but when i reach at last image it shows error and app got crash,following is my snippet code
public class Test_Pager extends Activity{
private String strtd;
String[] imgStr;
ImageView imageView;
ArrayList<String> userImgArrayList;
String[] myURLs;
/*country list*/
JSONArray country_list=null;
private ServiceHandler sh;
private String jsonStr;
private JSONObject jsonObj;
private String user_img;
private String user_pro;
private static String PROFILE_VIEW_URL = "";
private static final String USER_IMG="product_images";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test_pagerss);
userImgArrayList = getIntent().getStringArrayListExtra("user_images");
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
imageView = (ImageView) findViewById(R.id.full_image_view);
PROFILE_VIEW_URL="";
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
sh = new ServiceHandler();
// Making a request to url and getting response
jsonStr = sh.makeServiceCall(PROFILE_VIEW_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
try {
jsonObj = new JSONObject(jsonStr);
/*proview_business = jsonObj.getString(PROFILE_VIEW_BUSINESS);
proview_subcat = jsonObj.getString(PROFILE_VIEW_SUB_CATAGORY);
proview_mainpro = jsonObj.getString(PROFILE_VIEW_MAINPRODUCTS);
proview_expr = jsonObj.getString(PROFILE_VIEW_EXPERIENCE);
proview_cmpname = jsonObj.getString(PROFILE_VIEW_COMPANYNAME);
proview_website = jsonObj.getString(PROFILE_VIEW_WEBSITE);*/
// user_img=jsonObj.getString(USER_IMG);
user_img=jsonObj.getString(USER_IMG);
user_img = "";
userImgArrayList = new ArrayList<String>();//declare userImgArrayList globally like ArrayList<String> userImgArrayList;
JSONArray picarray = jsonObj.getJSONArray(USER_IMG);
for(int i=0;i< picarray.length();i++)
{
user_img = picarray.getString(i);
userImgArrayList.add(user_img);
Log.d("mylog", "curent pro pic = " + user_img);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public class ImageAdapter extends PagerAdapter {
Context context;
ImageAdapter(Context context)
{
this.context=context;
}
#Override
public int getCount() {
return USER_IMG.length();
}
#Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.activity_horizontal_margin);
imageView.setPadding(padding, padding, padding, padding);
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//Picasso.with(context).load(userImgArrayList.get(position)).into(imageView);
Picasso.with(context).load(userImgArrayList.get(position)).resize(200, 200) .into(imageView);
/*for(int i=0; i<myURLs.length;i++)
{
try {
url = new URL(myURLs[i]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(bmp);
}
*/
// imageView.setImageURI(Uri.parse(imgStr[position]));
((ViewPager) container).addView(imageView, 0);
return imageView;
}
}
}
Myxml
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/lnr"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView android:id="#+id/full_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/></LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#drawable/secondpart"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*" android:stretchColumns="*" >
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Product Name"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/txtageprofile"
android:textColor="#android:color/black"
android:layout_marginLeft="5dp"
/></TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Code"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/txtheightprofile"
android:textColor="#android:color/black"
android:layout_marginLeft="5dp"
/></TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Purchase Rate"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/txtmrgprofile"
android:textColor="#android:color/black"
android:layout_marginLeft="5dp"
/></TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Weight"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/txtworkingprofile"
android:textColor="#android:color/black"
android:layout_marginLeft="5dp"
/></TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Sales Rate"
android:layout_marginLeft="5dp"
android:textColor="#android:color/black"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/txtplaceprofile"
android:textColor="#android:color/black"
android:layout_marginLeft="5dp"
/></TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#drawable/secondpart"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical|right"
android:id="#+id/txteatprofile"
android:textColor="#android:color/black"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
USER_IMG.length(); returns the length of the String (the number of chars of "product_images"). You should return the size of the data set instead. Change
#Override
public int getCount() {
return USER_IMG.length();
}
to
#Override
public int getCount() {
if (userImgArrayList == null) {
return 0;
}
return userImgArrayList.size();
}
change your getcount() method to
#Override
public int getCount() {
int count =0
if (userImgArrayList != null) {
count=userImgArrayList.size();
}
return count;
}
And also add notifyDataSetChanged after filling the arraylist like this..
for(int i=0;i< picarray.length();i++)
{
user_img = picarray.getString(i);
userImgArrayList.add(user_img);
Log.d("mylog", "curent pro pic = " + user_img);
}
adapter.notifyDataSetChanged()
replace,
Picasso.with(context).load(userImgArrayList.get(position)).resize(200, 200) .into(imageView);
with,
try{Picasso.with(context).load(userImgArrayList.get(position)).resize(200, 200) .into(imageView);}catch(Exception e){
e.printStackTrace();}
I've a strange problem with my custom spinner. My implementation is to hide the spinner and only fire the choices whenever i click a button so my spinner visibility is set to INVISIBLE and only fire a list of choices and assign the selected value back to a TextView. My code works only in emulator but when i went to my actual device the list of choices is not opened and only the default value is selected back to my textview. I've tried to set spinner visibility to Visible. This time i got the choices. I remember i have another implementation with the visibility is set to INVISIBLE and it worked as desired. Any clues?
XML Layout:
<?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="fill_parent"
android:background="#EEE8E0"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:weightSum="100" >
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#E69100" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="100"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#ffffff"
android:orientation="horizontal" >
<ImageView
android:id="#+id/attach4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/attach" />
<ImageView
android:id="#+id/attach3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/attach" />
<ImageView
android:id="#+id/attach2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/attach" />
<ImageView
android:id="#+id/attach1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/attach" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
<View
android:id="#+id/view1"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_centerInParent="true"
android:background="#222222"
android:visibility="invisible" />
<EditText
android:id="#+id/addtitle"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/selecttype"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:gravity="right|center_vertical"
android:hint="عنوان الأعلان"
android:inputType="text"
android:lines="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" >
</EditText>
<EditText
android:id="#+id/addbrief"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_below="#+id/addtitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:gravity="right|center_vertical"
android:hint="المواصفات"
android:inputType="text"
android:lines="4"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" />
<EditText
android:id="#+id/addprice"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/addbrief"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:gravity="right|center_vertical"
android:hint="السعر"
android:inputType="number"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" />
<Spinner
android:id="#+id/addmoney"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/addbrief"
android:layout_alignTop="#+id/addprice"
android:layout_toLeftOf="#+id/addprice"
android:background="#drawable/edittextback"
android:entries="#array/country_arrays"
android:gravity="center_horizontal|center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:prompt="#string/country_prompt"
android:textColor="#444545"
android:textSize="17dp" />
<EditText
android:id="#+id/addkilometrage"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/addprice"
android:layout_alignRight="#+id/addprice"
android:layout_below="#+id/addprice"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:gravity="right|center_vertical"
android:hint="المسافة المقطوعة"
android:inputType="number"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" />
<Spinner
android:id="#+id/addkilo"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/addmoney"
android:layout_alignTop="#+id/addkilometrage"
android:layout_toLeftOf="#+id/addprice"
android:background="#drawable/edittextback"
android:entries="#array/kilo"
android:gravity="center_vertical|center_horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:prompt="#string/country_prompt"
android:textColor="#444545"
android:textSize="17dp" />
<Spinner
android:id="#+id/Spinner01"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/addkilo"
android:layout_alignRight="#+id/addkilometrage"
android:layout_below="#+id/addkilometrage"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:entries="#array/year"
android:gravity="right|center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:prompt="#string/country_prompt"
android:textColor="#444545"
android:textSize="17dp" />
<EditText
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignLeft="#+id/Spinner01"
android:layout_alignRight="#+id/Spinner01"
android:layout_below="#+id/Spinner01"
android:layout_marginTop="10dp"
android:background="#drawable/edittextback"
android:ems="10"
android:gravity="right|center_vertical"
android:hint="رقم الهاتف/الجوال"
android:inputType="number"
android:lines="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#444545"
android:textSize="17dp" />
<Button
android:id="#+id/continueregist"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignLeft="#+id/EditText01"
android:layout_alignRight="#+id/addprice"
android:layout_below="#+id/EditText01"
android:layout_marginTop="26dp"
android:background="#drawable/green_buttons"
android:text="Continue"
android:textColor="#ffffff"
android:textSize="19dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:visibility="invisible" />
<Spinner
android:id="#+id/spinner_model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:visibility="invisible" />
<ImageView
android:id="#+id/selecttype"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/addtitle"
android:layout_marginTop="20dp"
android:scaleType="fitXY"
android:src="#drawable/sell" />
<TextView
android:id="#+id/textcattype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/spinner_type"
android:layout_marginRight="21dp"
android:layout_toLeftOf="#+id/selecttype"
android:text=""
android:textSize="19dp" />
<ImageView
android:id="#+id/selectmodel"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignRight="#+id/addmoney"
android:layout_alignTop="#+id/selecttype"
android:scaleType="fitXY"
android:src="#drawable/buy" />
<TextView
android:id="#+id/textcarmodel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/selectmodel"
android:layout_toLeftOf="#+id/selectmodel"
android:text=""
android:textSize="19dp" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/mainaddshekh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#222222"
android:orientation="vertical" >
</RelativeLayout>
Java Class:
public class Sell_Class extends Activity implements OnClickListener {
Spinner Type, Model;
MyAdapter adapter1;
MyAdapter2 adapter2;
EditText cartypeedit;
ImageView cartype, carmodel;
TextView cartypetext, carmodeltext;
String CarType="",CarModel;
int arr_images[] = {
R.drawable.hy,
R.drawable.hy
};
final String[] CarsType = new String[] {
"هيونداي", "هوندا",
};
final String[] Hyundai = new String[] {
"أكسنت", "أكسيل"
};
final String[] Honda = new String[] {
"أكورد", "سيفيك",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainsell);
Type = (Spinner) findViewById(R.id.spinner_type);
Model = (Spinner) findViewById(R.id.spinner_model);
cartype = (ImageView) findViewById(R.id.selecttype);
carmodel = (ImageView) findViewById(R.id.selectmodel);
cartypetext = (TextView) findViewById(R.id.textcattype);
carmodeltext = (TextView) findViewById(R.id.textcarmodel);
cartype.setOnClickListener(this);
carmodel.setOnClickListener(this);
Type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// get spinner value
cartypetext.setText(CarsType[arg2]);
CarType = CarsType[arg2];
CarModel="";
carmodeltext.setText("");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Model.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (CarType.contentEquals("هيونداي")) {
carmodeltext.setText(Hyundai[arg2]);
CarModel = Hyundai[arg2];
}
if (CarType.contentEquals("هوندا")) {
carmodeltext.setText(Honda[arg2]);
CarModel = Honda[arg2];
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.selecttype:
adapter1 = new MyAdapter(this, CarsType, arr_images);
Type.setAdapter(adapter1);
Type.performClick();
cartypetext.setText("");
break;
case R.id.selectmodel:
if (CarType.contentEquals("")) {
Toast.makeText(getApplicationContext(),
"Please select car first", Toast.LENGTH_LONG).show();
}
else
{
if (CarType.contentEquals("هيونداي")) {
adapter2 = new MyAdapter2(Sell_Class.this, Hyundai);
Model.setAdapter(adapter2);
Model.performClick();
carmodeltext.setText("");
}
if (CarType.contentEquals("هوندا")) {
adapter2 = new MyAdapter2(Sell_Class.this, Honda);
Model.setAdapter(adapter2);
Model.performClick();
carmodeltext.setText("");
}
}
break;
}
}
}
MyAdapter:
public class MyAdapter extends BaseAdapter {
Context c;
String[] title;
int[] pic;
LayoutInflater inflater;
Typeface font;
int arr_images[] = {
R.drawable.hy,
R.drawable.hy
};
public MyAdapter(Context context, String[] title, int[] pic) {
super();
this.c = context;
this.title = title;
this.pic = pic;
inflater = LayoutInflater.from(context);
// font = Typeface.createFromAsset(context.getAssets(),"fonts/khalaadsara.ttf");
}
#Override
public int getCount() {
return title.length;
}
public class ViewHolder {
TextView cat;
ImageView icon;
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView== null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.spinnertext, null);
holder.cat = (TextView) convertView.findViewById(R.id.titlesub);
holder.icon = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.cat.setText(title[position]);
// holder.cat.setTypeface(font);
holder.icon.setImageResource(arr_images[position]);
return convertView;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
}