Android:从代码中使用new.xml更新ListViewItem
我有一个带有OnClick方法的ListView,我想更改单击的ListViewItem的布局。我想通过将布局设置为新的XML来实现这一点。当视图加载时,我在ArrayAdapter的getView方法中通过Android:从代码中使用new.xml更新ListViewItem,android,android-listview,android-arrayadapter,layout-inflater,Android,Android Listview,Android Arrayadapter,Layout Inflater,我有一个带有OnClick方法的ListView,我想更改单击的ListViewItem的布局。我想通过将布局设置为新的XML来实现这一点。当视图加载时,我在ArrayAdapter的getView方法中通过 LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if(position % 2 == 0){
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position % 2 == 0){
v = vi.inflate(R.layout.wizard_listview_rowlayout_even, null);
} else {
v = vi.inflate(R.layout.wizard_listview_rowlayout_odd, null);
}
这是有效的,因为该方法返回膨胀视图。我尝试在onClick方法中使用它
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.wizard_listview_rowlayout_checked, null);
但这不起作用,可能是因为从未返回视图(v)。有没有办法操纵视图(ListViewItem)以更新应用程序中的布局
我尝试过使用选择器,但它只会使我的应用程序崩溃,我觉得我很接近使用这种方法
谢谢:)
编辑:这是我完整的ListViewFiller类:
public class ListViewFiller extends ListActivity {
private Context context;
private ArrayList<String> items;
private final ListView listview;
ListViewFiller(final ListView listview, ArrayList<String> items, Context appContext){
context = appContext;
this.listview = listview;
this.items = items;
ArrayAdapter adapter = new StudiesAdapter(appContext, android.R.layout.simple_list_item_1, items);
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
v.setSelected(true);
print();
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.wizard_listview_rowlayout_checked, null);
listview.invalidate();
}
});
}
public class StudiesAdapter extends ArrayAdapter<String> {
private ArrayList<String> studies;
public StudiesAdapter(Context context, int textViewResourceId, ArrayList<String> studies) {
super(context, textViewResourceId, studies);
this.studies = studies;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (true) { //opprinnelig if(v == null)
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position % 2 == 0){
v = vi.inflate(R.layout.wizard_listview_rowlayout_even, null);
} else {
v = vi.inflate(R.layout.wizard_listview_rowlayout_odd, null);
}
}
String study = studies.get(position);
if (study != null) {
TextView text = (TextView) v.findViewById(R.id.label);
if (text != null) {
text.setText(study);
Typeface face = Typeface.createFromAsset(context.getAssets(), "MyriadWebPro-Bold.ttf");
text.setTypeface(face);
}
}
return v;
}
}
公共类ListViewFiller扩展ListActivity{
私人语境;
私有ArrayList项;
私有最终列表视图列表视图;
ListViewFiller(最终ListView ListView、ArrayList项、Context appContext){
context=appContext;
this.listview=listview;
这个项目=项目;
ArrayAdapter=new StudiesAdapter(appContext,android.R.layout.simple\u list\u item\u 1,items);
setAdapter(适配器);
listview.setChoiceMode(listview.CHOICE\u MODE\u MULTIPLE);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图v、,
内部位置,长id){
v、 已选择(正确);
打印();
LayoutInflater vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.layout.wizard\u listview\u rowlayout\u选中,空);
listview.invalidate();
}
});
}
公共类StudiesAdapter扩展了ArrayAdapter{
私人ArrayList研究;
public StudiesAdapter(上下文、int-textViewResourceId、ArrayList研究){
super(上下文、textViewResourceId、研究);
这项研究=研究;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
if(true){//opprinnelig if(v==null)
LayoutInflater vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
如果(位置%2==0){
v=vi.充气(R.layout.wizard\u listview\u rowlayout\u偶数,null);
}否则{
v=vi.充气(R.layout.wizard\u listview\u rowlayout\u odd,null);
}
}
字符串研究=研究.get(位置);
如果(研究!=null){
TextView text=(TextView)v.findViewById(R.id.label);
如果(文本!=null){
text.setText(研究);
Typeface=Typeface.createFromAsset(context.getAssets(),“MyriadWebPro Bold.ttf”);
text.setTypeface(字体);
}
}
返回v;
}
}
}
我试图实现的是,当单击列表项(并调用onItemClick方法)时,我希望将该项的布局设置为xml文件“R.layout.wizard\u listview\u rowlayout\u checked”。您可以更改指定行的视图,然后调用list view的invalidate()方法来重新绘制整个listview 我发现: ListView显然多次调用getView方法。为了解决这个问题,我必须在getView中检查特定位置的视图是否被选中,并基于此将XML设置为选中、奇数或偶数
在clickListener中,我将项目设置为选中。您的问题不清楚。。发布您的
getView
的完整代码以及您想要实现的目标更新了我的帖子,我使用items arraylist中的元素填充我的ListView的完整类(每个元素都是一个带有研究名称的字符串)。我还尝试调用这里建议的invalidate()方法,但没有立即得到结果。我尝试了这个方法,但没有成功。用代码和无效调用更新了我的帖子。我做得对吗?