将数据库URL中的另一个图像添加到Android studio列表视图

时间:2017-05-01 10:03:25

标签: android listview android-studio imageview picasso

我有一个工作列表视图,其中包含来自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;
}
}

这些是图像的吸气剂和设定者

 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;
}

}

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

请显示您要实施的代码。

  

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

以及要实现的事件(onClick,onItemClick等...)

我稍后会编辑

答案 1 :(得分:-1)

在RecyclerView中执行此操作。执行并不困难..你的错误在于viewholder ...阅读Recycler视图,不会有任何问题。