praxmon
praxmon

Reputation: 5121

Android Adapter not working

I have followed this tutorial for the creation of a custom adapter.This link is mentioned in the comments to this SO question.

The codes are given as follows:

MyMainView.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android: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.context.GlobalSearch">

<AutoCompleteTextView
    android:id="@+id/globalSearch_editBrandData"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    android:hint="Enter Name"
    android:imeOptions="actionDone"
    android:singleLine="true"
/>

<ListView
    android:id="@+id/globalSearch_listResult"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_below="@+id/globalSearch_editBrandData"
    android:visibility="visible"
>
</ListView>

<TextView
    android:id="@+id/globalSearch_errorMessage"
    android:layout_below="@+id/globalSearch_editBrandData"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Please enter the correct data"
    android:gravity="center"
    android:layout_marginBottom="100dp"
    android:visibility="gone"
/>

In the list for for each element I want to display an Image and some text. So my edited listview fields are defined as:

MyEdited.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/rounded_corners"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:elevation="1dp" >

    <ImageView
        android:id="@+id/globalSearch_imageView"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        />

    <TextView
        android:id="@+id/globalSearch_textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/globalSearch_imageView"
        android:text = "Puma"
        android:gravity="center"
        />
</RelativeLayout>

Now the adapter for the list view is defined as:

GlobalSearchInformation.java

import android.util.Log;

public class GlobalSearchInformation {

private int drawableID;
private String textDetails;

public GlobalSearchInformation(int drawbaleID, String textDetails)
{
    super();
    this.drawableID = drawableID;
    this.textDetails = textDetails;
}

public void setTextDetails(String textDetails)
{
    this.textDetails = textDetails;
}

public String getTextDetails()
{
    return textDetails;
}

public void setDrawableID(int drawableID)
{
    this.drawableID = drawableID;
}

public int getDrawableID()
{
    return drawableID;
}

}

GlobalSearchAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class GlobalSearchAdapter extends BaseAdapter {
Context context;
protected List<GlobalSearchInformation> globalSearchList;
LayoutInflater layoutInflater;

public GlobalSearchAdapter(Context context, List<GlobalSearchInformation> globalSearchList)
{
    this.globalSearchList = globalSearchList;
    this.layoutInflater = LayoutInflater.from(context);
    this.context = context;
}

@Override
public int getCount() {
    return globalSearchList.size();
}

@Override
public Object getItem(int position) {
    return globalSearchList.get(position);
}

@Override
public long getItemId(int position) {
    return globalSearchList.get(position).getDrawableID();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if(convertView == null)
    {
        holder = new ViewHolder();
        convertView = this.layoutInflater.inflate(R.layout.globalsearch_listview, parent, false);
        holder.globalsearchadapter_text = (TextView) convertView.findViewById(R.id.globalSearch_textView);
        holder.globalsearchadapter_image = (ImageView) convertView.findViewById(R.id.globalSearch_imageView);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    GlobalSearchInformation gsi = globalSearchList.get(position);
    holder.globalsearchadapter_image.setImageResource(gsi.getDrawableID());
    holder.globalsearchadapter_text.setText(gsi.getTextDetails());
    return convertView;
}

private class ViewHolder
{
    ImageView globalsearchadapter_image;
    TextView globalsearchadapter_text;
}
}

GlobalSearch.java

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import APP.infrastructure.models.Merchant;

import java.util.ArrayList;
import java.util.List;


public class GlobalSearch extends ActionBarActivity {



ListView listViewGlobal;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_global_search);

    final AutoCompleteTextView actSearch = (AutoCompleteTextView) findViewById(R.id.globalSearch_editBrandSearch);
    final ListView lvSearch = (ListView) findViewById(R.id.globalSearch_listResult);
    final TextView tvSearch = (TextView) findViewById(R.id.globalSearch_errorMessage);

    List<Merchant> merchants = Merchant.findWithQuery(Merchant.class,"select * from MERCHANT");
    ArrayList <String> al = new ArrayList<String>();
    for(int i=0;i<merchants.size(); i++)
    {
        al.add(i, merchants.get(i).getName());
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, al);
    actSearch.setAdapter(adapter);

    actSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_DONE)
            {
                String mEdit = actSearch.getText().toString();
                List<Merchant> merchants = Merchant.findWithQuery(Merchant.class,"select * from MERCHANT where UPPER(NAME) LIKE \'"+mEdit.toUpperCase()+"%\'");

                if(merchants.size()==0)
                {
                    Log.d("debug", "Alert dialog: does not exist");
                    lvSearch.setVisibility(View.GONE);
                    tvSearch.setVisibility(View.VISIBLE);
                }
                else
                {
                    ArrayList <GlobalSearchInformation> arrayInformation = new ArrayList<GlobalSearchInformation>();
                    Log.d("global search", "merchant size != 0");
                    GlobalSearchInformation globalSearch_Page = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName());
                    GlobalSearchInformation globalSearch_Feed = new GlobalSearchInformation(R.drawable.image_id, "Posts by "+merchants.get(0).getName());
                    GlobalSearchInformation globalSearch_Store = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName()+" stores around you");
                    GlobalSearchInformation globalSearch_Alerts = new GlobalSearchInformation(R.drawable.image_id, merchants.get(0).getName()+" Events");
                    arrayInformation.add(globalSearch_Page);
                    arrayInformation.add(globalSearch_Feed);
                    arrayInformation.add(globalSearch_Store);
                    arrayInformation.add(globalSearch_Alerts);
                    GlobalSearchAdapter adapter = new GlobalSearchAdapter(getApplicationContext(), arrayInformation);
                    lvSearch.setAdapter(adapter);
                }
            }
            return false;
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_global_search, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

All this is being compiled and is running without an error but on viewing the listview after entering the data in the autocomplete textview, the elements from the database are being retrieved but the listview remains empty. Where am I going wrong?

Upvotes: 0

Views: 1687

Answers (1)

Abhishek V
Abhishek V

Reputation: 12526

  • Don't use application context to inflate the list view. Use activity context instead.

    GlobalSearchAdapter adapter = new GlobalSearchAdapter(GlobalSearch.this, arrayInformation); lvSearch.setAdapter(adapter);

  • Don't initilize the adpater each time onEditorAction fuction is called, instead just update the data in adapter and call notifyDataSetChanged().

  • globalSearch_textView is overlapping the ListView so list view won't be visible. Change the height of text view to wrap content.

       <TextView
    
        android:id="@+id/globalSearch_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/globalSearch_imageView"
        android:gravity="center"
        android:text="Puma" />
    

Upvotes: 1

Related Questions