在Listview中搜索选项

时间:2015-05-09 02:07:39

标签: android json listview search

我想添加搜索功能。

我已经这样做,所以在互联网的帮助下,现在想要搜索分类帐并过滤我的应用程序中的记录。

我的java文件如下

trailbalance.java

package com.cs_infotech.newpathshala;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.cs_infotech.newpathshala.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

public class trailbalance extends Fragment {
    ListView list;
    TextView ledgername;
    TextView debit;
    TextView credit;
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
    //URL to get JSON Array
    public static String url = "http://www.cs-infotech.com/cms/andgettrail.php";
    //JSON Node Names
    private static final String TAG_OS = "android";
    private static final String TAG_LEDGERNAME = "ledgername";
    private static final String TAG_DEBIT = "debit";
    private static final String TAG_CREDIT = "credit";
    private static final String TAG_ATTRIBUTES = "attributes";
    JSONArray android = null;
    private View v;
    public trailbalance() {
    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.trailbalance, container, false);

    TextView showGlobal     = (TextView) rootView.findViewById(R.id.header);
    showGlobal.setText("Trail Balance");
    oslist = new ArrayList<HashMap<String, String>>();

    v=rootView;

    new JSONParse().execute();

    return rootView;
}
EditText search;

private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ledgername = (TextView) v.findViewById(R.id.ledgername);
        debit = (TextView) v.findViewById(R.id.debit);
        credit = (TextView) v.findViewById(R.id.credit);
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            android = json.getJSONArray(TAG_OS);
            for(int i = 0; i < android.length(); i++){
                JSONObject c = android.getJSONObject(i);
                // Storing  JSON item in a Variable
                String ledgername = c.getString(TAG_LEDGERNAME);

                String debit = c.getString(TAG_DEBIT);
                String credit = c.getString(TAG_CREDIT);
                String attributes = c.getString(TAG_ATTRIBUTES);
                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_LEDGERNAME, ledgername);
                if (Double.parseDouble(debit)>0)
                {
                    map.put(TAG_DEBIT, debit);
                }
                else
                {
                    map.put(TAG_DEBIT, "");
                }
                if (Double.parseDouble(credit)>0)
                {
                    map.put(TAG_CREDIT, credit);
                }
                else
                {
                    map.put(TAG_CREDIT, "");
                }
                map.put(TAG_ATTRIBUTES, attributes);
                oslist.add(map);
                list=(ListView) v.findViewById(R.id.list);
                final ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                        R.layout.trailrow,
                        new String[] {TAG_LEDGERNAME,TAG_DEBIT, TAG_CREDIT }, new int[] {
                        R.id.ledgername,R.id.debit, R.id.credit})


                {
                    @Override
                    public View getView (int position, View convertView, ViewGroup parent)
                    {
                        View v = super.getView(position, convertView, parent);



                        if(position %2==0)
                        {
                            v.setBackgroundColor(Color.LTGRAY);
                        }
                        else
                        {
                            v.setBackgroundColor(Color.WHITE);
                        }


                        if (oslist.get(+position).get("attributes").toString().trim().toUpperCase().equals("N"))
                        {

                        }
                        else
                        {
                            //v.setBackgroundColor(Color.BLACK);
                            TextView textView=(TextView) v.findViewById(R.id.ledgername);
                            textView.setTypeface(null, Typeface.BOLD);
                            //textView.setTextColor(8421504);
                            //textView.setBackgroundColor(000000);

                            TextView debit=(TextView) v.findViewById(R.id.debit);
                            debit.setTypeface(null, Typeface.BOLD);
                            //debit.setTextColor(8421504);
                            //debit.setBackgroundColor(000000);

                            TextView credit=(TextView) v.findViewById(R.id.credit);
                            credit.setTypeface(null, Typeface.BOLD);
                         //   credit.setTextColor(8421504);
                           // credit.setBackgroundColor(000000);
                        }

                        return v;
                    }


                };


                list.setAdapter(adapter);
                search= (EditText) v.findViewById(R.id.search);

                search.addTextChangedListener(new TextWatcher() {

                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    public void beforeTextChanged(CharSequence s, int start, int count,
                                                  int after) {


                    }

                    public void afterTextChanged(Editable s) {
                    }
                });



                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(getActivity(), "You Clicked at " + oslist.get(+position).get("ledgername"), Toast.LENGTH_LONG).show();
                    }

                });

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

}

trailbalance.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include  layout="@layout/headerlayout" android:id="@+id/id" />

    <LinearLayout
        android:id="@+id/ll1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >



    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll2"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"

        >

    </LinearLayout>

    <include  layout="@layout/trailheader" android:id="@+id/id1" />
    <EditText
        android:id="@+id/search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dip"
        android:layout_marginBottom="30dip"
        />


    <include  layout="@layout/commonlayout" android:id="@+id/id" />

</RelativeLayout>

0 个答案:

没有答案