更改列表视图中特定项目的图像视图
Changing imageview of a specific item in a listview
我想根据一些意图值更改列表视图中的图像视图。为此,我确实覆盖了简单游标适配器的 class,如下所示,
public class NotesAdapter extends SimpleCursorAdapter {
Intent intent;
public NotesAdapter(Context context, int layout, Cursor c, String[] from, int[] to, Intent i) {
super(context, layout, c, from, to);
intent=i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = super.getView(position, convertView, parent);
ImageView image = (ImageView) item.findViewById(R.id.icon);
ImageView image2= (ImageView) item.findViewById(R.id.icon2);
if (intent.getStringExtra("category")!=null && intent.getStringExtra("category").equalsIgnoreCase("draft")){
image.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
}
return item;
}
}
在包含列表视图的 activity 中,我定义了 NotesAdapter 并在片段 onCreateView 方法中初始化它,如下所示,
mSimpleCursorAdapter = new NotesAdapter(getActivity(), R.layout.notes_row, null, from, to, getActivity().getIntent());
这是我初始化适配器的代码,
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todo_list, container, false);
mSimpleCursorAdapter = new NotesAdapter(getActivity(), R.layout.notes_row, null, from, to, getActivity().getIntent());
getLoaderManager().initLoader(LOADER_ID, null, this); //once this is done onCreateLoader will be called.
final ListView listView = (ListView) rootView.findViewById(R.id.notes_list); //findViewById must be called using the rootView because we are inside a fragment.
listView.addFooterView(new View(getActivity()), null, true);
text= (TextView) rootView.findViewById(R.id.empty_list);
listView.setAdapter(mSimpleCursorAdapter);
if(mSimpleCursorAdapter.isEmpty()) {
text.setVisibility(View.VISIBLE);
listView.setEmptyView(text);
if (getActivity().findViewById (R.id.fragment_container)!=null){
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id. fragment_container, new EmptyEditor()).commit();
}
}
else {
text.setVisibility(View.INVISIBLE);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Cursor cursor = mSimpleCursorAdapter.getCursor();
if (cursor != null && cursor.moveToPosition(position)) {
String category= cursor.getString(1);
String summary= cursor.getString(2);
String description=cursor.getString(3);
long id= cursor.getLong(cursor.getColumnIndex(NotesContract.NotesTable._ID));
int locationId= cursor.getInt(cursor.getColumnIndex(NotesContract.NotesTable.COLUMN_LOCATION));
String [] retrievedData= {category, summary, description};
if (getActivity().findViewById (R.id.fragment_container)!=null){
//two pane layout
listView.setFocusable(true);
listView.setSelection(position);
Bundle args = new Bundle();
args.putStringArray("data",retrievedData);
/*args.putInt("update", 1);*/
args.putLong("id", id);
args.putInt("locationId", locationId);
mCallback.onlistElementClicked(args );/*this is available in the parent activity*/
}
else {
// one pane layout:
Intent intent = new Intent(getActivity(), NotesDetails.class);
intent.putExtra(Intent.EXTRA_TEXT, retrievedData);
/*intent.putExtra("update", 1); */ //to indicate that the query should be update not insert.
intent.putExtra("id", id);
intent.putExtra("locationId", locationId); //whether it is 0 or 1
startActivity(intent);
}
}//end outer cursor if.
}
});
return rootView;
}
这是行布局的 xml 文件,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/staricon"
android:visibility="visible">
</ImageView>
<ImageView
android:id="@+id/icon2"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/drafticon"
android:visibility="gone">
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:lines="1"
android:text="@+id/TextView01"
android:textSize="24dp"
>
</TextView>
</LinearLayout>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <!--text will be set dynamically -->
</LinearLayout>
但是有一个问题,我只想更改特定项目的图像(基于 intent 值),但是当图像更改时,所有其他图像都会更改。我认为发生这种情况是因为 simplecursoradapter 对所有项目使用相同的 xml 文件。但是,我找不到解决方案,它只是不断更改所有图像视图。我需要更改其图像视图的项目是最后保存的项目。我有另一个 activity,用户在其中创建笔记,并指定它们的类型(草稿或重要的),当用户单击保存时,该项目被添加到列表中,图像视图应根据类型更改(使用意图),这也适用于用户编辑列表中的现有项目时。
任何人都可以为这个问题提出解决方案吗?
非常感谢任何帮助。
谢谢。
I only want the image of specific item to be changed (based on the
intent value)
确实如此,所以您必须将您的意图值与此项目进行比较。而Item是基于ListView中的位置.
因此,只需根据位置从列表中获取数据对象,并将其与 Intent 的值进行比较,并据此更改 Imageview 的可见性。
并且在图像可见性的getView()
方法中添加else部分作为相反。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = super.getView(position, convertView, parent);
Cursor cursor = getCursor();
ImageView image = (ImageView) item.findViewById(R.id.icon);
ImageView image2= (ImageView) item.findViewById(R.id.icon2);
// Add one more comparison expression for data object position value which has draft value
if (cursor.getString(1).equalsIgnoreCase("draft")){
image.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
}else if (cursor.getString(1).equalsIgnoreCase("important"))
{
image.setVisibility(View.VISIBLE);
image2.setVisibility(View.GONE);
}
return item;
}
如果您的意思是您的行对象已更新,您必须在适配器的数据中更改它的内容并触发 RowUpdateds。
这样您的视图将使用新值重新创建。
我想根据一些意图值更改列表视图中的图像视图。为此,我确实覆盖了简单游标适配器的 class,如下所示,
public class NotesAdapter extends SimpleCursorAdapter {
Intent intent;
public NotesAdapter(Context context, int layout, Cursor c, String[] from, int[] to, Intent i) {
super(context, layout, c, from, to);
intent=i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = super.getView(position, convertView, parent);
ImageView image = (ImageView) item.findViewById(R.id.icon);
ImageView image2= (ImageView) item.findViewById(R.id.icon2);
if (intent.getStringExtra("category")!=null && intent.getStringExtra("category").equalsIgnoreCase("draft")){
image.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
}
return item;
}
}
在包含列表视图的 activity 中,我定义了 NotesAdapter 并在片段 onCreateView 方法中初始化它,如下所示,
mSimpleCursorAdapter = new NotesAdapter(getActivity(), R.layout.notes_row, null, from, to, getActivity().getIntent());
这是我初始化适配器的代码,
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todo_list, container, false);
mSimpleCursorAdapter = new NotesAdapter(getActivity(), R.layout.notes_row, null, from, to, getActivity().getIntent());
getLoaderManager().initLoader(LOADER_ID, null, this); //once this is done onCreateLoader will be called.
final ListView listView = (ListView) rootView.findViewById(R.id.notes_list); //findViewById must be called using the rootView because we are inside a fragment.
listView.addFooterView(new View(getActivity()), null, true);
text= (TextView) rootView.findViewById(R.id.empty_list);
listView.setAdapter(mSimpleCursorAdapter);
if(mSimpleCursorAdapter.isEmpty()) {
text.setVisibility(View.VISIBLE);
listView.setEmptyView(text);
if (getActivity().findViewById (R.id.fragment_container)!=null){
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id. fragment_container, new EmptyEditor()).commit();
}
}
else {
text.setVisibility(View.INVISIBLE);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Cursor cursor = mSimpleCursorAdapter.getCursor();
if (cursor != null && cursor.moveToPosition(position)) {
String category= cursor.getString(1);
String summary= cursor.getString(2);
String description=cursor.getString(3);
long id= cursor.getLong(cursor.getColumnIndex(NotesContract.NotesTable._ID));
int locationId= cursor.getInt(cursor.getColumnIndex(NotesContract.NotesTable.COLUMN_LOCATION));
String [] retrievedData= {category, summary, description};
if (getActivity().findViewById (R.id.fragment_container)!=null){
//two pane layout
listView.setFocusable(true);
listView.setSelection(position);
Bundle args = new Bundle();
args.putStringArray("data",retrievedData);
/*args.putInt("update", 1);*/
args.putLong("id", id);
args.putInt("locationId", locationId);
mCallback.onlistElementClicked(args );/*this is available in the parent activity*/
}
else {
// one pane layout:
Intent intent = new Intent(getActivity(), NotesDetails.class);
intent.putExtra(Intent.EXTRA_TEXT, retrievedData);
/*intent.putExtra("update", 1); */ //to indicate that the query should be update not insert.
intent.putExtra("id", id);
intent.putExtra("locationId", locationId); //whether it is 0 or 1
startActivity(intent);
}
}//end outer cursor if.
}
});
return rootView;
}
这是行布局的 xml 文件,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/staricon"
android:visibility="visible">
</ImageView>
<ImageView
android:id="@+id/icon2"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/drafticon"
android:visibility="gone">
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:lines="1"
android:text="@+id/TextView01"
android:textSize="24dp"
>
</TextView>
</LinearLayout>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <!--text will be set dynamically -->
</LinearLayout>
但是有一个问题,我只想更改特定项目的图像(基于 intent 值),但是当图像更改时,所有其他图像都会更改。我认为发生这种情况是因为 simplecursoradapter 对所有项目使用相同的 xml 文件。但是,我找不到解决方案,它只是不断更改所有图像视图。我需要更改其图像视图的项目是最后保存的项目。我有另一个 activity,用户在其中创建笔记,并指定它们的类型(草稿或重要的),当用户单击保存时,该项目被添加到列表中,图像视图应根据类型更改(使用意图),这也适用于用户编辑列表中的现有项目时。
任何人都可以为这个问题提出解决方案吗?
非常感谢任何帮助。
谢谢。
I only want the image of specific item to be changed (based on the intent value)
确实如此,所以您必须将您的意图值与此项目进行比较。而Item是基于ListView中的位置.
因此,只需根据位置从列表中获取数据对象,并将其与 Intent 的值进行比较,并据此更改 Imageview 的可见性。
并且在图像可见性的getView()
方法中添加else部分作为相反。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = super.getView(position, convertView, parent);
Cursor cursor = getCursor();
ImageView image = (ImageView) item.findViewById(R.id.icon);
ImageView image2= (ImageView) item.findViewById(R.id.icon2);
// Add one more comparison expression for data object position value which has draft value
if (cursor.getString(1).equalsIgnoreCase("draft")){
image.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
}else if (cursor.getString(1).equalsIgnoreCase("important"))
{
image.setVisibility(View.VISIBLE);
image2.setVisibility(View.GONE);
}
return item;
}
如果您的意思是您的行对象已更新,您必须在适配器的数据中更改它的内容并触发 RowUpdateds。
这样您的视图将使用新值重新创建。