使用ImageView和TextView的ListView,向下滚动向上设置一个随机项

时间:2013-05-15 06:35:01

标签: android android-listview

我有一个包含ImageView和TextView的列表。我读了扫描时间,并且必须设置textview的指定位置。这是以正确的方式完成的,但是在向下滚动然后向上设置Listview中的另一个文本视图。我看到这不是新的,但我无法解决这个问题。任何想法? BaseAdapter如下。我尝试了一周多但没有解决方案

  @Override
        public View getView( final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                Log.d("convertView", "NULLL");


                convertView = LayoutInflater.from(mContenxt).inflate(
                        R.layout.list_items, null);

                holder = new ViewHolder();

                holder.textView = (TextView) convertView
                        .findViewById(R.id.placeName);

                holder.imageView = (ImageView) convertView
                        .findViewById(R.id.itemSymbole);

                holder.textDate = (TextView) convertView
                        .findViewById(R.id.txtViewTimeAndDate);

                holder.imageMap = (ImageView)convertView.findViewById(R.id.imageViewMap);

                holder.linearLayoutText = ( LinearLayout)convertView.findViewById(R.id.txtViewList);


                holder.linearLayoutImage = ( LinearLayout)convertView.findViewById(R.id.thumbnail);

                convertView.setTag(holder);

            }
                Log.d("convertView", "Not NULLLL");
                holder = (ViewHolder) convertView.getTag();


            holder.id = position;

            holder.textView.setText(items.get(position).get("ProductName"));
            holder.imageView.setId(position);


            holder.linearLayoutText.setBackgroundColor(position);


            if (items.get(position).get("Identifier")
                    .equals(com.metasec.wachmann.ItemActivity.uID)) {

                holder.linearLayoutText.setBackgroundColor(Color.CYAN);

                holder.imageView.setImageResource(R.drawable.ok);

                Log.d("TIMEEEE", ItemActivity.scannDateAndTime);

                holder.textDate.setText(ItemActivity.scannDateAndTime);
                notifyDataSetChanged();
            }else {

               holder.imageView.setImageResource(R.drawable.not_ok);
               holder.textDate.setText("");
               notifyDataSetChanged();
            }

            holder.linearLayoutText.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    holder.linearLayoutText.setBackgroundColor(position);
                    holder.linearLayoutText.setBackgroundColor(Color.RED);
                    notifyDataSetChanged();

                    String placeName = items.get(position).get("ProductName");
                    String identifier = items.get(position).get("Identifier");
                    String itemId = items.get(position).get("ItemId");
                    String propertyBaseId = null;

                    String gpsCoordinates = null;

                    GPSTracker gpsTracker = new GPSTracker(mContenxt);
                    gpsTracker.stopUsingGPS();

                    double latitude = 0;
                    double longitude = 0;

                    if (gpsTracker.canGetLocation()) {
                        latitude = gpsTracker.getLatitude();
                        longitude = gpsTracker.getLongitude();

                        gpsCoordinates = latitude + ", " + longitude;
                    }

                    for (int k = 0; k < LoggedActivity.propertyBaseList.size(); k++) {
                        if (LoggedActivity.propertyBaseList.get(k).getName()
                                .equals("xxxxxxx")) {

                            propertyBaseId = LoggedActivity.propertyBaseList.get(k)
                                    .getPropertyBaseId();


                            Intent intentSenderDataToServer = new Intent(mContenxt,
                                    SenderReportDemageToServerActivity.class);



                            // give ItemId
                            intentSenderDataToServer.putExtra(ITEM_ID, itemId);

                            // give GroupId
                            intentSenderDataToServer.putExtra(PRODUCT_NAME,
                                    placeName);

                            // give GroupName
                            intentSenderDataToServer.putExtra(IDENTIFIER,
                                    identifier);

                            // give PropertyBaseID
                            intentSenderDataToServer.putExtra(PROPERTY_BASE_ID,
                                    propertyBaseId);

                            // give GPS Coordinates
                            intentSenderDataToServer.putExtra(GPS_COORDINATES,
                                    gpsCoordinates);

                            Log.d("items.get(position).get(ProductNNNNName)", items
                                    .get(position).get("ProductName"));

                            intentSenderDataToServer.putExtra("ProductName",
                                    placeName);
                            intentSenderDataToServer.putExtra("Identifier",
                                    identifier);
                            mContenxt.startActivity(intentSenderDataToServer);

                        } // END IF Loop

                    } // END FOR Loop

                }
            });



            holder.imageMap.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    String placeName = items.get(position).get("ProductName");
                    String identifier = items.get(position).get("Identifier");
                    String itemId = items.get(position).get("ItemId");


                    Log.d("placeName11",placeName);
                    Log.d("identifier111",identifier);
                    Log.d("itemId111",itemId);

                    Intent intentWachmanMapActivitiy = new Intent(mContenxt,
                            WachmanOpenStreetMapViewActivity.class);


                    intentWachmanMapActivitiy.putExtra(ITEM_ID, itemId);

                    // give GroupId
                    intentWachmanMapActivitiy.putExtra(PRODUCT_NAME,
                            placeName);

                    // give GroupName
                    intentWachmanMapActivitiy.putExtra(IDENTIFIER,
                            identifier);

                    Log.d("items.get(position).get(ProductNNNNName)", items
                            .get(position).get("ProductName"));

                    intentWachmanMapActivitiy.putExtra("ProductName",
                            placeName);
                    intentWachmanMapActivitiy.putExtra("Identifier",
                            identifier);


                    mContenxt.startActivity(intentWachmanMapActivitiy);

                }
            });

            return convertView;
        }

        static class ViewHolder {
            TextView textView = null;
            TextView textDate = null;
            CheckBox checkbox = null;
            ImageView imageView = null;
            ImageView imageMap = null;
            ImageButton imageButtonMap = null;

            LinearLayout linearLayoutText = null;
            LinearLayout linearLayoutImage = null;
            int id;

        }

