2010-03-30 25 views
6

Tôi đang sử dụng 3 AutocompleteTextViews để đề xuất các mục từ cơ sở dữ liệu. Tôi đã phân lớp AutocompleteTextView để xử lý cài đặt văn bản mặc định thành rỗng khi được nhấp và đặt lại về hướng dẫn mặc định nếu bị di chuyển và không có gì được nhập.

Tôi đã sử dụng SimpleCursorAdapter để liên kết với chế độ xem, nhưng tôi phát hiện ra rằng không có cách nào tôi có thể lấy id của AutocompleteTextView từ OnItemClickListener, mà tôi cần thêm thông tin từ hàng đã chọn trong biến trên đó AutocompleteTextView từ đó. Tất cả những gì tôi có thể truy cập là AutoDompleteTextView $ DropDownListView, đó là một lớp bên trong không có giấy tờ xuất hiện để cung cấp không có chức năng thực sự. Không có cách nào để đi lên hệ thống phân cấp khung nhìn để có được AutocompleteTextView gốc.

Vì vậy, tôi đã phân lớp SimpleCursorAdapter và thêm một int vào hàm tạo để xác định xem AutocompleteTextView nào là bộ điều hợp và tôi có thể truy cập từ khung nhìn được chuyển vào OnItemClick(). Vì vậy, mặc dù giải pháp của tôi hoạt động tốt, tôi tự hỏi nếu nó có thể nhận được id của một AutocompleteTextView từ DropDownListView của nó?

Tôi cũng đang sử dụng truy vấn cơ sở dữ liệu khác nhận id từ OnItemClick và sau đó tra cứu dữ liệu cho mục đó, vì tôi không thể tìm cách chuyển đổi nhiều cột thành chuỗi. Tôi có nên sử dụng CursorAdapter cho việc này, để lưu bắt đầu một truy vấn khác không? Oh, và một điều nữa, tôi cần một con trỏ cơ sở dữ liệu ban đầu (all_cursor) khi tất cả những gì tôi đang làm là lọc nó để có được một con trỏ mới? Có vẻ như quá mức cần thiết.

Hoạt động ....

dbse.openDataBase(); 
    Cursor all_Cursor = dbse.autocomplete_query(); 
    startManagingCursor(all_Cursor); 
    String[] from_all = new String[]{DbAdapter.KEY_NAME}; 
    int[] to_all = new int[] {android.R.id.text1}; 
    from_adapt = new AutocompleteAdapter(FROM_DBADAPTER, this,android.R.layout.simple_dropdown_item_1line, all_Cursor, from_all, to_all); 
    from_adapt.setStringConversionColumn(1); 
    from_adapt.setFilterQueryProvider(this); 
    to_adapt = new AutocompleteAdapter(TO_DBADAPTER, this,android.R.layout.simple_dropdown_item_1line, all_Cursor, from_all, to_all); 
    to_adapt.setStringConversionColumn(1); 
    to_adapt.setFilterQueryProvider(this); 
from_auto_complete = (Autocomplete) findViewById(R.id.entry_from); 
from_auto_complete.setAdapter(from_adapt); 
from_auto_complete.setOnItemClickListener(this); 

to_auto_complete = (Autocomplete) findViewById(R.id.entry_to); 
to_auto_complete.setAdapter(to_adapt); 
to_auto_complete.setOnItemClickListener(this); 

public void onItemClick (AdapterView<?> parent, View view, int position, long id) { 
    Cursor selected_row_cursor = dbse.data_from_id(id); 
    selected_row_cursor.moveToFirst(); 
    String lat = selected_row_cursor.getString(1); 
    String lon = selected_row_cursor.getString(2); 
    int source = ((AutocompleteAdapter) parent.getAdapter()).getSource(); 

Autocomplete lớp:

public class Autocomplete extends AutoCompleteTextView implements OnTouchListener,OnFocusChangeListener{ 

String textcontent; 
Context mycontext = null; 
int viewid = this.getId(); 

public Autocomplete(Context context, AttributeSet attrs) { 
super(context, attrs); 
textcontent = this.getText().toString(); 
mycontext = context; 
this.setOnFocusChangeListener(this);  
this.setOnTouchListener(this); 
} 
public boolean onTouch(View v, MotionEvent event) { 
if (textcontent.equals(mycontext.getString(R.string.from_textbox)) | 
textcontent.equals(mycontext.getString(R.string.to_textbox)) | 
textcontent.equals(mycontext.getString(R.string.via_textbox))) { 
this.setText(""); 
} 
return false; 
} 
public void onFocusChange(View v, boolean hasFocus) { 
if (hasFocus == false) { 
int a = this.getText().length(); 
if (a == 0){ 
if (viewid == R.id.entry_from) {this.setText(R.string.from_textbox);} 
if (viewid == R.id.entry_to) {this.setText(R.string.to_textbox);} 
if (viewid == R.id.entry_via) {this.setText(R.string.via_textbox);} 
} 
} 
} 
} 

Adaptor:

public class AutocompleteAdapter extends SimpleCursorAdapter { 
int source; 
public AutocompleteAdapter(int query_source, Context context, int layout, Cursor c, 
     String[] from, int[] to) { 
    super(context, layout, c, from, to); 
    source = query_source; 
} 
public int getSource() { 
    return source; 
} 
    } 

xin lỗi đó là rất nhiều mã! Cảm ơn bạn đã giúp đỡ.

Stephen

Trả lời

1

Thay vì sử dụng this như người nghe, tạo ra một lớp người nghe mới và cung cấp cho nó TextView autocomplete của bạn:

public class MyActivity extends Activity { 

    // .... somewhere 
    from_auto_complete.setOnItemClickListener(new MyClickListener(from_auto_complete)); 

    private class MyClickListener implements OnClickListener { 
     AutoCompleteTextView autoComplete; 
     MyClickListener(AutoCompleteTextView actv) { 
      autoComplete = actv; 
     } 
     // ... handle clicks 
    } 
} 
+0

Đã một thời gian cho tôi để xem xét này, nhưng nó làm việc. Tôi đã thay đổi OnClickListener thành OnItemClickListener, để cung cấp tất cả các chi tiết về bản ghi từ DropDown. Cảm ơn rất nhiều, tôi biết phải có câu trả lời! –

Các vấn đề liên quan