如何将图像插入列表视图?

时间:2014-09-04 19:30:14

标签: android image listview visible invisible

我想在列表视图中的每个项目的右侧插入一个小图片 基本上我的应用程序应该这样做,一旦用户点击列表视图中的项目,图像变得可见,否则它必须保持不可见。

下面是我的XML活动

活动

public class EpisodiActivity extends Activity {

public class ViewModel {
    private String url;
    private String name;

    public ViewModel(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public String getUrl() {
        return this.url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return this.name;
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //creazione fullscreen activity
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.episodi_activity);

    String[] episodi = getIntent().getStringArrayExtra("Product");
    String[] urls = getIntent().getStringArrayExtra("urls");

    ListView mylist = (ListView) findViewById(R.id.listView1);


    // And in this loop we create the ViewModel instances from 
    // the name and url and add them all to a List
    List<ViewModel> models = new ArrayList<ViewModel>();
    for (int i = 0; i < episodi.length; i++) {
        String name = episodi[i];
        String url = "No value";
        if (i < urls.length) {
            url = urls[i];
        }
        ViewModel model = new ViewModel(url, name);
        models.add(model);
    }


    // Here we create the ArrayAdapter and assign it to the ListView
    // We pass the List of ViewModel instances into the ArrayAdapter
    final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(this, android.R.layout.simple_list_item_1, models);

    mylist.setAdapter(adapter);


    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {

            // Here we get the ViewModel at the given position
            ViewModel model = (ViewModel) arg0.getItemAtPosition(position);

            // And the url from the ViewModel
            String url = model.getUrl();

            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    });
}  

XML

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/pubblicita"
    android:cacheColorHint="#ffd700"
    android:background="@drawable/sfondobottone" />

4 个答案:

答案 0 :(得分:2)

我认为你想在listview中输出这种输出

包含listview中图片的文字

enter image description here

您可以使用自定义列表视图。创建一个扩展BaseAdapter类的类

这是我正在使用的例子

您的BaseAdapter

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

    public class FrontListBaseAdapter extends BaseAdapter {
     private static ArrayList<FrontDetails> itemDetailsrrayList;

     private LayoutInflater l_Inflater;

     public FrontListBaseAdapter(Context context, ArrayList<FrontDetails> results) {
      itemDetailsrrayList = results;
      l_Inflater = LayoutInflater.from(context);
     }

     public int getCount() {
      return itemDetailsrrayList.size();
     }

     public Object getItem(int position) {
      return itemDetailsrrayList.get(position);
     }

     public long getItemId(int position) {
      return position;
     }

      // get the views in frontview xml file where you have
      // define multiple views that will appear in listview each row
     public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (convertView == null) {
       convertView = l_Inflater.inflate(R.layout.frontview, null);
       holder = new ViewHolder();
       holder.Image = (ImageView) convertView.findViewById(R.id.adminpic1);
       holder.MsgType = (TextView) convertView.findViewById(R.id.msgtype1);

       convertView.setTag(holder);
      } else {
       holder = (ViewHolder) convertView.getTag();
      }


      holder.Image.setImageResource(R.drawable.mainlogo); // you can set your setter here
      holder.MsgType.setText(itemDetailsrrayList.get(position).getMsgType());

      return convertView;
     }

     // holder view for views
     static class ViewHolder {
      ImageView Image;
      TextView MsgType;
     }
    }

您的FrontDetails类,您将在其中创建getter和setter,此类将在最终的ArrayList中使用resultse = new ArrayList();

import android.graphics.Bitmap;

public class FrontDetails {

    public int getImage() {
    return image;
    }
    public void setImage(int imageN) {
    this.image = imageN;
    }


    public String getMsgType() {
    return MsgType;
    }
    public void setMsgType(String text) {
    this.MsgType = text;
    }



    private int image;
    private String MsgType;

    }

您的frontview.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="100dp"
    android:orientation="vertical"
    android:layout_margin="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp" >

        <ImageView
            android:id="@+id/adminpic1"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >



            <TextView
                android:id="@+id/msgtype1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="4dp"
                android:textSize="1sp"
                android:text="MsgType" />


        </LinearLayout>

    </LinearLayout>

</LinearLayout>

和xml中的listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/sync"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sync" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" >
    </ListView>

</LinearLayout>

现在在您的主要活动中

    final ArrayList<FrontDetails> resultse = new ArrayList<FrontDetails>();
FrontListBaseAdapter asdf = new FrontListBaseAdapter(context, resultse);
                lv1.setAdapter(new FrontListBaseAdapter(Front.this, resultse));


 lv1.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {

                    Object o = lv1.getItemAtPosition(position);
                    FrontDetails obj_itemDetails = (FrontDetails)o;
                    Toast.makeText(context, "You have chosen " + ' ' + obj_itemDetails.getMsgType(), Toast.LENGTH_LONG).show();

    }
        });

修改: 从这里我学习了Custom Listview,它是一个带图像的简单例子

http://www.javasrilankansupport.com/2012/05/android-listview-example-with-image-and.html

http://www.javacodegeeks.com/2012/10/android-listview-example-with-image-and.html

答案 1 :(得分:1)

将自定义列表视图与BaseAdapter一起使用

您的适配器

public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;

public CustomBaseAdapter(Context context, List<RowItem> items) {
    this.context = context;
    this.rowItems = items;
}

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater)
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

@Override
public int getCount() {    
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

}

您的list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:contentDescription="@string/image"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/icon"
    android:paddingBottom="10dp"
    android:textColor="#CC0033"
    android:textSize="16dp" />

<TextView
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/title"
    android:layout_toRightOf="@+id/icon"
    android:paddingLeft="10dp"
    android:textColor="#3399FF"
    android:textSize="14dp" />

</RelativeLayout>

您的单行项目类

public class RowItem {
private int imageId;
private String title;
private String desc;

public RowItem(int imageId, String title, String desc) {
    this.imageId = imageId;
    this.title = title;
    this.desc = desc;
}
public int getImageId() {
    return imageId;
}
public void setImageId(int imageId) {
    this.imageId = imageId;
}
public String getDesc() {
    return desc;
}
public void setDesc(String desc) {
    this.desc = desc;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
@Override
public String toString() {
    return title + "\n" + desc;
}  
}

列出视图实现

listView = (ListView) findViewById(R.id.list);
    CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
    listView.setAdapter(adapter);

答案 2 :(得分:0)

我可以提供一些提示,但遗憾的是,无法通过示例帮助你。

首先创建一个自定义适配器(扩展BaseAdapter),然后创建一个自定义布局。 这里的自定义布局包含右侧的textview和一个图像视图(默认情况下不可见)。

只需使用适配器自定义列表视图,并通过get view()..

将文本放在TextView中

最后在listItemClickListener中,使图像的位置可见。

答案 3 :(得分:-1)

你可以像这样在上设置这个

android:visibility="visible" 

android:visibility="invisible" 

android:visibility="gone"

Java程序:

ImageView imgView = (ImageView)findViewById(R.id.custom);

像这样设置你的ImageView

imgView .setVisibility(View.VISIBLE);


imgView .setVisibility(View.INVISIBLE);


imgView .setVisibility(View.GONE);

INVISIBLE和GONE之间的区别。

INVISIBLE - 窗口小部件将不可见,但窗口小部件的空间将显示。

GONE - 空间和小部件都是不可见的。

现在您可以挂钩setOnItemClickListener()

listview.setOnItemClickListener(new OnItemClickListener()
{
 @Override 
 public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
 { 
    imgView .setVisibility(View.VISIBLE);
 }
});