以下类显示了我自己的适配器的调用位置。我backgroudn我必须调用我的适配器,前一次和数字必须保留。

private class SendBackground extends AsyncTask<Void, Void, Boolean>
{
    @Override
    protected void onPreExecute() {
          mProgDialog = new ProgressDialog(getBaseContext());
          mProgDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          mProgDialog = ProgressDialog.show(ItemActivity.this, "Sending", " Please Wait" );

    }


    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO Auto-generated method stub           
         return httpRequest.postData(itemIdTmp, propertyBaseIdtmp, uID, coordinatesTmp);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        Log.d("SendBackground", "onPostExecute");

        if (result == true) {
            Log.d("onPostExecute", "TRUE");
            //mProgressBar.setVisibility(View.GONE);

            mProgDialog.dismiss();
            Toast.makeText(getApplicationContext(), "Data sent..",
                    Toast.LENGTH_LONG).show();

                    /*----------------------------->>>> Whenever from Server True is return then my oown adapter hast to be adapted, as mentioned the state and time of previous items have to be remained */
            //setListAdapter(myBaseAdapterItemActivity);
            listView.setAdapter(myBaseAdapterItemActivity);
            myBaseAdapterItemActivity.notifyDataSetChanged();
        } else {

            mProgDialog.dismiss();

            Toast.makeText(getApplicationContext(),
                    "Sending Data to Server failed", Toast.LENGTH_LONG)
                    .show();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

//阅读我写的所有需要​​修改的评论 //不要在适配器内调用notify dataset change。 //如果它不起作用,那么我将编写一个示例代码

@Override
 public View getView( final int position, View convertView, ViewGroup parent) {

     ViewHolder holder = null; // use it 

     if (convertView == null) {
         Log.d("convertView", "NULLL");


         convertView = LayoutInflater.from(mContenxt).inflate(
                 R.layout.list_items, null);

         holder = new ViewHolder();

         holder.textView = (TextView) convertView
                 .findViewById(R.id.placeName);

         holder.imageView = (ImageView) convertView
                 .findViewById(R.id.itemSymbole);

         holder.textDate = (TextView) convertView
                 .findViewById(R.id.txtViewTimeAndDate);

         holder.imageMap = (ImageView)convertView.findViewById(R.id.imageViewMap);

         holder.linearLayoutText = ( LinearLayout)convertView.findViewById(R.id.txtViewList);


         holder.linearLayoutImage = ( LinearLayout)convertView.findViewById(R.id.thumbnail);

         convertView.setTag(holder);

     } else {
         holder = (ViewHolder) convertView.getTag(); // new changes
         Log.d("convertView", "Not NULLLL");
     }

     holder.id = position;

     holder.textView.setText(items.get(position).get("ProductName"));
     holder.imageView.setId(position); // why you set position it will cause error


     //holder.linearLayoutText.setBackgroundColor(position);


     if (items.get(position).get("Identifier")
             .equals(com.metasec.wachmann.ItemActivity.uID)) {

         holder.linearLayoutText.setBackgroundColor(Color.CYAN);

         holder.imageView.setImageResource(R.drawable.ok);

         Log.d("TIMEEEE", ItemActivity.scannDateAndTime);

         holder.textDate.setText(ItemActivity.scannDateAndTime);
        // notifyDataSetChanged(); no need to call it.
     }else {

        holder.imageView.setImageResource(R.drawable.not_ok);
        holder.linearLayoutText.setBackgroundColor(Color.GREEN);
        holder.textDate.setText("");
        //notifyDataSetChanged(); no need to call it.
     }

     holder.linearLayoutText.setTag(position);
     holder.linearLayoutText.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {

            // holder.linearLayoutText.setBackgroundColor(position);
             holder.linearLayoutText.setBackgroundColor(Color.RED);
            // notifyDataSetChanged();

             int selectedPosition = (Integer)v.getTag();

             String placeName = items.get(selectedPosition).get("ProductName");
             String identifier = items.get(selectedPosition).get("Identifier");
             String itemId = items.get(selectedPosition).get("ItemId");
             String propertyBaseId = null;

             String gpsCoordinates = null;

             GPSTracker gpsTracker = new GPSTracker(mContenxt);
             gpsTracker.stopUsingGPS();

             double latitude = 0;
             double longitude = 0;

             if (gpsTracker.canGetLocation()) {
                 latitude = gpsTracker.getLatitude();
                 longitude = gpsTracker.getLongitude();

                 gpsCoordinates = latitude + ", " + longitude;
             }

             // you are starting activity in lop it is dangerous because
             // you are launching many activity.
             // you should pass array 
             // i do not know are requirement.

             for (int k = 0; k < LoggedActivity.propertyBaseList.size(); k++) {
                 if (LoggedActivity.propertyBaseList.get(k).getName()
                         .equals("xxxxxxx")) {

                     propertyBaseId = LoggedActivity.propertyBaseList.get(k)
                             .getPropertyBaseId();


                     Intent intentSenderDataToServer = new Intent(mContenxt,
                             SenderReportDemageToServerActivity.class);



                     // give ItemId
                     intentSenderDataToServer.putExtra(ITEM_ID, itemId);

                     // give GroupId
                     intentSenderDataToServer.putExtra(PRODUCT_NAME,
                             placeName);

                     // give GroupName
                     intentSenderDataToServer.putExtra(IDENTIFIER,
                             identifier);

                     // give PropertyBaseID
                     intentSenderDataToServer.putExtra(PROPERTY_BASE_ID,
                             propertyBaseId);

                     // give GPS Coordinates
                     intentSenderDataToServer.putExtra(GPS_COORDINATES,
                             gpsCoordinates);

                     Log.d("items.get(position).get(ProductNNNNName)", items
                             .get(position).get("ProductName"));

                     intentSenderDataToServer.putExtra("ProductName",
                             placeName);
                     intentSenderDataToServer.putExtra("Identifier",
                             identifier);
                     mContenxt.startActivity(intentSenderDataToServer);

                 } // END IF Loop

             } // END FOR Loop

         }
     });

