我想设法获得一个带有动态项目的列表视图。我构建了一个包含 ImageView 和 2 个 TextViews 的 row_layout。当我构建适配器时,我希望能够根据行号显示或不显示 ImageView。所以我想在第 2 行有 img1,在第 3 行有 img2,然后在第 5 行有 img3。在所有其他行上,我不希望显示任何 imageView(而且 textviews 的对齐方式必须改变填充由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>
- 我在哪里可以将我的图片分配给我的 ImageView?
- 以及如何使某些行的 ImageView “消失”?
谢谢