【发布时间】:2015-03-17 16:51:00
【问题描述】:
我正在制作一个应用程序,我想从另一个布局更改某个布局内的图像。那可能吗?在这种情况下,详细信息活动包含具有重要项目和草稿的微调器,并且有一个保存按钮将数据保存在数据库中。主要活动有一个列表视图,每一行由图像视图和文本视图组成。详细信息活动的 xml 文件与包含图像视图的行布局的 xml 文件不同。图像视图中的图像应根据已保存注释中微调器的值而更改。我知道 spinner 的值可以通过意图传递,并且我们可以在此基础上设置图像。但是由于引用了不在主要活动的 xml 文件中的图像视图的 ID,我得到了错误。我仅将行 xml 用作加载器中的参数,该加载器位于主活动中,其中主活动 xml 文件与行 xml 文件不同。换句话说,没有使用row xml文件作为其布局的活动,它只是在加载器中用作参数。那么,在这种情况下是否可以更改图像?
这是我在详细信息片段中使用的代码,用于将微调器值发送到另一个活动中的片段:
if (isUpdateQuery) {
long id = getActivity().getIntent().getLongExtra("id", 0);
int updated = getActivity().getContentResolver().update(NotesContract.NotesTable.buildUriWithId(id), values, null, null);
Intent intent = new Intent(getActivity(), NotesList.class);
intent.putExtra("category", category);
startActivity(intent);
}//en inner if
而且,这是我在片段中用于获取微调器值并更新图像的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View temp = inflater.inflate(R.layout.notes_row, container, false);
ImageView imageView = (ImageView) temp.findViewById(R.id.icon);
if (getActivity().getIntent().getStringExtra("category")!=null && getActivity().getIntent().getStringExtra("category").equalsIgnoreCase("important")){
imageView.setImageResource(R.drawable.staricon);
}
mSimpleCursorAdapter=new SimpleCursorAdapter(getActivity(),R.layout.notes_row,null, from, to,0);
View rootView = inflater.inflate(R.layout.fragment_todo_list, container, false);
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.
if(mSimpleCursorAdapter.getCount()==0) {
TextView text= (TextView) rootView.findViewById(R.id.empty_list);
text.setVisibility(View.VISIBLE);
}
if (getActivity().findViewById (R.id.fragment_container)!=null){
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);}//end if.
listView.setAdapter(mSimpleCursorAdapter);
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.setItemChecked(position, true);
listView.setBackgroundColor(Color.BLUE);
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;
}
尝试按 ID 查找图像时出现 NullPointerException。从第二个代码部分可以看出,我首先暂时膨胀包含图像的视图,以便能够通过 id 获取图像并更新它,然后我膨胀片段的原始布局(rootView),并且我返回了 rootView。那是对的吗? 注意:我发送意图的 Activity 的布局与行布局不同,并且该 Activity 内的片段也有其 on 布局。
感谢任何帮助。
谢谢
【问题讨论】:
-
太宽泛的问题,我建议你用代码 sn-p 和你得到的错误将它们分成块
-
好的,我会发布一些代码
标签: android xml android-layout android-inflate