     holder.imageMap.setTag(postion);

     holder.imageMap.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
             // TODO Auto-generated method stub
             int selectedPosition = (Integer)v.getTag();
             String placeName = items.get(selectedPosition).get("ProductName");
             String identifier = items.get(selectedPosition).get("Identifier");
             String itemId = items.get(selectedPosition).get("ItemId");


             Log.d("placeName11",placeName);
             Log.d("identifier111",identifier);
             Log.d("itemId111",itemId);

             Intent intentWachmanMapActivitiy = new Intent(mContenxt,
                     WachmanOpenStreetMapViewActivity.class);


             intentWachmanMapActivitiy.putExtra(ITEM_ID, itemId);

             // give GroupId
             intentWachmanMapActivitiy.putExtra(PRODUCT_NAME,
                     placeName);

             // give GroupName
             intentWachmanMapActivitiy.putExtra(IDENTIFIER,
                     identifier);

             Log.d("items.get(position).get(ProductNNNNName)", items
                     .get(position).get("ProductName"));

             intentWachmanMapActivitiy.putExtra("ProductName",
                     placeName);
             intentWachmanMapActivitiy.putExtra("Identifier",
                     identifier);
             mContenxt.startActivity(intentWachmanMapActivitiy);

         }
     });

     return convertView;
 }

 static class ViewHolder {
     TextView textView = null;
     TextView textDate = null;
     CheckBox checkbox = null;
     ImageView imageView = null;
     ImageView imageMap = null;
     ImageButton imageButtonMap = null;

     LinearLayout linearLayoutText = null;
     LinearLayout linearLayoutImage = null;
     int id;

 }