【问题标题】:Adding another image from database url to Android studio list view将数据库 url 中的另一个图像添加到 Android Studio 列表视图
【发布时间】:2017-09-28 18:23:40
【问题描述】:

我有一个工作列表视图,其中包含来自 drawables 文件夹的图像,我有工作代码,它获取图像并将其上传到我的服务器等,我有从数据库中获取图像的 url,我现在被困在如何通过自动将此链接中的新图像添加到列表视图中,将其添加到我现有的列表视图中。

这是“时间轴”列表视图,显示我们已有的图片

/**
 * Method which creates the list view on screen and displays images
 */
public class Timeline extends Activity implements OnItemClickListener {

//global variables
String[] pic_names;
TypedArray profile_pics;
List<RowItem> rowItems;
ListView mylistview;
ImageView btnTakePic;
String[] uploaded_pic_name;
TypedArray pic_url;

//Overridden method to create the main layout
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timeline);

    //set the global variables
    //rowItems is now an arraylist
    rowItems = new ArrayList<RowItem>();
    //pic_names is set to the resource of pic_names
    pic_names = getResources().getStringArray(R.array.pic_names);
    uploaded_pic_name = getResources().getStringArray(R.array.uploaded_pic_name);
    pic_url = getResources().obtainTypedArray(R.array.pic_url);
    //profile_pics is now set to the resource of profile_pics
    profile_pics = getResources().obtainTypedArray(R.array.profile_pics);

    //gets the picture and name for each resource in the for loop array
    for (int i = 0; i < pic_names.length; i++) {
        RowItem item = new RowItem(pic_names[i], profile_pics.getResourceId(i, -1));

        //adds items from the array
        rowItems.add(item);

    }

    RowItem uploadedItem = new RowItem(uploaded_pic_name[0], pic_url.getResourceId(0, 0));
    rowItems.add(uploadedItem);


    //creates a new listview
    mylistview = (ListView) findViewById(R.id.list);
    CustomAdapter adapter = new CustomAdapter(this, rowItems);
    mylistview.setAdapter(adapter);

    //onclick listener on this main activity
    mylistview.setOnItemClickListener(this);

    btnTakePic = (ImageView) findViewById(R.id.btnTakePic);






    // on click listener used to give function to the button when clicked.
    btnTakePic.setOnClickListener(new View.OnClickListener() {

        // onClick method defines what the function is
        // Intent used to communicate to start
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Timeline.this, Camera.class);
            startActivity(i);


        }

    });

}




//overridden method to show toast message on the picture
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {

    String pic_name = rowItems.get(position).getPic_name();
    Toast.makeText(getApplicationContext(), "" +  pic_name,
            Toast.LENGTH_SHORT).show();
}





}

这是我当前使用的自定义适配器类

/**
 * TODO
 */
public class CustomAdapter extends BaseAdapter {

//Instantiates getters for variables
Context context;
List<RowItem> rowItems;

//creates setters for variables
CustomAdapter(Context context, List<RowItem> rowItems) {
    this.context = context;
    this.rowItems = rowItems;
}

//Overridden method to get the size of the rows
@Override
public int getCount() {
    return rowItems.size();
}

//Overridden method to get the item position from rowItems array returning the position
@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

//Overridden method to get the Item id return the position
@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

/**
 *  private view holder class
 *
 */
private class ViewHolder {
    ImageView profile_pic;
    TextView pic_name;
}


// Overriden method to insert image and its associated xml in the listview
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    //Instantiating local variables
    ViewHolder holder;
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    //If the View is null create the layout
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        //set the textview and image view to required parameters
        holder.pic_name = (TextView) convertView.findViewById(R.id.pic_name);
        holder.profile_pic = (ImageView) convertView.findViewById(profile_pic);

        convertView.setTag(holder);

        //create a new viewholder and get the tag from the view
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    //getter for the position of the row
    RowItem row_pos = rowItems.get(position);

    //sets the position of the row
    holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
    holder.pic_name.setText(row_pos.getPic_name());

    //return the view
    return convertView;
}
}

这些是图像的 getter 和 setter

 public class RowItem {

private String pic_name;
private int profile_pic_id;

public RowItem(String pic_name, int profile_pic_id) {

    this.pic_name = pic_name;
    this.profile_pic_id = profile_pic_id;

}

//getter for the pic name
public String getPic_name() {
    return pic_name;
}

//setter for the pic name
public void setPic_name(String pic_name) {
    this.pic_name = pic_name;
}

//getter for the profile pic
public int getProfile_pic_id() {
    return profile_pic_id;
}

//setter for the profile pic
public void setProfile_pic_id(int profile_pic_id) {
    this.profile_pic_id = profile_pic_id;
}

}

非常感谢任何帮助

【问题讨论】:

    标签: android listview android-studio imageview picasso


    【解决方案1】:

    在 RecyclerView 中执行此操作。实现起来并不困难..您的错误在于viewholder...阅读Recycler view,不会有任何问题。

    【讨论】:

    • 这无论如何都不能回答这个问题。
    • 那么这是否意味着将我的列表视图更改为回收视图并尝试这种方式,我曾尝试过但失败了,我当前的代码是否完全正确?
    【解决方案2】:

    请展示您要实现的代码。

    我有工作代码,可以拍摄图像并将其上传到我的服务器 等等,我有从数据库中获取图像的 url,我现在

    以及在哪个事件上实现(onClick、onItemClick 等...)

    我稍后会编辑这个

    【讨论】:

      猜你喜欢
      • 2016-10-05
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 2020-02-12
      • 2021-03-27
      • 1970-01-01
      相关资源
      最近更新 更多