开发android, layout_A 在 layout_B 里被include两次以上,如何设置各个layout_A内的TextView的值?
就是比如下面的形式
main.xml里面
<LinearLayout ...>
<include android:id="c1" layout="a"/>
<include android:id="c2" layout="a"/>
</LinearLayout>
a.xml里面
<LinearLayout ...>
<TextView ....>
</LinearLayout>
如何对c1和c2里面的TextView分别赋值 展开
设置listview中的item具体方法为:
重写BaseAdapter的getView()方法,在这个方法里去判断。
要用到Hodler的.我们把所有的不同的item项放在一个item项中.在getView()方法里去判断是否可见,即可。
例子:
1.MainActivity.java
package com.bivin;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainAcitivity extends Activity
{
private ListView lv;
ArrayList<HashMap<String, Object>> datas = new ArrayList<HashMap<String, Object>>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HashMap<String, Object> data;
for (int i = 0; i <30; i++)
{
data = new HashMap<String, Object>();
data.put("name", "name " + i);
data.put("age", "age " + i);
data.put("sex", "sex "+i);
datas.add(data);
}
lv = (ListView) findViewById(R.id.mylv);
Myadapter adapter = new Myadapter(this);
lv.setAdapter(adapter);
}
private class Myadapter extends BaseAdapter
{
private Context context;
public Myadapter(Context context)
{
this.context = context;
}
@Override
public int getCount()
{
return datas.size();
}
@Override
public Object getItem(int position)
{
return datas.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ItemHolder holder = null;
if (convertView == null)
{
holder = new ItemHolder();
LayoutInflater inflater =getLayoutInflater();
convertView =inflater.inflate(R.layout.item,null);
holder.tv = (TextView) convertView.findViewById(R.id.name);
holder.tv2 = (TextView) convertView.findViewById(R.id.age);
holder.tv3 = (TextView) convertView.findViewById(R.id.sex);
switch (position)
{
case 0:
holder.tv.setVisibility(View.GONE);
break;
case 1:
holder.tv.setVisibility(View.VISIBLE);
holder.tv2.setVisibility(View.GONE);
break;
case 2:
holder.tv3.setVisibility(View.GONE);
holder.tv2.setVisibility(View.VISIBLE);
break;
default:
holder.tv.setVisibility(View.VISIBLE);
holder.tv2.setVisibility(View.VISIBLE);
holder.tv3.setVisibility(View.VISIBLE);
break;
}
convertView.setTag(holder);
}else {
holder =(ItemHolder)convertView.getTag();
holder.tv.setText(datas.get(position).get("name").toString());
holder.tv2.setText(datas.get(position).get("sex").toString());
holder.tv3.setText(datas.get(position).get("age").toString());
}
return convertView;
}
}
}
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="wrap_content" android:id="@+id/mylv"
android:layout_height="wrap_content" />
<Button android:layout_width="wrap_content" android:id="@+id/mybutton"
android:layout_height="wrap_content" android:text="button" />
</LinearLayout>
3. item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="@+id/name" android:layout_width="fill_parent"
android:text="name" android:layout_height="wrap_content"></TextView>
<TextView android:layout_height="wrap_content" android:text="age"
android:id="@+id/age" android:layout_width="fill_parent"></TextView>
<TextView android:layout_width="fill_parent" android:text="sex"
android:id="@+id/sex" android:layout_height="wrap_content"></TextView>
</LinearLayout>
((TextView) findViewById(c2).findViewById(textviewid)).setText("Text2");
<include android:id="@+id/c2" layout="@layout/a" />
===>
TextView c1 = (TextView) (findViewById(R.id.c1).findViewById(R.id.textview));
c1.setText("test1");
TextView c2 = (TextView) (findViewById(R.id.c2).findViewById(R.id.textview));
c2.setText("test2");
这样应该看的懂了吧