Android适配器过滤

时间:2016-03-12 07:00:55

标签: android listview android-adapter

我是Android的新手我从Mysql创建了一个数据列表,当我过滤Listview时它工作正常但是当我点击过滤的项目时,它获取了错误的对象Id但我想要的是当我点击过滤的项目时它应该显示正确的项目ID。

这是代码:

package com.example.akbari.test;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.amigold.fundapter.BindDictionary;
import com.amigold.fundapter.FunDapter;
import com.amigold.fundapter.extractors.StringExtractor;
import com.amigold.fundapter.interfaces.FunDapterFilter;
import com.kosalgeek.android.json.JsonConverter;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

public class ClientList extends AppCompatActivity implements AsyncResponse, AdapterView.OnItemClickListener {

    final String LOG = "ClientList";
    ListView lv;
    private ArrayList<Clients> list;

    EditText inputSearch;

    Global global;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
                startActivity(new Intent(ClientList.this,ClientInsert.class));
            }
        });

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        global = ((Global) getApplicationContext());

        if(global.isInternetOn()) {
            init();
        }else{
            showConnectionAlert();
        }
    }

    private void init() {

        HashMap postData = new HashMap();
        postData.put("token",global.getMyToken());

        PostResponseAsyncTask readData = new PostResponseAsyncTask(ClientList.this,postData, this);
        readData.execute(global.getURL() + "client_list.php");
    }

    @Override
    public void processFinish(String s) {

        Log.d(LOG,s);

        list = new JsonConverter<Clients>().toArrayList(s,Clients.class);

        BindDictionary<Clients> dictionary = new BindDictionary<Clients>();

        dictionary.addStringField(R.id.textClientName, new StringExtractor<Clients>() {
            @Override
            public String getStringValue(Clients client, int position) {
                return client.name;
            }
        });

        dictionary.addStringField(R.id.textDomain, new StringExtractor<Clients>() {
            @Override
            public String getStringValue(Clients client, int position) {
                return client.client;
            }
        });

        dictionary.addStringField(R.id.textExpiry, new StringExtractor<Clients>() {
            @Override
            public String getStringValue(Clients client, int position) {
                return client.days;

            }
        });


        FunDapter<Clients> adapter = new FunDapter<>(ClientList.this,list,R.layout.layout_client_row,dictionary);

        lv = (ListView) findViewById(R.id.clientList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(this);

        inputSearch = (EditText) findViewById(R.id.editSearchClient);
        initTextFilter(adapter);


    }

    private void initTextFilter(final FunDapter<Clients> adapter) {

        // init the filter in the adapter
        adapter.initFilter(new FunDapterFilter<Clients>() {

            @Override
            public ArrayList<Clients> filter(String filterConstraint,
                                           ArrayList<Clients> originalList) {

                ArrayList<Clients> filtered = new ArrayList<Clients>();

                for (int i = 0; i < originalList.size(); i++) {
                    Clients client = originalList.get(i);
                    Log.d(LOG, filterConstraint.toLowerCase());
                    if (client.name.toLowerCase().contains(filterConstraint.toLowerCase())) {
                        filtered.add(client);
                    }
                }

                return filtered;
            }
        });


        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                // now we can use the regular ListView API for filtering:
                adapter.getFilter().filter(arg0);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            @Override
            public void afterTextChanged(Editable arg0) {

            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

        Clients selectedClient = list.get(position);

        Intent in = new Intent(ClientList.this,ClientDetails.class);

        in.putExtra("name", selectedClient.name);
        in.putExtra("domain", selectedClient.client);
        in.putExtra("contact", selectedClient.contact);
        in.putExtra("regDate", selectedClient.reg_date);
        in.putExtra("expireDate", selectedClient.exp_date);
        in.putExtra("status", selectedClient.status);
        in.putExtra("service", selectedClient.service);
        in.putExtra("notes", selectedClient.notes);
        in.putExtra("price", selectedClient.price);
        in.putExtra("cid", selectedClient.id);

        startActivity(in);

        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

    }

    public void showConnectionAlert(){
        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Connection Problem!");
        alertDialogBuilder.setMessage("Your mobile is not connected to the internet, do you want to connect it now?");

        alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                //Toast.makeText(Main.this, "You clicked yes button", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setClassName("com.android.phone", "com.android.phone.NetworkSetting");
                startActivity(intent);
            }
        });

        alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

1 个答案:

答案 0 :(得分:0)

insted of

  ArrayList<Clients> filtered = new ArrayList<Clients>();

使用

  list= new ArrayList<Clients>();