如何从列表视图本身单击项目时更新列表视图。我知道 notifydatachanged 但我不知道如何在我的代码中实现它以使其工作。请帮忙。
private ListView listView;
private ArrayList<String> friends;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initialize button ass and textview
Button btnSearch = (Button)findViewById(R.id.btnSearch);
final EditText ED = (EditText)findViewById(R.id.editText1);
final TextView TV_word = (TextView)findViewById(R.id.TV_word);
btnSearch.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
String word = ED.getText().toString();
fillFreinds(word);
setUpList();
TV_word.setText(word); //set the word into the textview which the user has entered into the search field
}
});
//��� �������� ������
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
//���, ���� �������!
fillWords();
setUpList();
}
private void setUpList()
{
//��������� ����������� ������� � layout �������� ��� ���������
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends));
listView = getListView();
//������� ���� ����, ��� ����
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
Toast.makeText(getApplicationContext(),((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
String wordClicked = ((TextView) view).getText().toString();
fillFreinds(wordClicked);
}
});
}
//���������� �������� �� ���� ������
private void fillFreinds(String word) {
final TextView partOfspeech = (TextView)findViewById(R.id.tv_PartOfSpeech);
friends = new ArrayList<String>();
Cursor friendCursor = database.rawQuery("SELECT * FROM friends where name='"+ word + "'", null);
friendCursor.moveToFirst();
if(!friendCursor.isAfterLast())
{
do
{
String name = friendCursor.getString(2);
partOfspeech.setText(friendCursor.getString(3));
friends.add(name);
} while (friendCursor.moveToNext());
}
friendCursor.close();
}
//On Start Activity
private void fillWords() {
friends = new ArrayList<String>();
Cursor friendCursor = database.rawQuery("SELECT * FROM friends", null);
friendCursor.moveToFirst();
if(!friendCursor.isAfterLast())
{
do
{
String name = friendCursor.getString(1);
//partOfspeech.setText(friendCursor.getString(3));
friends.add(name);
} while (friendCursor.moveToNext());
}
friendCursor.close();
}
我谷歌一个小时,但我无法弄明白
答案 0 :(得分:1)
将setUpList
更改为以下代码:
private void setUpList()
{
//��������� ����������� ������� � layout �������� ��� ���������
//// change to this
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends);
setListAdapter(adapter);
listView = getListView();
//������� ���� ����, ��� ����
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
Toast.makeText(getApplicationContext(),((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
String wordClicked = ((TextView) view).getText().toString();
fillFreinds(wordClicked);
///// add following line
adapter.notifyDatasetChanged();
/////
}
});
}
您还需要定义类的以下行顶部:
ArrayAdapter<String> adapter;
要刷新数据,您可以使用notifyDatasetChanged();
答案 1 :(得分:1)
在您的课程中添加此行
ArrayAdapter<String> adapter;
更改此行
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends));
到
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, friends)
setListAdapter(adapter);
并将适配器通知为
adapter.notifyDataSetChanged();
即。将您的listView Item click
功能更改为
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
Toast.makeText(getApplicationContext(),((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
String wordClicked = ((TextView) view).getText().toString();
fillFreinds(wordClicked);
adapter.notifyDataSetChanged();
}
});