简体   繁体   中英

Search In List View Android

How to implement search method on this code? It successfully load data from Service but I am not able to implement searching on this list.

Main Class from where it Application Runs

import java.util.HashMap;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ClaimList extends Activity implements OnItemClickListener{

private static final String SOAP_ACTION = "http://tempuri.org/Adminclaim_List";
private static final String METHOD_NAME = "Adminclaim_List";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.ps-computers.com/service.asmx";

EditText searchText;
ProgressDialog Dialog;
SoapObject resultRequestSOAP;
ListView lview;
ListViewAdapter_claimlist lviewAdapter;

String Constants = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.claimlist);

    Dialog = new ProgressDialog(this);
    Dialog.setMessage("Loading...");

    lview  = (ListView) findViewById(R.id.lstvw_cliamlist);
    searchText = (EditText) findViewById(R.id.searchTextFeild);


new doCallKsoapApiTask().execute();

    lview.setOnItemClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub

}

private class doCallKsoapApiTask extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        Dialog.show();
    }

    @Override
    protected String doInBackground(Void... arg0) {

        HashMap<String, String> a = new HashMap<String, String>();
        try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
                    URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            resultRequestSOAP = (SoapObject) envelope.bodyIn;
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            Log.v("test", "Error : " + e.getMessage()); // / REMOVE

            return null;
        }

    }

    @Override
    protected void onPostExecute(String response) {

        if (Dialog.isShowing())
            Dialog.dismiss();

        try {

            SoapObject root = (SoapObject) resultRequestSOAP.getProperty(0);
            SoapObject s_deals = (SoapObject) root
                    .getProperty("NewDataSet");
            String claimid[] = new String[s_deals.getPropertyCount()];
            String ref[] = new String[s_deals.getPropertyCount()];
            String policy[] = new String[s_deals.getPropertyCount()];
            String natureofloss[] = new String[s_deals.getPropertyCount()];
            String registration[] = new String[s_deals.getPropertyCount()];
            String status[] = new String[s_deals.getPropertyCount()];

            for (int i = 0; i < s_deals.getPropertyCount(); i++) {

                Object property = s_deals.getProperty(i);
                if (property instanceof SoapObject) {
                    SoapObject category_list = (SoapObject) property;
                    String chk = "";

                    chk = category_list.getProperty("claim_id").toString()
                            .trim();
                    if (chk.equals("anyType{}")) {
                        claimid[i] = Constants;
                    } else {
                        claimid[i] = chk;
                    }

                    chk = category_list.getProperty("key_id").toString()
                            .trim();
                    if (chk.equals("anyType{}")) {
                        ref[i] = Constants;
                    } else {
                        ref[i] = chk;
                    }

                    chk = category_list.getProperty("policy_no").toString()
                            .trim();
                    if (chk.equals("anyType{}")) {
                        policy[i] = Constants;
                    } else {
                        policy[i] = chk;
                    }

                    chk = category_list.getProperty("Nature_of_Loss")
                            .toString().trim();
                    if (chk.equals("anyType{}")) {
                        natureofloss[i] = Constants;
                    } else {
                        natureofloss[i] = chk;
                    }

                    chk = category_list.getProperty("regis")
                            .toString().trim();
                    if (chk.equals("anyType{}")) {
                        registration[i] = Constants;
                    } else {
                        registration[i] = chk;
                    }

                    chk = category_list.getProperty("claim_status")
                            .toString().trim();
                    if (chk.equals("anyType{}")) {
                        status[i] = Constants;
                    } else {
                        status[i] = chk;
                    }
                }
            }
            lviewAdapter = new ListViewAdapter_claimlist(ClaimList.this,
                    claimid, ref, policy, natureofloss,registration, status);
            lview.setAdapter(lviewAdapter);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Claims not found",
                    Toast.LENGTH_SHORT).show();
        }
    }
}
}

