My gridview shows only one row - android

//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&apos;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&apos;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

Related

Custom ListView show only one record

Hello my customlist view shows only one record. Everything is fine it shows multiple records in console but when ever i try to show the records in ListView it shows only last record its overriding first record, currently there are two records which are showing in my console but in ListView it shows only last record, I am searching for solution since yesterday , I have spend more then 6 hours to solve this problem.
Cart_Adapter.java
public class Cart_Adapter extends BaseAdapter {
Activity activity;
ArrayList<ProductBean> list;
String abc;
public Cart_Adapter(Activity activity, ArrayList<ProductBean> list) {
this.activity = activity;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v=view;
if(v==null){
LayoutInflater layoutInflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=layoutInflater.inflate(R.layout.cartxml,null);
}
TextView title=(TextView) v.findViewById(R.id.title);
TextView size=(TextView) v.findViewById(R.id.size);
TextView color=(TextView) v.findViewById(R.id.color);
Button remove=(Button) v.findViewById(R.id.remove);
ImageView imageView=(ImageView) v.findViewById(R.id.imageView);
title.setText(list.get(i).getTitle());
size.setText(list.get(i).getSize());
color.setText(list.get(i).getColor());
ArrayList<ImagesBean> aa =list.get(i).getImagesList();
if(aa!=null){
for(i=0; i<aa.size(); i++) {
abc = String.valueOf(aa.get(i).getImgone());
System.out.println("--testing" + abc);
}
}
System.out.println("===="+abc);
if(abc !=null ) {
imageView.setImageBitmap(getBitmapFromString(abc));
// imageView.setImageBitmap(getBitmapFromString(list.get(i).getImagesList()));
}else{}
return v;
}
private Bitmap getBitmapFromString(String encode){
byte[] b = Base64.decode(encode,Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b,0,b.length);
}
}
class from where I am getting database record
ArrayList<String> arrayList=mydatabase.getpid(userid);
System.out.println("--outside"+arrayList);
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
}
Activity_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bhm.ishopping.Cart">
<android.support.constraint.ConstraintLayout
android:id="#+id/customlayout"
android:layout_width="410dp"
android:layout_height="63dp"
android:layout_marginStart="8dp"
android:background="#39a423"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.617"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView15"
android:layout_width="86dp"
android:layout_height="29dp"
android:layout_marginStart="320dp"
android:textColor="#android:color/holo_blue_bright"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="16dp" />
</android.support.constraint.ConstraintLayout>
<TextView
android:id="#+id/textView19"
android:layout_width="171dp"
android:layout_height="34dp"
android:layout_marginEnd="120dp"
android:layout_marginTop="76dp"
android:text="Your Cart Detail"
android:textColor="#android:color/black"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="#+id/lv"
android:layout_width="373dp"
android:layout_height="411dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/customlayout" />
<Button
android:id="#+id/submit"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_marginStart="76dp"
android:layout_marginTop="40dp"
android:text="Submit Your Order"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/lv" />
</android.support.constraint.ConstraintLayout>
Cart.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="99dp"
android:layout_height="87dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/remove"
android:layout_marginStart="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView"
android:layout_marginStart="15dp"
android:layout_toEndOf="#+id/imageView"
android:text="Ttile"
android:textColor="#android:color/background_dark"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.456"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/textView16"
android:layout_marginTop="102dp"
android:text="Color"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView16" />
<TextView
android:id="#+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/textView17"
android:layout_below="#+id/textView17"
android:text="Size"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView17" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/size"
android:layout_alignTop="#+id/remove"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignStart="#+id/size"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title" />
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_above="#+id/textView18"
android:layout_marginEnd="26dp"
android:layout_toStartOf="#+id/remove"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/color" />
<Button
android:id="#+id/remove"
android:layout_width="46dp"
android:layout_height="67dp"
android:layout_alignBottom="#+id/size"
android:layout_alignParentEnd="true"
android:layout_marginEnd="13dp"
android:background="#5d8f37"
android:text="X"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
console output show two records
enter code here System.out: ===pids1 System.out: ==database record[ProductBean{id='', title='ddd', color='dff', size='Select Size', type='Shart', cost='ddf', imagesList=null System.out: ===pids2 System.out: ==database record[ProductBean{id='', title='aa', color='aa', size='Select Size', type='Select Type', cost='aa', imagesList=null}]
You need to make your cartxml hight to android:layout_height="wrap_content"
like below code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="wrap_content">
EDIT
ArrayList<ProductBean> list= new ArrayList();
ArrayList<String> arrayList=mydatabase.getpid(userid);
System.out.println("--outside"+arrayList);
myadapter = new Cart_Adapter(Cart.this, list);
lv.setAdapter(myadapter);
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
list.addAll(mydatabase.getproduct_details(pid));
}
}
myadapter.notifyDataSetChanged().
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
You are setting adapter inside loop .Which is totally wrong . Put this code outside Loop .
And one more thing Its time to upgrade to RecyclerView . Its easy and better implementation of ViewHolder Pattern .
ArrayList<String> arrayList=mydatabase.getpid(userid);
ArrayList<String> dataList=new ArrayList();;
for(int i =0; i<arrayList.size(); i++) {
String l = arrayList.get(i);
final String ll = l.toString().replace("[", "").replace("]", "").replace(" ", "");
System.out.println("--before loop" + ll);
for (String s : ll.split(",")) {
// Do your stuff add data to list here
dataList.add(stringToAdd);
}
}
myadapter = new Cart_Adapter(Cart.this, dataList);
lv.setAdapter(myadapter);
Do this way and if your dataset is not building properly then i thing you are using wrong dataset for your list . Or probably you need an ExpandableListView . Figure out the dataSet first currently which is 'ArrayList'.
Print your list size in getCount(), to be sure if list is being sent inside. Check your item's layout_height, it should be wrap_content.
Because you are setting adapter each items in loop.
for (String s : ll.split(",")) {
int pid = Integer.parseInt(s);
myadapter = new Cart_Adapter(Cart.this, mydatabase.getproduct_details(pid));
lv.setAdapter(myadapter);
}
}
so it will take last pid from the iteration and will fetch product details which you are setting in card adapter.
Wrong part is: you are calling setAdapter() with each iteration in the loop
Solution:
Collect the list of records in loop, you can keep adding product details in the array list and then set the final list outside loop in the adapter.
Check your cartxml layout file. The root RelativeLayout height will be wrap_content otherwise it will take all the available space for a single item. Use android:layout_width="wrap_content" for RelativeLayoutin in your cartxml

