在列表视图中搜索Android

时间:2015-05-16 09:39:00

标签: android listview search android-filterable

如何在此代码上实现搜索方法?它成功加载了来自Service的数据,但我无法在此列表中实现搜索。

应用程序运行的主类

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();
        }
    }
}
}

自定义适配器类

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;

}   

}

主要布局

    <?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>

自定义行

<?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>

屏幕截图

enter image description here

如果有人实施“通过警察搜索”(在代码政策中没有)

,请提前致谢

1 个答案:

答案 0 :(得分:0)

我用它作为搜索过滤器的代码。我希望这有帮助 。

    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) {


        }
    });