Custom Adapter Class

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter_claimlist extends BaseAdapter{

Activity context;
String claimid[];
String ref[];
String policy[];
String natureofloss[];
String registration[];
String status[];


public ListViewAdapter_claimlist(Activity context, String[] claimid, String[] ref,String[] policy, String[] natureofloss,String[] registration, String[] status) {
    super();
    this.context = context;
    this.claimid = claimid;
    this.ref = ref;
    this.policy = policy;
    this.natureofloss = natureofloss;
    this.registration = registration;
    this.status = status;       

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return ref.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}
private class ViewHolder {
    TextView cliamid;
    TextView ref;
    TextView policy;
    TextView natureofloss;
    TextView registration;
    TextView status;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    LayoutInflater inflater =  context.getLayoutInflater();
    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.rowitem_cliamlist, null);
        holder = new ViewHolder();
        holder.cliamid = (TextView) convertView.findViewById(R.id.tv_claims);
        holder.ref = (TextView)          convertView.findViewById(R.id.tv_referencepolicy);
        holder.policy = (TextView) convertView.findViewById(R.id.tv_policyNo);
        holder.natureofloss = (TextView) convertView.findViewById(R.id.tv_natureoflos);
        holder.registration = (TextView) convertView.findViewById(R.id.tv_registration);
        holder.status = (TextView) convertView.findViewById(R.id.tv_claimstatus);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.cliamid.setText(claimid[position]);
    holder.ref.setText(ref[position]);
    holder.policy.setText(policy[position]);
    holder.natureofloss.setText(natureofloss[position]);
    holder.status.setText(status[position]);



return convertView;

}   

}

Main Layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <TableRow
         android:id="@+id/tableRow2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:weightSum="100" >

          <EditText
              android:id="@+id/searchTextFeild"
             style="@style/FormTextBoxes"
              android:layout_margin="5dp"
              android:layout_weight="30"
              android:paddingLeft="5px"
              android:ems="10"
              android:hint="Police No..."
              android:singleLine="true" >

              <requestFocus />
          </EditText>

     </TableRow>
    <ListView
        android:id="@+id/lstvw_cliamlist"
        android:layout_width="fill_parent"
        android:layout_height="347dp"
        android:layout_centerHorizontal="true"
         android:scrollbars="none"
        android:focusable="false"
        android:layout_weight="0.88" >
    </ListView>


    </LinearLayout>

Custom Row

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
 >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="7"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="10" >

        <TextView
            android:id="@+id/TextView02"
            style="@style/rowitem_left"
            android:layout_weight="5"
            android:text="Claim #"
            android:textColor="@color/default_blue" />

        <TextView
            android:id="@+id/tv_claims"
            style="@style/rowitem_right"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:text="220330"

            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="10" >

        <TextView
            android:id="@+id/textView2"
            style="@style/rowitem_left"
            android:layout_weight="5"
            android:text="Ref #" />

        <TextView
            android:id="@+id/tv_referencepolicy"
            style="@style/rowitem_right"
            android:layout_weight="5"
            android:text="220330" />

    </LinearLayout>

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TextView04"
            style="@style/rowitem_left"
            android:layout_weight="5"
            android:text="Registration" />

        <TextView
            android:id="@+id/tv_registration"
            style="@style/rowitem_right"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:text="0" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TextView01"
            style="@style/rowitem_left"
            android:layout_weight="5"
            android:text="Police No" />

        <TextView
            android:id="@+id/tv_policyNo"
            style="@style/rowitem_right"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:text="220330" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView3"
            style="@style/rowitem_left"
            android:layout_weight="5"
            android:text="Nature of Loss"
             />

        <TextView
            android:id="@+id/tv_natureoflos"
            style="@style/rowitem_right"
            android:layout_weight="5"
            android:text="TD"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            style="@style/rowitem_left"
            android:layout_weight="5"
            android:text="Status"
            />

        <TextView
            android:id="@+id/tv_claimstatus"
            style="@style/rowitem_right"
            android:layout_weight="5"
            android:text="Open"
            android:textStyle="bold"
            android:textColor="@color/textgreen" />
    </LinearLayout>
</LinearLayout>

</LinearLayout>

</RelativeLayout>

Screen Shot

在此处输入图片说明

Thanks in Advance if some one implement Searching Via Police No (In code policy no)

I have used this as my code for search filter . i hope this helps .

    yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String Police No=yourEditText.getText().toString();

            ArrayList<YouDataModel> Filter=new ArrayList<YouDataModel>();

            for (DataModel data: leaveDatas) {
                if (data.getUserName().toLowerCase().contains(nameToSearch.toLowerCase()) || data.getUserName().toLowerCase().equalsIgnoreCase(nameToSearch.toLowerCase()) )
                {


                    filteredLeaves.add(data);
                }


            }

            leaves_adapter = new Leaves_Adapter(filteredLeaves, Activity.this);
            listView.setAdapter(leaves_adapter);

        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM