问题
I would like to manage to obtain a listview with dynamic items. I built a row_layout containing an ImageView, and 2 TextViews. When I build the adapter, I want to be able to display the ImageView OR NOT depending on the row number. So I want to have img1 on row 2, img2 on row 3, then img3 on row 5. And on all other rows I do not want any imageView displayed (and also the alignment of the textviews must change filling the empty space left by the ImageView missing).
Is that possible?
I populate my adapter here:
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
}
....
I have a class:
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;
}
}
and a custom Adapter class
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;
}
}
Also I have an activity layout (irrelevant) One listview and a bottom button (relativeLayout). And I have a row_layout which I use to display the rows.
<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>
- Where can I assign my pictures to my ImageView?
- And How can I make the ImageView "disappear" for certain rows?
Thank you
回答1:
Where can I assign my pictures to my ImageView?
Do this in the getView()
method of your Adapter
. It looks like you are doing that now.
And How can I make the ImageView "disappear" for certain rows?
Use anif
statement and if
it isn't the correct row (position
) call
holder.hldIcon.setVisibility(View.INVISIBLE)`
or
holder.hldIcon.setVisibility(View.GONE)
depending on what you want
来源:https://stackoverflow.com/questions/17982735/android-custom-layout-listview-with-images