我遇到了这个问题。我想创建一个listView来显示所有用户的信息,包括昵称,真实姓名和个人资料图片。现在我想为每个配置文件图像添加监听器,因此用户可以单击每个图像以查看更多信息。我不知道如何添加监听器。这是代码。请帮我如何添加监听器。任何帮助将不胜感激。
public class SearchActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor>, OnClickListener{
private SimpleCursorAdapter adapter;
private ListView listView;
private TextView searchText;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
addClickAction((View)this.getListView());
adapter = new SimpleCursorAdapter(this,
R.layout.search_list_item,
null, // cursor will be added from function onLoadFinished
new String[]{DataProvider.COL_NAME, DataProvider.COL_ID,DataProvider.PROFILE_COL_IMAGE},
new int[]{R.id.text1, R.id.text2,R.id.image},
0);
listView = getListView();
getLoaderManager().initLoader(0, null, this);
}
private void searchAction() {
// fetch user input
searchText = (TextView)findViewById(R.id.searchText);
String userName = searchText.getText().toString();
searchText.clearFocus();
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
switch(view.getId()) {
case R.id.text2:
((TextView)view).setText(String.format("Show user's real name"));
return true;
case R.id.image:
// show user's profile image
byte[] imageByte = cursor.getBlob(columnIndex);
((ImageView)view).setImageBitmap(DbBitmapUtility.getResizedBitmap(DbBitmapUtility.getImage(imageByte),125,125));
return true;
}
return false;
}
});
listView.setAdapter(adapter);
listView.requestFocus();
}
//----------------------------------------------------------------------------
//implement abstract functions for LoaderManager.LoaderCallbacks<Cursor>
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader loader = new CursorLoader(this,
DataProvider.CONTENT_URI_PROFILE,
new String[]{DataProvider.COL_ID, DataProvider.COL_NAME,DataProvider.PROFILE_COL_IMAGE},
null,
null,//new String[]{DataProvider.PROFILE_COL_LASTMSGAT},
DataProvider.PROFILE_COL_LASTMSGAT + " DESC");
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.searchButton:
searchAction();
Toast.makeText(getBaseContext(), "Search button", Toast.LENGTH_LONG).show();
break;
}
}
public void addClickAction(View view) {
ImageButton searchbutton = (ImageButton) this.findViewById(R.id.searchButton);
searchbutton.setOnClickListener(this);
}
}
答案 0 :(得分:0)
我想出了什么。我可以编辑setViewBinder。代码
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
final int id = columnIndex;
switch(view.getId()) {
// here can add one more line in the main page for each account
case R.id.text2:
((TextView)view).setText(String.format("Show user's real name"));
return true;
case R.id.avatar:
byte[] imageByte = cursor.getBlob(columnIndex);
((ImageView)view).setImageBitmap(DbBitmapUtility.getResizedBitmap(DbBitmapUtility.getImage(imageByte),125,125));
((ImageView)view).setClickable(true);
((ImageView)view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"Go to view more information."+"columnIndex: "+id,Toast.LENGTH_LONG).show();
}
});
return true;
}
return false;
}
});