AutoCompleteTextView แบบกำหนดเองของ Android พร้อมอะแดปเตอร์แบบกำหนดเอง

โดยพื้นฐานแล้ว ฉันต้องการเขียนบางอย่างในข้อความแก้ไข จากนั้นจะมีการเรียกคำขอ http ของเว็บซึ่งส่งคืน JSONObject ซึ่งมีอาร์เรย์ JSON ซึ่งมีค่าอยู่ที่ไหนสักแห่งข้างใน ฉันต้องเติมรายการแบบเลื่อนลงที่มาพร้อมกับมุมมองเติมข้อความอัตโนมัติพร้อมผลลัพธ์จากวัตถุ JSON

ฉันสามารถทำบิตที่สองได้ เช่น ฉันสามารถเติมรายการดรอปดาวน์ด้วยค่าที่ฉันต้องการได้โดยใช้คลาสอะแดปเตอร์แบบกำหนดเองที่ขยาย arrayadapter ดังที่คุณเห็นด้านล่าง ปัญหาของฉันอยู่ที่บิตแรก ฉันจะแทนที่ AutoCompleteTextView ได้อย่างไร โดยที่มันไม่แสดงค่าคงที่ที่กรองแล้วจากอาร์เรย์ แต่แสดงค่าที่ฉันให้ไว้ ไม่อยากให้มันกรองเลย นี่คือซอร์สโค้ดสำหรับการเติมข้อความอัตโนมัติ view http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/AutoCompleteTextView.java#91

public class PersonAdapter extends ArrayAdapter
{

    // we use the constructor allowing to provide a List of objects for the data
    // to be binded.
    public PersonAdapter(Context context, int textViewResourceId,
            List objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // retrieve the Person object binded at this position
        final Person p = getItem(position);

        // A ViewHolder keeps references to children views to avoid unneccessary
        // calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no
        // need
        // to reinflate it. We only inflate a new View when the convertView
        // supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.list_item, parent, false);

            // Creates a ViewHolder and store references to the two children
            // views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.textName = (TextView) convertView
                    .findViewById(R.id.textName);
            holder.textEmail = (TextView) convertView
                    .findViewById(R.id.textEmail);
            holder.picture = (ImageView) convertView.findViewById(R.id.image);
            holder.picture.setFocusable(false);
            holder.picture.setFocusableInTouchMode(false);
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.textName.setText(p.getName());
        holder.textEmail.setText(p.getEmail());
        holder.picture.setImageResource(p.getResImage());
                //click on the picture
        holder.picture.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),
                        "Clicked on " + p.getName() + "'s picture",
                        Toast.LENGTH_SHORT).show();

            }
        });

        return convertView;
    }

    /**
     *
     * Inner holder class for a single row view in the ListView
     *
     */
    static class ViewHolder {
        TextView textName, textEmail;
        ImageView picture;
    }

}



คำตอบ (1)


ในเมธอด PersonAdapter ของคุณจะแทนที่ getFilter() โดยส่งคืน Filter ภายในที่คุณกำหนดเอง โดยที่คุณต้องใช้สองวิธี: PerformFiltering() และ publishResults() การดำเนินการ PerformFiltering จะถูกเรียกใช้ในเธรดของผู้ปฏิบัติงาน และที่นี่คุณเรียกใช้คำขอเว็บของคุณ ใน publishResults เพียงแค่เรียก clear () และเพิ่ม () รายการ

person pskink    schedule 01.10.2013
comment
ขอบคุณมันทำได้ แม้ว่าฉันจะได้รับความช่วยเหลือจากคำถามนี้เช่นกัน stackoverflow.com/questions/5023645/ - person Dv_MH; 02.10.2013