Solved: ListView not getting scrolled smoothly and it stucks/crashes while scrolling even with ViewHolder

Here I'm getting lots of data from web services and I have attached an image of list item too. But my ListView gets stuck or crash when I scroll it. It has lots of data as you can see. I've tried many things but nothing is worth to me.
MainActivity.java
lv = (ListView) findViewById(R.id.script_list);
lv.setScrollingCacheEnabled(false);
lv.setFastScrollEnabled(true);
//AsynTask
public class PostReview extends AsyncTask<Void, Void, String> {
ProgressBar progressBar;
String posted_by = "";
public PostReview(Context con, String item, ProgressBar progressBar) {
this.posted_by = item;
this.progressBar = progressBar;
}
//
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
/*pDialog = new ProgressDialog(ScriptViewClick.this, AlertDialog.THEME_HOLO_DARK);
pDialog.setMessage("Please Wait ..... ");
pDialog.setCancelable(false);
pDialog.show();*/
// progressBar.setVisibility(View.VISIBLE);
}
#Override
protected String doInBackground(Void... params) {
return getString();
}
private String getString() {
// TODO Auto-generated method stub
String POST_PARAMS = "adivsor_id=" + posted_by;
URL obj = null;
HttpURLConnection con = null;
try {
obj = new URL(Constants.AppBaseUrl + "/advisor_calls/");
String userPassword = "rickmams" + ":" + "advisor11";
String header = "Basic " + new String(android.util.Base64.encode(userPassword.getBytes(), android.util.Base64.NO_WRAP));
con = (HttpURLConnection) obj.openConnection();
con.addRequestProperty("Authorization", header);
con.setRequestMethod("POST");
// For POST only - BEGIN
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Log.i("TAG21", response.toString());
if (response.toString() != null) {
JSONObject jsonObject;
try {
jsonObject = new JSONObject(response.toString());
JSONArray js = jsonObject.getJSONArray("list");
for (int ii = 0; ii < js.length(); ii++) {
JSONObject c = js.getJSONObject(ii);
EquityDetails allDirectory = new EquityDetails();
allDirectory.setEntry_value(c.getString("entry"));
String value1 = c.getString("entry");
allDirectory.setCall_id(c.getString("call_id"));
String value2 = c.getString("tgt_1");
allDirectory.setSerial_value(c.getString("sl"));
allDirectory.setTg_value1(c.getString("tgt_1"));
allDirectory.setTg_value2(c.getString("tgt_2"));
allDirectory.setMainTitle_value(c.getString("script"));
allDirectory.setMain_subTitle_value(c.getString("exchange"));
allDirectory.setRating_value(c.getString("rating"));
allDirectory.setReview_value(c.getString("review"));
allDirectory.setPosted_by(c.getString("posted_by"));
allDirectory.setImage1(c.getString("advisor_image"));
allDirectory.setImage2(c.getString("script_image"));
allDirectory.setBuy(c.getString("buy_sentiment"));
allDirectory.setSell(c.getString("sell_sentiment"));
allDirectory.setRecommend(c.getString("recommendation"));
allDirectory.setPosted_date(c.getString("posted_date"));
allDirectory.setExpiry_date(c.getString("expiry_date"));
catListDao.add(allDirectory);
}
sca = new ScriptViewAdapter(getApplicationContext(), catListDao);
lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
} catch (JSONException e) {
e.printStackTrace();
}
}
return response.toString();
} else {
Log.i("TAG12", "POST request did not work.");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
lv.setAdapter(sca);
}
}
BaseAdapter of the class
public ScriptViewAdapter(Context com, List<EquityDetails> list) {
this.con = com;
this.items = list;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) con
.getSystemService(con.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.script, null);
holder = new ViewHolder();
holder.tv_rating_title = (TextView) convertView
.findViewById(R.id.script_tv_rating_title);
} else
holder = (ViewHolder) convertView.getTag();
Picasso.with(con)
.load(items.get(position).getImage1())
.into(holder.scriptView);
Picasso.with(con)
.load(items.get(position).getImage2())
.into(holder.advisoryView);
// new DownloadImageTask(holder.advisoryView).execute(items.get(position).getImage1());
// new DownloadImageTask(holder.scriptView).execute(items.get(position).getImage2());
return convertView;
}
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:layout_marginTop="#dimen/percentage_text_top_margin"
android:background="#android:color/black"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="#+id/script_elements_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="2dp"
android:layout_marginTop="#dimen/percentage_text_top_margin"
android:orientation="horizontal">
<TextView
android:id="#+id/script_tv_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/et_topPadding"
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_top_text"
android:textStyle="bold" />
<TextView
android:id="#+id/script_tv_mcx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_top_text"
android:textStyle="bold" />
<TextView
android:id="#+id/script_tv_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/green"
android:textSize="#dimen/home_screen_main_text" />
<TextView
android:id="#+id/script_tv_rating"
android:layout_width="#dimen/rating_number_bg_width"
android:layout_height="#dimen/rating_number_bg_height"
android:layout_marginLeft="#dimen/percentage_text_top_margin"
android:layout_marginRight="#dimen/percentage_text_top_margin"
android:background="#drawable/rating_bg"
android:gravity="center"
android:paddingRight="5dp"
android:text="50"
android:textColor="#FCD730"
android:textSize="#dimen/home_screen_top_text" />
<TextView
android:id="#+id/script_tv_review_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/percentage_text_top_margin"
android:text=" Review "
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
<RatingBar
android:id="#+id/script_rating"
android:layout_width="wrap_content"
android:layout_height="#dimen/rating_bar_height"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/percentage_text_top_margin"
android:layout_marginRight="#dimen/percentage_text_top_margin"
android:clickable="false"
android:focusable="false"
android:isIndicator="true"
android:numStars="5"
android:progressDrawable="#drawable/custom_star_rating" />
<TextView
android:id="#+id/script_tv_rating_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Rating"
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
<TextView
android:id="#+id/script_tv_rating_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/percentage_text_top_margin"
android:paddingLeft="5dp"
android:text="3"
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_main_text" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/color_bg_drawer_first">
<ImageView
android:id="#+id/script_iv_logo"
android:layout_width="#dimen/home_screen_image_width"
android:layout_height="#dimen/home_screen_image_height"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dp"
android:background="#drawable/script_img"
android:scaleType="fitXY" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/script_layout_company_images"
android:layout_toRightOf="#+id/script_iv_logo"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tv_entry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/entry"
android:textColor="#color/green"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
<TextView
android:id="#+id/script_tv_entry_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:text="105.35"
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
<View
android:layout_width="#dimen/view_line_width"
android:layout_height="#dimen/view_line_height"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/text_color"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tv_serial_line"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/serial"
android:textColor="#color/green"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
<TextView
android:id="#+id/script_tv_serial_line_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:text="452.00"
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
<View
android:layout_width="#dimen/view_line_width"
android:layout_height="#dimen/view_line_height"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#color/text_color"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_tgt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tgt"
android:textColor="#color/green"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
<TextView
android:id="#+id/script_tv_tgt_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:text="104"
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/script_tv_sentimenst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/edittext_side_margin"
android:text="#string/sentiments"
android:textColor="#color/text_color"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:id="#+id/script_layout_company_images"
android:layout_width="wrap_content"
android:layout_height="#dimen/home_screen_image_small_height"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/script_iv_icon_sell"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff1919"
android:gravity="center"
android:inputType="textCapCharacters"
android:text="recommedn"
android:textColor="#FFF"
android:textSize="#dimen/home_screen_main_text"
android:textStyle="bold" />
<ImageView
android:id="#+id/script_iv_icon_company"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/small_script"
android:scaleType="fitXY" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="2dp"
android:background="#color/color_bg_drawer_first" />
</LinearLayout>
The stacktrace when app crashes on scroll is
12-12 13:15:58.188 9104-9104/com.package E/filemap: mmap(0,154425) failed: Out of memory
12-12 13:15:58.188 9104-9104/com.package E/InputEventReceiver: Exception dispatching input event.
12-12 13:15:58.188 9104-9104/com.package E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
12-12 13:15:58.218 9104-9104/com.package E/MessageQueue-JNI: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:334)
at android.graphics.Typeface.createFromAsset(Typeface.java:308)
at com.package.adapters.ScriptViewAdapter.getView(ScriptViewAdapter.java:116)
at android.widget.AbsListView.obtainView(AbsListView.java:2712)
at android.widget.ListView.makeAndAddView(ListView.java:1811)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillGap(ListView.java:661)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:6686)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3920)
at android.widget.AbsListView.onTouchMove(AbsListView.java:4772)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:4600)
at android.view.View.dispatchTouchEvent(View.java:8135)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2416)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2140)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2422)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2155)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2295)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1622)
at android.app.Activity.dispatchTouchEvent(Activity.java:2565)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2243)
at android.view.View.dispatchPointerEvent(View.java:8343)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4767)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4633)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4191)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4245)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4214)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4325)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4222)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4382)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4191)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4245)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4214)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4222)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4191)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6556)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6473)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6444)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6409)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6636)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQue
Note: The size of images are 128X66 and 259X194 pixels.
Accordingly to the code you posted here:
Move all the setTypeFace into the if guard. It is a pretty expensive call and you don't need to do it continuously during the scroll.
Avoid to instantiate SimpleDateFormat every time getView is called. It would better to provide the date in the dataset already in the correct format
Declare the ViewHolder as static: public static class ViewHolder
Make sure the image size is not bigger and should be render fine.
Second, Do not load complete list in one go. Everybody prefer to read make 10 records and load on demand if needed more.
Its a list view and you may load while scrolling.
I always strongly recommended that follow some standards which makes your application faster.

