![](/img/trans.png)
[英]How can I get the ID of the ListView Item instead of the position on the listview?
[英]How to update Listview item position when I updating listview
我創建對話框,並進入給定項目列表的對話框。 並且在對話框中,我添加了搜索功能。 當我單擊項目時,它會獲得正確的項目位置,但是當我搜索列表時,它沒有用數據項目更新,而是無法檢索完全可點擊的項目。 以下是我的代碼。
public void uploadFromDirve(View vi) {
EditText et;
//setContentView(R.layout.list_dialog);
// TODO Auto-generated method stub
listDialog = new Dialog(Project_Define_Activity.this);
listDialog.setTitle("Select Item");
LayoutInflater li = (LayoutInflater) Project_Define_Activity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.list_dialog, null, false);
listDialog.setContentView(v);
listDialog.setCancelable(true);
//there are a lot of <span id="IL_AD7" class="IL_AD">settings</span>, for dialog, <span id="IL_AD1" class="IL_AD">check</span> them all out!
ListView list1 = (ListView) listDialog.findViewById(R.id.listview);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
list1.setAdapter(adapter);
et=(EditText)listDialog.findViewById(R.id.edit_Search);
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Project_Define_Activity.this.adapter.getFilter().filter(cs);
adapter.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
//list1.setAdapter(new ArrayAdapter<String>(Project_Define_Activity.this,android.R.layout.simple_list_item_1,names));
//now that the dialog is set up, it's time to <span id="IL_AD2" class="IL_AD">show</span> it
list1.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, final int arg2,
long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(Project_Define_Activity.this);
builder.setMessage("Attach file "+arg2)
.setPositiveButton("Attach ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.out.println("OK CLICKED");
Log.e("Selected", names.get(arg2));
fileflag = 1;
fileindex = arg2;
listDialog.cancel();
nameOfFile.setText(names.get(arg2));
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
listDialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.setTitle("Information");
alert.show();
}
});
listDialog.show();
}
要更新數據,您可以通知數據集已更改-
adapter.notifyDataSetChanged();
或者另一種選擇是清除要在適配器中填充的數組列表,並用新值更新數組列表,然后將其填充回-
ArrayList<String> print_copy=new ArrayList<String();
比清除並再次分配新值
print_copy.clear();
print_copy.add(data);
print_copy.add(data);
並再次填充它。
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.