2017-05-01 50 views
0

我有一个工作列表视图与drawables文件夹中的图像,我有工作代码需要一个图像并将其上传到我的服务器等,我有url获取图像从数据库中,我现在陷入了如何将它添加到我已经存在的列表视图通过自动添加一个新的图像从这个链接到列表视图。从数据库URL添加另一个图像到Android Studio列表视图

这是显示画面的“时间表”列表视图中我们已经有

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

}

非常感谢任何帮助

回答

-1

这样做,RecyclerView。有实现并不困难..你的错误是在viewholder中...阅读Recycler视图,不会有任何问题。

+2

这不会回答这个问题。 – azizbekian

+0

所以这意味着改变我的列表视图到一个循环视图,并尝试这种方式,我曾尝试过,但失败了,我沿着我的当前代码的所有权利? –

0

请显示您想要实现的代码。

我有工作代码需要的图像,并上传到我的服务器 等等,我知道该网址,从数据库中读取图像和我现在

,并为执行该事件( onClick,onItemClick等)

我会稍后编辑这个

相关问题