How to get two textviews in a listview on the same line?

I have two TextViews tvA and tvB which appear in a ListView lvA.
tvA and tvB display data pulled from a Sqlite Database using Cursor.
Cursor cursor = myDb.getAllRows();
String[] from = new String[]
{DBAdapter.KEY_A,DBAdapter.KEY_B};
int[] to = new int[]
{R.id.tvA,R.id.tvB};
SimpleCursorAdapter myCursorAdapter =new SimpleCursorAdapter(this,R.layout.lvA, cursor,from,to);
myList = (ListView) findViewById(R.id.lvA);
myList.setAdapter(myCursorAdapter);
registerForContextMenu(myList);
The issue is that tvA and tvB doesn't appear as one item in lvA (i.e. they don't appear on the same line) and appear as two different items.
Below is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<TextView
android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_toRightOf="#id/tvA"/>
</RelativeLayout>
If you want both the TextViews in the same align then you can use LinearLayout with android:layout_weight attribute.
To have equal width TextView, give same weight value.
You can use TableLayout
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<TextView android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
</TableRow></TableLayout >
or LinearLayout with android:orientation="horizontal"
and TextView with android:layout_weight="1"
Here's how I do it with three items on the same row = This is my item list filled from a cursor
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView_StudentName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_gravity="left"
android:text="#string/student_id"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:gravity="center"
android:text="#string/student_Location"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_alignParentRight="true"
android:text="#string/student_class" >
</TextView>
</RelativeLayout>`
I'm in the process of replacing the hard sizes with info from the dimen file, but so far this has worked.
TRY this :
Main Java:
//DECLARE:
private Map<String, Map<String, String>> mMenuView = new HashMap<String, Map<String, String>>();
//IN onCreateView
Adapter mAdapter = new NewAdapter(getActivity().getApplicationContext(), mMenuView);
mListView.setAdapter(mAdapter);
// ADD onActivityCreated or onStart save data from your DB and store in mMenuView
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("TEXTVALE_A", datafromdb_A);
tempMap.put("TEXTVALE_B", datafromdb_B);
mMenuView.put(Integer.toString(mMenuView.size()), tempMap);
}
//NEWADAPTER
private class NewAdapter extends BaseAdapter {
private Context mContext;
private TextView mTitle1;
private TextView mTitle2;
private Map<String, Map<String, String>> mData = new HashMap<String, Map<String, String>>();
public NewAdapter(Context applicationContext,Map<String, Map<String, String>> mMenuValue) {
mContext = applicationContext;
mData = mMenuValue;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Map<String, String> getItem(int position) {
return mData.get(Integer.toString(position));
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Map<String, String> data = mData.get(Integer.toString(position));
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.linear_layout, null);
mTitle1 = (TextView) convertView.findViewById(R.id.t1A);
mTitle2 = (TextView) convertView.findViewById(R.id.t1B);
}
if (data != null) {
mTitle1.setText(data.get("TEXTVALE_A"));
mTitle2.setText(data.get("TEXTVALE_B"));
}
return convertView;
}
}
linear_layout.xml
<LinearLayout
android:id="#+id/content_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/tab1_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center|center_horizontal"
>
<TextView
android:id="#+id/t1A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
android:textColor="#color/text_color3x"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="0.5"
android:orientation="vertical"
android:gravity="left|center_vertical|center_horizontal"
>
<TextView
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
/>
</LinearLayout>
</LinearLayout>

ListView Selction in Android?

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

Android: Alignment/ Layout issues on using different Data Structures

I have a custom listview in my application. The needs of the user varies with the option he selects and the result is on the listview. I am representing the same list once by using ArrayList<String> and other time, while using ArrayList<HashMap<String,String>>. Now, while using the former i.e. ArrayList<String>, the result in the listView row gets aligned perfectly but then, while using the later i.e. ArrayList<HashMap<String,String>> the alignment gets worse. I am not understanding what the problem can be. Please help!
Below I am posting the images, code and source code of the XML which is used as a row in my custom Listview.
Images Link: (As I am new user I cannot upload images)
Using ArrayList<String>
http://i46.tinypic.com/2r2b0g5.jpg
Using ArrayList<HashMap<String, String>>
http://i47.tinypic.com/11w7tqe.jpg
Code:
For Arraylist< HashMap< String, String>>
ArrayList<HashMap<String,String>> searchList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<productName.size();i++)
{
String currentListItem=productName.get(i).toString();
if(textLength<=currentListItem.length()){
//compare the String in EditText with Names in the ArrayList
if(searchText.equalsIgnoreCase(currentListItem.substring(0,textLength))){
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("PRODUCTNAME",productName.get(i));
temp.put("PRODUCTID", productID.get(i));
temp.put("BRANDNAME", brandName.get(i));
temp.put("VENDORNAME", vendor.get(i));
temp.put("OPENINGSTOCK", openingStock.get(i));
temp.put("AVAILABLESTOCK", availableStock.get(i));
temp.put("ADDSTOCK", addStock.get(i));
temp.put("PURCHASRPRICE", purchasePrice.get(i));
temp.put("SALESPRICE", salesPrice.get(i));
searchList.add(temp);
}
}
}
SimpleAdapter aaadapter = new SimpleAdapter(
new_stock_io.this,
searchList,
R.layout.list_otcmain123,
new String[] {"PRODUCTID", "PRODUCTNAME", "BRANDNAME","VENDORNAME","OPENINGSTOCK","AVAILABLESTOCK","ADDSTOCK","SALESPRICE", "PURCHASEPRICE"},
new int[] {R.id.txt_id,R.id.txt_name, R.id.txt_bname, R.id.txt_vendor, R.id.txt_openingstock,R.id.txt_availablestock,R.id.txt_addstock,R.id.txt_salesprice,R.id.txt_purchaseprice}
);
lv.setAdapter(aaadapter);
For ArrayList< String>
List<String> vendorName = new ArrayList<String>();
List<String> vendorID = new ArrayList<String>();
List<Object> objectArray = new ArrayList<Object>();
List<String> stockID = new ArrayList<String>();
List<String> picture = new ArrayList<String>();
List<String> productID = new ArrayList<String>();
List<String> productName = new ArrayList<String>();
List<String> brandName = new ArrayList<String>();
List<String> vendor = new ArrayList<String>();
List<String> openingStock = new ArrayList<String>();
List<String> availableStock = new ArrayList<String>();
List<String> addStock= new ArrayList<String>();
List<String> purchasePrice= new ArrayList<String>();
List<String> salesPrice= new ArrayList<String>();
List<String> subCategoryID = new ArrayList<String>();
List<String> subCategoryName= new ArrayList<String>();
List<String> productCategory= new ArrayList<String>();
List<String> subCategoriesNameArray = new ArrayList<String>();
List<String> subCategoriesIDArray= new ArrayList<String>();
for (int i = 0; i < productID.size(); i++)
{
Object obj = new Object();
objectArray.add(obj);
}
adapter = new ListItemsAdapter(objectArray, 1);
lv.setAdapter(adapter);
//List Adapter for listView.
public class ListItemsAdapter extends ArrayAdapter<Object> {
ViewHolder holder1;
public ListItemsAdapter(List<Object> items, int x) {
super(new_stock_io.this,android.R.layout.simple_list_item_1, items);
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.list_otcmain123, null);
holder1 = new ViewHolder();
holder1.tv_id=(TextView)convertView.findViewById(R.id.txt_id);
holder1.tv_name=(TextView)convertView.findViewById(R.id.txt_name);
holder1.tv_brandname=(TextView)convertView.findViewById(R.id.txt_bname);
holder1.tv_openingstock=(TextView)convertView.findViewById(R.id.txt_openingstock);
holder1.tv_availablestock=(TextView)convertView.findViewById(R.id.txt_availablestock);
holder1.tv_addstock=(TextView)convertView.findViewById(R.id.txt_addstock);
holder1.tv_addstock.setTag(position);
holder1.tv_addstock.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
// TODO Auto-generated method stub
Object o = (Object)holder1.tv_addstock.getTag();
Log.d("method focus", "call");
Log.d("position", position+"");
Log.d("edit text value", o.toString());
Log.d("value",":"+holder1.tv_addstock.getText().toString());
addStock.set(position, holder1.tv_addstock.getText().toString());
}
});
holder1.tv_prizeperunit=(TextView)convertView.findViewById(R.id.txt_salesprice);
holder1.tv_vendor=(TextView)convertView.findViewById(R.id.txt_vendor);
holder1.tv_prizeperunitpurchase=(TextView)convertView.findViewById(R.id.txt_purchaseprice);
convertView.setTag(holder1);
if (checkboxClicked==0)
{
holder1.tv_addstock.setTag(objectArray.get(position));
holder1.tv_id.setText(productID.get(position));
holder1.tv_name.setText(productName.get(position));
holder1.tv_brandname.setText(brandName.get(position));
holder1.tv_openingstock.setText(openingStock.get(position));
holder1.tv_availablestock.setText(availableStock.get(position));
holder1.tv_addstock.setId(position);
holder1.tv_addstock.setText(addStock.get(position));
holder1.tv_prizeperunit.setText(salesPrice.get(position));
holder1.tv_vendor.setText(vendor_Name);
holder1.tv_prizeperunitpurchase.setText(purchasePrice.get(position));
}
else if (checkboxClicked==1)
{
//holder1.tv_addstock.setTag(objectArray.get(position));
holder1.tv_id.setText(onCheckProductID.get(position));
holder1.tv_name.setText(onCheckProductName.get(position));
holder1.tv_brandname.setText(onCheckBrandName.get(position));
holder1.tv_openingstock.setText(onCheckOpeningStock.get(position));
holder1.tv_availablestock.setText(onCheckAvailableStock.get(position));
holder1.tv_addstock.setId(position);
holder1.tv_addstock.setText(onCheckAddStock.get(position));
holder1.tv_prizeperunit.setText(onCheckSalesPrice.get(position));
holder1.tv_vendor.setText(onCheckVendor.get(position));
holder1.tv_prizeperunitpurchase.setText(onCheckPurchasePrice.get(position));
}
return convertView;
}
}
//Simple holder class for above Adapter class.
public class ViewHolder
{
TextView tv_id,tv_name,tv_brandname,tv_vendor,tv_openingstock,tv_availablestock,tv_addstock,tv_prizeperunit,tv_prizeperunitpurchase;
}
XML:
<LinearLayout
android:id="#+id/ll_list_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txt_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:gravity="center"
android:text="ID"
android:textSize="6dip"
android:textStyle="bold" />
<View
android:id="#+id/view1"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:gravity="center"
android:text="Name"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view2"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<View
android:id="#+id/view3"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" /> -->
<TextView
android:id="#+id/txt_bname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:gravity="center"
android:text="Brand Name"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view3"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_vendor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.08"
android:gravity="center"
android:text="Vendor"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view27"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_openingstock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:gravity="center"
android:text="Opening Stock"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view26"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_availablestock"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:gravity="center"
android:text="Available Stock"
android:textSize="6dip"
android:textStyle="bold"/>
<View
android:id="#+id/view25"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_addstock"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.10"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" />
<View
android:id="#+id/view24"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_salesprice"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.10"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" android:width="7dp" />
<View
android:id="#+id/view24"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#e5e5e5" />
<TextView
android:id="#+id/txt_purchaseprice"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="0.12"
android:gravity="center"
android:hint="Rs."
android:textSize="6dip"
android:textStyle="bold" android:width="7dp"/>
</LinearLayout> </RelativeLayout>

Categories

Resources