2017-05-01 45 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等)

我會稍後編輯這個

相關問題