我想设法获得包含动态项目的列表视图。 我构建了一个包含ImageView和2个TextView的row_layout。 当我构建适配器时,我希望能够显示ImageView OR NOT,具体取决于行号。 所以我想在行2上使用img1,在行3上使用img2,然后在第5行使用img3。在所有其他行上我不希望显示任何imageView(并且文本视图的对齐必须更改填充由ImageView缺失。)
这可能吗?
我在这里填充我的适配器:
JSONObject json_data;
try {
int length = lststat.length();
detalii_client = new Client[length];
for (int i = 0; i < length; i++) {
json_data = lststat.getJSONObject(i);
detalii_client[i] = new Client();
String categoria = json_data.getString("den");
if ("1".equals(categoria)){ // row 1
detalii_client[i].desc ="Some Title";
detalii_client[i].icon = 0; // here I want NO IMAGE
}
if ("2".equals(categoria)){ // row 2
detalii_client[i].desc ="Some other Title";
detalii_client[i].icon = 0; // here I want to assign a picture to the imageView
}
....
我有一个班级:
public class Client {
public int icon;
public String desc;
public String title;
public String id;
public Client(){
super();
}
public Client(int icon, String desc, String title, String id) {
super();
this.icon = icon;
this.desc = desc;
this.id = id;
this.title= title;
}
}
和自定义适配器类
public class ClientAdapter extends ArrayAdapter<Client>{
Context context;
int layoutResourceId;
Client data[] = null;
public ClientAdapter(Context context, int layoutResourceId, Client[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ClientHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ClientHolder();
holder.hldIcon = (ImageView)row.findViewById(R.id.imgIconx);
holder.hldTitle = (TextView)row.findViewById(R.id.txtTitlu);
holder.hldDesc = (TextView)row.findViewById(R.id.txtDescr);
holder.hldId = (TextView)row.findViewById(R.id.txtId);
row.setTag(holder);
}
else
{
holder = (ClientHolder)row.getTag();
}
Client clientul = data[position];
holder.hldTitle.setText(clientul.title);
holder.hldDesc.setText(clientul.desc);
holder.hldId.setText(clientul.id);
holder.hldIcon.setImageResource(clientul.icon);
return row;
}
static class ClientHolder
{
ImageView hldIcon;
TextView hldTitle;
TextView hldId;
TextView hldDesc;
}
}
我还有一个活动布局(不相关)一个列表视图和一个底部按钮(relativeLayout)。 我有一个row_layout,用于显示行。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgIconx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/client64" />
<LinearLayout
android:layout_width="145dp"
android:layout_height="match_parent"
android:layout_weight="1.50"
android:orientation="vertical" >
<TextView
android:id="@+id/txtDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/lbl_loc"
android:textColor="@color/darkred"
android:textSize="@dimen/descriere_detaliu"
android:typeface="serif" />
<TextView
android:id="@+id/txtTitlu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbl_client"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/darkred"
android:textSize="18sp"
android:textStyle="normal"
android:typeface="serif" />
<TextView
android:id="@+id/txtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbl_idviz"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white" />
</LinearLayout>
</RelativeLayout>
谢谢
答案 0 :(得分:1)
我可以在哪里将照片分配到ImageView?
在getView()
的{{1}}方法中执行此操作。看起来你现在正在这样做。
如何让ImageView对某些行“消失”?
使用Adapter
语句和if
这不是正确的行(if
)来电
position
或
holder.hldIcon.setVisibility(View.INVISIBLE)`
取决于你想要的东西