I am using volley to get some JSON data.
How do I use the image src to populate my ImageView? Below response is the json data from dribbble. http://api.dribbble.com/shots/everyone?
The data will give me an image source
"image_url":"https://d13yacurqjgara.cloudfront.net/users/24831/screenshots/2112992/2015-06-18_19.51.18_copy.jpg"
How can I use that image source inside my ImageView
// BUILD THE ARRAY
JSONArray shots = response.getJSONArray("shots");
List<String> titles = new ArrayList<String>();
for (int i=0; i < shots.length(); i++) {
JSONObject post = shots.getJSONObject(i);
String title = post.getString("title");
titles.add(title);
}
// You can see I am just taking that data and turning it into strings..
String[] titleArr = new String[titles.size()];
titleArr = titles.toArray(titleArr);
// SET THE ADAPTER
ListAdapter dribbbleFeedAdapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.feed_card,R.id.info_text,titleArr);
// SET THE LISTVIEW
ListView dribbbleDataListView = (ListView) findViewById(R.id.dribbbleFeedListView);
// SET THE LISTVIEW ADAPTER
dribbbleDataListView.setAdapter(dribbbleFeedAdapter);
// CHECK THE DATA
Log.d("this is my array", "arr: " + Arrays.toString(titleArr));
feed_card.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/card_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_weight="1"
android:id="#+id/info_text"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#aaa"
android:id="#+id/dribbble_img"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like" />
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Related
how to create this kind of layout in android
i have tried using table layout. but getting error in creating this.
please check my code i am able to display images. 1 image in 1 row. but i want 2 small images in 1 row. and continues.
here is my code
private void getBannerList() {
final Call<BannerList_Model> itemMainResponse = APIHandler.getApiService().bannerList(getFiledMapForItemList());
itemMainResponse.enqueue(new Callback<BannerList_Model>() {
#Override
public void onResponse(Response<BannerList_Model> response, Retrofit retrofit) {
BannerList_Model itemResponse = null;
itemResponse = response.body();
if (itemResponse == null) {
UIUtils.showToast(getContext(), "Server Error !");
} else {
String msg = itemResponse.getMsg().toString();
if (msg.equals("success")) {
userList = itemResponse.getBannerList();
if(userList != null) {
TableLayout table = (TableLayout) view.findViewById(R.id.tblBanner_list);
table.setColumnStretchable(0,true);
table.setColumnStretchable(1,true);
int tsize = userList.size();
TextView[] t1 = new TextView[tsize];
ImageView[] img = new ImageView[tsize];
for(int i=0; i<userList.size();i++)
{
TableRow row = new TableRow(getContext());
String banner_size = userList.get(i).getBannerSize().toString();
img[i] = new ImageView(getContext());
Picasso.with(getContext())
.load(Constants.VIDEO_URL + userList.get(i).getBannerUrl())
.into(img[i]);
row.addView(img[i]);
table.addView(row);
}
}
}
}
}
}
});
}
here is the video how i want layout
http://shrivivah.com/layout.mp4
if there is 2 small images than it will display in 1 row.
if there is 2 medium or full size image than it will display in 1 row each.
if 1 small and 2 medum than 1 image per row to display.
layout image
Your mistake is that you are adding TextView to TableRow(row) but you didn't add row to TableLayout. Here is an example for what you need. You can set some margins and add button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</TableRow>
</TableLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.tops.stackoverflow.MainActivity">
<LinearLayout
android:id="#+id/llShortcutParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
I'll start showing you the code.
lv = (ListView) findViewById(android.R.id.list);
//Updating parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(
this, locationsList,
R.layout.list_results, new String[]{
TAG_NATION, TAG_CITY, TAG_NAME, TAG_TYPE, TAG_PRICERANGE, TAG_ID, TAG_IMAGE},
new int[]{R.id.tvResNation, R.id.tvResCity, R.id.tvResName, R.id.tvResType, R.id.tvResPrice, R.id.tvResID,
R.id.ivResImage1});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent in = new Intent(ResultsActivity.this, LocationActivity.class);
String resID = ((TextView) view.findViewById(R.id.tvResID)).getText().toString();
String resName = ((TextView) view.findViewById(R.id.tvResName)).getText().toString();
String resNation = ((TextView) view.findViewById(R.id.tvResNation)).getText().toString();
String resCity = ((TextView) view.findViewById(R.id.tvResCity)).getText().toString();
String resType = ((TextView) view.findViewById(R.id.tvResType)).getText().toString();
in.putExtra(TAG_ID, resID);
in.putExtra(TAG_NAME, resName);
in.putExtra(TAG_TYPE, resType);
in.putExtra(TAG_NATION, resNation);
in.putExtra(TAG_CITY, resCity);
startActivity(in);
}
});
All the properties that we set on the adapter are retrieved by a JSONObject that we convert to an hashmap (we have verified that all the properties have the right value).
Once the properties are set (?) on the adapter using that hashmap the property resID apparently is not. From the textView with id R.id.tvResID we always receive an empty string using getText().
The funny thing is that all the properties have a value before the setting of the adapter and all the other properties, except ID, are retrieved inside the listener...
Here's the XML for the adapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.66">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResNation"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResCity"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResName"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResType"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResPrice"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ivResImage1"
android:layout_weight="0.33" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResID"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textColor="#000000"/>
</LinearLayout>
SOLVED
We set the adapter in another part of the code and there was a field missing...
You can try like ,
TextView txtView = (TextView) view.findViewById(R.id.tvResID);
String str = txtView.getText().toString();
Log.i("TAG", "Text is : " + str);
This should work.
EDIT 1
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvResID"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:text="hello world!!!"
android:textColor="#000000"/>
After this you will get
hello world!!!
Try this code:
String text = (String) mListView.getItemAtPosition(position);
I need to make the image that I get from database circular
I have made this code but I don't know how to complete it to make the image circular
please help
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
name = (TextView) findViewById(R.id.names);
date = (TextView) findViewById(R.id.dates);
Story = (TextView) findViewById(R.id.story);
im = (ImageView) findViewById(R.id.img_car);
im.setScaleType(ImageView.ScaleType.FIT_XY);
//ScrollView scroller = new ScrollView(this);
//scroller.addView(Story);
Bundle extras = getIntent().getExtras();
int idf = extras.getInt("index");
Book selectedBook;
db = new SQLhelper(getApplicationContext());
// read the book with "id" from the database
selectedBook = db.readBook(++idf);
name.setText(selectedBook.getTitle());
date.setText(selectedBook.getdate());
Story.setText(selectedBook.getstory()+"\n \n ");
pname = selectedBook.getTitle();
test = selectedBook.getimage();
int id = getResources().getIdentifier("com.example.tarekhy:drawable/" + test, null, null);
im.setImageResource(id);
}
and this is xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:background="#drawable/customborder">
<ImageView
android:id="#+id/img_car"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:contentDescription="#null"
android:gravity="center_vertical"
android:onClick="viewbig"
/>
<RelativeLayout
android:id="#+id/imgs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/layout_car_model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/names"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:text="gfhbj"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_car_color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_car_model"
android:orientation="horizontal" >
<TextView
android:id="#+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<ScrollView android:id="#+id/textAreaScroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_x="0px"
android:layout_y="25px"
android:scrollbars="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="#+id/fbedittext"
android:layout_marginTop="22dp"
android:id="#+id/story"
android:textStyle="bold"
android:layout_marginBottom="20px"
/>
</ScrollView>
"
</LinearLayout>
I think that I need to make a style
I tried to do this but I failed
Help please
You Need CircularImageView From CircularImageView
in xml file You have To Replace this code instead of Imageview :-<de.hdodenhof.circleimageview.CircleImageView
android:id ="#+id/img_car"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true"
app:border_width="2dp"
app:border_color="#color/dark" />
In Activity File Replace This Code instead of Imageview:-
CircleImageView im= (CircleImageView) findViewById(R.id.img_car);
I want to display only 5 rows in a ListView and rest of them should be scrollable.So far i have tried this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/txtFilename"
android:layout_width="375dp"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="2dp"
android:
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
android:padding="5dp" >
<Button
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"/>
<Button
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CANCEL"/>
</LinearLayout>
</LinearLayout>
and in java code
public class MainActivity extends Activity {
private ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",
"item 1", "item 2", "item3", "item 4" };
#SuppressWarnings("unchecked")
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//parentLayout.add(childLayout);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in
// list.
list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));
}
have any idea how to achieve it
Here i am Just Modifying Your Layout just check it out!!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/txtFilename"
android:layout_width="375dp"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="300dip">
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="2dp"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
android:padding="5dp" >
<Button
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"/>
<Button
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CANCEL"/>
</LinearLayout>
</LinearLayout>
I have written Demo for this Like:
if you want to fix Height of ListView to 5 items. then you have to set Height of ListView pragmatically by calculating listItems height.
First you need to create Layout for List Item and give it some fixed height, here i'm added height in Tag cause runtime i was getting layouts height as 0.
This is ListItem Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:background="#android:color/black"
android:tag="40"
android:layout_height="40dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXX"
android:textColor="#android:color/darker_gray"
android:textSize="30sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/text"
android:text="ZZZ"
android:textColor="#android:color/darker_gray"
android:textSize="30sp" />
</RelativeLayout>
and to set Height of ListView:
list = (ListView) findViewById(R.id.listView1);
adapter = new Adapter(MainActivity.this, setter);
list.setAdapter(adapter);
RelativeLayout.LayoutParams lstViewParams = (LayoutParams) list.getLayoutParams();
View lisItemInflatedView = View.inflate(this, R.layout.item, null);
RelativeLayout relLayoutInLstItem = (RelativeLayout) lisItemInflatedView.findViewById(R.id.relativeLayout);
final float scale = this.getResources().getDisplayMetrics().density;
int pixels = (int) ( Integer.parseInt( relLayoutInLstItem.getTag().toString() ) * scale + 0.5f);
int listHeigt = pixels * 5 + 5;
lstViewParams.height = listHeigt;
here's the screenshot..
Hope this will Help.
I am trying to create search activity. The layout contains EditText (to type search query) Button & ListView (to show search results). search_layout.xml (For showing searchbox & ListView) is as follow :
<?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="wrap_content"
android:orientation="horizontal">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText_search"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/search_practice" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_search"
android:onClick="searchPractice" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/praclist"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
And layout for list items list_items.xml is :
<?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="wrap_content"
android:orientation="horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Name Label -->
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="6dip"
android:paddingBottom="2dip" />
<!-- Description label -->
<TextView
android:id="#+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="#+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
<!-- Linear layout for cost and price Cost: Rs.100 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Cost Label -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:gravity="left"
android:textStyle="bold"
android:text="Phone: " >
</TextView>
<!-- Price Label -->
<TextView
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#acacac"
android:textStyle="bold"
android:gravity="left">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
On button click (searchBtnClick) I am calling method doMySearch :
public void searchBtnClick(View view){
-------
------
ListAdapter adapter = doSearch(query);
ListView pracListView = (ListView) findViewById(R.id.praclist);
PracListView.setAdapter(adapter);
return;
}
private ListAdapter doSearch(String query){
ListAdapter adapter = null;
String url = "https://mywebservice.com/apps/api/v1/search/practices?val="+query+"&by=nam";
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
String jsonResponsse = null;
try{
HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
HttpEntity httpEntity = httpResponse.getEntity();
jsonResponsse = getASCIIContentFromEntity(httpEntity);
Gson gson = new Gson();
PracticesByName practicesByName = gson.fromJson(jsonResponsse,PracticesByName.class);
ArrayList<Practices> practiceList = (ArrayList<Practices>)practicesByName.getPractices();
for(int i=0 ; i < practiceList.size() ; i ++){
Object a = practiceList.get(i);
System.out.println(a.getClass() + " " + a.toString());
Practices aPractice = practiceList.get(i);
HashMap<String, String> aMap = new HashMap<String, String>();
aMap.put(TAG_NAME, aPractice.getName());
aMap.put(TAG_CITY, aPractice.getCity());
aMap.put(TAG_EMAIL, aPractice.getEmail());
aMap.put(TAG_PHONE, aPractice.getPhone());
searchResultList.add(aMap);
}
adapter = new SimpleAdapter(this, searchResultList,
R.layout.list_item_prac_search,
new String[] { TAG_NAME, TAG_CITY, TAG_EMAIL, TAG_PHONE}, new int[] {
R.id.name, R.id.city, R.id.email, R.id.phone});
//pracListView.setAdapter(adapter);
}catch (Exception e){
System.out.println("Exception while getting serach for practice");
e.printStackTrace();
}
return adapter;
}
The code is running fine. I am getting search result from web service. But don't see any list below. Any idea what I am messing up?
You should first create an array lis to fetch the recorde from the query,
ArrayList<String> items = new ArrayList<String>();
items.add(doSearch(query));
Then replace this line,
ListAdapter adapter = doSearch(query);
with this line,
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,items);
ListView pracListView = (ListView) findViewById(R.id.praclist);
PracListView.setAdapter(adapter);
Finally, Got it working. Corrected search_layout.xml as follow.
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText_search"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/search_practice"
android:imeOptions="actionDone" />
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/button_search"
android:onClick="searchPractice" />
</LinearLayout>
<ListView
android:id="#+id/praclist"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
making outer layout vertical & placing horizontal layout (containing EditText & Search Button in it) and ListView in it.
Thanks for your time.