Отправьте статическое изображение и текст элемента списка к следующему действию, нажимая на действие списка в Android - PullRequest
0 голосов
/ 29 июня 2011

У меня есть список действий, в котором я показываю изображение и текст в ряд.

Изображения статичны и помещаются в папку для рисования.

Я хочу напечатать изображения и текст в следующем упражнении, нажимая на элемент списка.Я могу успешно отправить текстовое представление о следующей деятельности.Но мне было трудно отправить изображение в следующее окно.Потому что это статично.

Может кто-нибудь помочь мне в этом вопросе.Заранее спасибо.

      public class Myimage extends ListActivity {

        private LayoutInflater mInflater;
        static Vector<RowData> data = new Vector<RowData>();
        RowData rd;
        static boolean fromCategory = false;
        static final String[] title = new String[] {
                "One", "Two", "Three "};

        private Integer[] img = {
          R.drawable.i,R.drawable.im,R.drawable.ima};
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mInflater = 

(LayoutInflater) getSystemService(
    Activity.LAYOUT_INFLATER_SERVICE);

    for(int i=0;i<title.length;i++){
    try {
        rd = new RowData(i,title[i]);
        } catch (Exception e) {
            e.printStackTrace();
       }
       data.add(rd);
    }
       CustomAdapter adapter = new CustomAdapter(this, R.layout.list,
                                         R.id.title, data);
       setListAdapter(adapter);
       getListView().setTextFilterEnabled(true);

    // This is the List Item Click method which i should work to send the image on next activity.




    public void onListItemClick(ListView parent, View v, int position,
                long id) { 
            Intent intent = new Intent(this,Second.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);






        } 
    public class RowData  {

               protected int mId;
               protected String mTitle;

               RowData(int id,String title){
               mId=id;
               mTitle = title;

            }
               public void setTitle(String title) {
                this.mTitle = title.trim();
            }

               public String getTitle() {
                return mTitle;
            }
               public void setId(int i){ 
                   this.mId=i;
                   }
               public int getId(){
                   return mId;
               }


               @Override
               public String toString() {
                       return mId+" "+mTitle;
               }
        }




          private class CustomAdapter extends ArrayAdapter<RowData> {
          public CustomAdapter(Context context, int resource,
                                int textViewResourceId, List<RowData> objects) {               
         super(context, resource, textViewResourceId, objects);
        }
              @Override
               public View getView(int position, View convertView, ViewGroup parent) {   
               ViewHolder holder = null;
               TextView title = null;

               ImageView i11=null;
               RowData rowData= getItem(position);
               if(null == convertView){
                    convertView = mInflater.inflate(R.layout.list, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
         }
                     holder = (ViewHolder) convertView.getTag();
                     title = holder.gettitle();
                     title.setText(rowData.mTitle);

                     i11=holder.getImage();
                     i11.setImageResource(img[rowData.mId]);
                     return convertView;
        }
                    private class ViewHolder {
                    private View mRow;
                    private TextView title = null;

                    private ImageView i11=null; 
                    public ViewHolder(View row) {
                    mRow = row;
         }
                 public TextView gettitle() {
                     if(null == title){
                         title = (TextView) mRow.findViewById(R.id.title);
                        }
                    return title;
                 }     

                public ImageView getImage() {
                     if(null == i11){
                          i11 = (ImageView) mRow.findViewById(R.id.img);
                                              }
                        return i11;
                }
             }
           } }

1 Ответ

1 голос
/ 29 июня 2011

Вы должны использовать класс Bundle для отправки текста и изображения.Я предлагаю вам отправлять imageid (R.id.imageID), а не отправлять в дыру ImageView.

  //Use It for setting value of Bundle variable 
  Intent intent = new Intent(CourtDetailActivity.thisDrivingDirection.class);
  intent.putExtra("CourtId", courtId);
  startActivity(intent);
   //Use It for geting value of Bandle variable 
   Bundle extras = getIntent().getExtras();
   courtId = extras != null ? extras.getString("ImageId") : "";

     //You just copy and past given blow code in your code
     public void onListItemClick(ListView parent, View v, int position,
            long id) { 
        Intent intent = new Intent(this,Second.class);

       intent.putExtra("ImageId", ""+img[position]);
        startActivity(intent);

    } 

Надеюсь, это полезно для вас

...