【问题标题】:Android: ListView item click not working properlyAndroid:ListView 项目单击无法正常工作
【发布时间】:2016-07-13 10:42:03
【问题描述】:

我正在尝试实现一个“收藏夹”系统,所以当一个产品被选为收藏夹时(通过单击按钮),它被传递给一个名为 Favoris.java 的类,它将所有收藏夹的产品显示为一个 ListView,所有这些都可以单击以获取有关单击产品的更多详细信息。到目前为止,列表视图工作正常。

但这是我的主要问题:当我单击其中一个产品以获取其详细信息时,会显示另一个不在收藏夹列表中的产品

我确实注意到了一些可能会有所帮助的东西:通过在我的列表中添加一些收藏夹,比如 3,我注意到了一个模式:

  • 当我单击我添加为收藏的第一个产品时,我会获得所有可用产品中第一个产品的详细信息(有一个包含所有可用产品的主 ListView,这些产品都是从 xml 提要中检索的)
  • 当我点击我添加为收藏的第二个产品时,我会获得所有可用产品中第二个产品的详细信息。
  • 当我单击我添加为收藏的第三个产品时,我会获得所有可用产品中第三个产品的详细信息。

我目前正在将最喜欢的产品作为类对象传递给最喜欢的类,方法是使用 Gson 转换它并使用 SharedPrefenreces 传递它,然后 Favoris.java 类获取该产品并将其转换回类对象。

所以这是我所有我最喜欢的产品的列表视图,你可以看到有 4 个产品被添加为收藏夹:

当点击第一个产品 (Palais Héracles) 时,我得到另一个名为“Château Périgord”的产品(默认产品列表中的第一个)(你可以看到我没有将它添加为收藏夹,因为星形图标不是黑色而是白色):

这是产品详细信息的代码:

public class ProduitDetail extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

/**
 * fields
 */
private StackProduits produit;
private Button butStar;
/*images (main, thumbnails and gallery)*/
private ArrayList<String> imagesUrlListThumb, imagesUrlListFull = new ArrayList<String>();
private RecyclerAdapter recyclerAdapter;
private String urlRecyclerThumb = "";
/*view elements*/
private RecyclerView recyclerView;
private ImageView imgCurRecyclerView;
private Button btnMailCurProduct, btnMailAgency;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_produit_detail);

    /*get ids of the two star icons*/
    final int imgStarFull = R.drawable.ic_star;
    final int imgStarBorder = R.drawable.ic_star_border;

    /*get view references from content_produit_detail.xml*/
    butStar = (Button) findViewById(R.id.button_detail_heart);

    /*get produit class object from both Ventes.java and Location.java*/
    produit = new StackProduits();
    Intent intent = getIntent();
    produit = intent.getParcelableExtra("produit");


    /**handle favorites icon turned on or off when entering the product detail*/
    final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    Set<String> jsonList = mPrefs.getStringSet("listJson2", new HashSet<String>());
    Set<String> jsonList2 = new HashSet<>();
    jsonList2.addAll(jsonList);
    produit.setIsAddedAsFav("1");
    Gson gson = new Gson();
    String myJson = gson.toJson(produit);

    if (jsonList2.contains(myJson)) {
        butStar.setCompoundDrawablesWithIntrinsicBounds(imgStarFull, 0, 0, 0);
    } else {
        produit.setIsAddedAsFav("0");
    }

    /**
     * Listener for the star button used to make the current product favorite by adding it to a
     * list of all the products currently set as favorite
     */
    butStar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*Change the icon and make a toast when the heart button is pressed*/
            if (produit.getIsAddedAsFav().equalsIgnoreCase("0")) {
                produit.setIsAddedAsFav("1");
                butStar.setCompoundDrawablesWithIntrinsicBounds(imgStarFull, 0, 0, 0);

                SharedPreferences.Editor prefsEditor = mPrefs.edit();
                Gson gson = new Gson();

                //convert product to string via gson
                String myJson = gson.toJson(produit);
                //prefsEditor.putString("MyObject", myJson);

                //get the Set<String> reference back to keep the entire list of favorites
                Set<String> jsonList = mPrefs.getStringSet("listJson2", new HashSet<String>());

                //make a copy of Set<String> pref to make it usable
                Set<String> jsonList2 = new HashSet<>();

                //add the list and the new product to the copy of Set<String>
                jsonList2.addAll(jsonList);
                jsonList2.add(myJson);

                //add list to editor
                prefsEditor.putStringSet("listJson2", jsonList2);
                prefsEditor.apply();
                Toast.makeText(ProduitDetail.this, getString(R.string.toast_favorite_added), Toast.LENGTH_SHORT).show();
            } else {
                produit.setIsAddedAsFav("1");
                butStar.setCompoundDrawablesWithIntrinsicBounds(imgStarBorder, 0, 0, 0);
                SharedPreferences.Editor prefsEditor = mPrefs.edit();

                Set<String> jsonList = mPrefs.getStringSet("listJson2", new HashSet<String>());

                Set<String> jsonList2 = new HashSet<>();
                jsonList2.addAll(jsonList);

                Gson gson = new Gson();
                String myJson = gson.toJson(produit);

                jsonList2.remove(myJson);

                prefsEditor.putStringSet("listJson2", jsonList2);
                prefsEditor.apply();
                produit.setIsAddedAsFav("0");
                Toast.makeText(ProduitDetail.this, getString(R.string.toast_favorite_removed), Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}

这是我的班级处理收藏夹的代码:

public class Favoris extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

/**fields*/
private ListView produitFavorisListView;
private List<StackProduits> listProduits = new ArrayList<>();
private ProduitsAdapter adapterFavoris;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_favoris);

    /*get ListView reference*/
    produitFavorisListView = (ListView) findViewById(R.id.favoritesList);

    /*getting back data from shared preferences*/
    final SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    final Gson gson = new Gson();

     //getting back favorites
    Set<String> myJson = mPrefs.getStringSet("listJson2", new HashSet<String>());

    if (myJson.isEmpty() && listProduits.isEmpty()) {
        produitFavorisListView.setAdapter(null);
    }
    else if (myJson.isEmpty() && listProduits != null) {
        adapterFavoris.notifyDataSetChanged();
        adapterFavoris = new ProduitsAdapter(getApplicationContext(), -1, listProduits);
        produitFavorisListView.setAdapter(adapterFavoris);
    }
    else{
        //for each where we get back values from sting set, then convert to product
        for (String id : myJson) {
            StackProduits savedProduct = gson.fromJson(id, StackProduits.class);
            listProduits.add(savedProduct);
        }
        adapterFavoris = new ProduitsAdapter(getApplicationContext(), -1, listProduits);
        produitFavorisListView.setAdapter(adapterFavoris);
    }

    //Set the click listener to launch the browser when a row is clicked.
    produitFavorisListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
            Intent intentProduitFavorisDetail = new Intent(Favoris.this, ProduitDetail.class);
            StackProduits ProduitFavoris = ProduitsXmlPullParser.getStackProduitFromFile(Favoris.this).get(pos);
            intentProduitFavorisDetail.putExtra("produit", ProduitFavoris);
            startActivity(intentProduitFavorisDetail);
        }
    });
}
}

这是我收藏的 ListView 的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="13dp"
android:layout_marginTop="55dp">

<TextView
    android:id="@+id/content_favorite_emptylist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:visibility="gone"
    android:textColor="#343232"
    android:textSize="18sp"
    android:textStyle="bold"/>

<ListView
    android:id="@+id/favoritesList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

这是我的适配器类:

public class ProduitsAdapter extends ArrayAdapter<StackProduits> {

/**
 * fields
 */
ImageLoader imageLoader;
DisplayImageOptions options;
List<StackProduits> productList;
SparseBooleanArray mSelectedItemsIds;

public ProduitsAdapter(Context ctx, int textViewResourceId, List<StackProduits> sites) {
    super(ctx, textViewResourceId, sites);

    /**Setup the ImageLoader, we'll use this to display our images*/
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(ctx).build();
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(config);

    mSelectedItemsIds = new SparseBooleanArray();

    /**Setup options for ImageLoader so it will handle caching for us.*/
    options = new DisplayImageOptions.Builder()
            .cacheInMemory()
            .cacheOnDisc()
            .build();
}

/**
 * This method is responsible for creating row views out of a StackProduits object that can be put
 * into our ListView.
 * <p/>
 * (non-Javadoc)
 *
 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
 */
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
    RelativeLayout row = (RelativeLayout) convertView;
    //Log.i("StackSites", "getView pos = " + pos);
    if (null == row) {   //No recycled View, we have to inflate one.
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = (RelativeLayout) inflater.inflate(R.layout.item_row, null);
    }

    //Get our View References from item_row.xml
    final ImageView iconImg = (ImageView) row.findViewById(R.id.iconImg);
    TextView txtDesignation = (TextView) row.findViewById(R.id.nameTxt);
    TextView txtAbout = (TextView) row.findViewById(R.id.aboutTxt);
    TextView txtPrice = (TextView) row.findViewById(R.id.priceTxt);
    TextView txtTotalArea = (TextView) row.findViewById(R.id.areaTxt);
    final ProgressBar indicator = (ProgressBar) row.findViewById(R.id.progress);

    //Initially we want the progress indicator visible, and the image invisible
    indicator.setVisibility(View.VISIBLE); //show progress indicator
    iconImg.setVisibility(View.INVISIBLE); //make image invisible

    //Setup a listener we can use to switch from the loading indicator to the Image once it's ready
    //changed ImageLoadingListener with SimpleImageLoadingListener
    SimpleImageLoadingListener listener = new SimpleImageLoadingListener() {
        @Override
        public void onLoadingStarted(String arg0, View arg1) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
            indicator.setVisibility(View.INVISIBLE);
            iconImg.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
            // TODO Auto-generated method stub
        }
    };

    //Load the image and use our options so caching is handled.
    imageLoader.displayImage(getItem(pos).getImgUrl(), iconImg, options, listener);

    //Set the relevant text in our TextViews (ListView)
    txtDesignation.setText(getItem(pos).getDesignation());
    txtAbout.setText(getItem(pos).getAbout());
    txtPrice.setText(getItem(pos).getPrice());
    txtTotalArea.setText(getItem(pos).getArea());

    //return view that represents the full row
    return row;
}
}

onItemClick 内的 Logcat (Favoris.java)

Log.i("test", adapterFavoris.getItem(pos).toString());


07-13 13:27:27.907 1613-1613/com.example.adam_jaamour.cfimmobilier W/System: ClassLoader referenced unknown path: /data/app/com.example.adam_jaamour.cfimmobilier-2/lib/x86
07-13 13:27:30.020 1613-1613/com.example.adam_jaamour.cfimmobilier W/System: ClassLoader referenced unknown path: /data/app/com.example.adam_jaamour.cfimmobilier-2/lib/x86
07-13 13:27:30.169 1613-1613/com.example.adam_jaamour.cfimmobilier W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-13 13:27:30.380 1613-1701/com.example.adam_jaamour.cfimmobilier D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                                 [ 07-13 13:27:30.388  1613: 1613 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0xaa979e80, tid 1613


                                                                                 [ 07-13 13:27:30.414  1613: 1701 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0xaa9796d0, tid 1701
07-13 13:27:30.418 1613-1701/com.example.adam_jaamour.cfimmobilier I/OpenGLRenderer: Initialized EGL, version 1.4
07-13 13:27:31.015 1613-1613/com.example.adam_jaamour.cfimmobilier I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.
07-13 13:27:32.408 1613-1613/com.example.adam_jaamour.cfimmobilier E/libEGL: call to OpenGL ES API with no current context (logged once per thread)
07-13 13:27:32.649 1613-1701/com.example.adam_jaamour.cfimmobilier E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa2329ad0
07-13 13:27:36.071 1613-1613/com.example.adam_jaamour.cfimmobilier W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=161.13647, y[0]=1479.668, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16698889, downTime=16695116, deviceId=0, source=0x1002 }
07-13 13:27:36.071 1613-1613/com.example.adam_jaamour.cfimmobilier W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=161.13647, y[0]=1479.668, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16698889, downTime=16695116, deviceId=0, source=0x1002 }
07-13 13:27:36.072 1613-1613/com.example.adam_jaamour.cfimmobilier W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=161.13647, y[0]=1479.668, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16698889, downTime=16695116, deviceId=0, source=0x1002 }
07-13 13:27:36.072 1613-1613/com.example.adam_jaamour.cfimmobilier W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=161.13647, y[0]=1479.668, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=16698889, downTime=16695116, deviceId=0, source=0x1002 }
                                                                               <p>&nbsp;</p>, reference=FFV0593, type=Apartment, numberOfRooms=Studio, livingArea=49sqm, terraceArea=-, building=Le Montaigne, district=Carré d'Or, parking=null, cellar=null, typeTransaction=Ԁt、��Fo, city=null, country=null, date created=2014-03-12 17:48:49, date last modified=2016-07-12 10:09:30]
07-13 13:27:36.366 1613-1613/com.example.adam_jaamour.cfimmobilier W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
07-13 13:27:36.836 1613-1623/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 12.863ms
07-13 13:27:36.848 1613-1623/com.example.adam_jaamour.cfimmobilier I/art: Background sticky concurrent mark sweep GC freed 12227(1070KB) AllocSpace objects, 3(52KB) LOS objects, 2% free, 41MB/42MB, paused 13.974ms total 66.456ms
07-13 13:27:36.952 1613-1619/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 19.437ms
07-13 13:27:37.062 1613-1623/com.example.adam_jaamour.cfimmobilier W/art: Suspending all threads took: 9.523ms
07-13 13:27:37.071 1613-1623/com.example.adam_jaamour.cfimmobilier I/art: Background sticky concurrent mark sweep GC freed 2527(286KB) AllocSpace objects, 17(812KB) LOS objects, 0% free, 43MB/43MB, paused 9.952ms total 80.879ms
07-13 13:27:37.125 1613-1701/com.example.adam_jaamour.cfimmobilier E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa2329c20
07-13 13:27:37.132 1613-1701/com.example.adam_jaamour.cfimmobilier D/OpenGLRenderer: endAllStagingAnimators on 0xa20b6880 (ListView) with handle 0xb3fec540

【问题讨论】:

  • 我认为您应该使用适配器类的父视图的 onClick 而不是使用 onItemClick,它确实为您的列表提供了 Holder,这不会创建位置问题。如果您发布适配器类,我可以尝试帮你解决这个问题。
  • @Drv 好的,我会看看,我将在我的问题结束时发布我的适配器。
  • @Drv 我添加了我的适配器类,所以你可以看看它:)

标签: java android listview gson sharedpreferences


【解决方案1】:

我认为您获取错误数据的问题在于,您获取的数据是基于从不同数据集中单击的项目,然后是适配器基于其视图的内容(这两个数据集可能包含相同的数据,但顺序不同)。

您正在从 json 构建数据集:

 //for each where we get back values from sting set, then convert to product 
    for (String id : myJson) {
        StackProduits savedProduct = gson.fromJson(id, StackProduits.class);
        listProduits.add(savedProduct);
    } 
    adapterFavoris = new ProduitsAdapter(getApplicationContext(), -1, listProduits);
    produitFavorisListView.setAdapter(adapterFavoris);

在您的 OnItemClickListener 内部,根据您的方法签名,您正在从文件中检索数据:

 //Set the click listener to launch the browser when a row is clicked. 
produitFavorisListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        Intent intentProduitFavorisDetail = new Intent(Favoris.this, ProduitDetail.class);
        StackProduits ProduitFavoris = ProduitsXmlPullParser.getStackProduitFromFile(Favoris.this).get(pos);
        intentProduitFavorisDetail.putExtra("produit", ProduitFavoris);
        startActivity(intentProduitFavorisDetail);
    } 
});

数据可以以不同的顺序存储,这可以解释为什么当您选择某些项目时会显示另一个项目信息。一个简单的测试方法是在 onItemClick 中放置一条日志语句以打印出列表项的字符串表示形式。

如果这确实是您的问题,一个可能的解决方案是在 onItemClick 内部对适配器的引用上调用 ProduitsAdapter.getItem() 以获取对应于“pos”的实际项目。

【讨论】:

  • 是的,我认为这可能是问题所在,因为通过单击收藏夹中的一个项目,我会从另一个列表中获取该项目(收藏夹的第一个项目对应于另一个列表的第一个项目) .我将尝试日志并将其发布在我的问题上
  • 好吧,我添加了 logcat,我相信这是我的问题,既然你指出了它,但我不明白你的解决方案,我应该从适配器类中获取 pos 并使用那个 int 得到正确的项目?
  • 在 onItemClick() 内部使用这行 'adapterFavoris.getItem(pos)' 来获取与单击的列表项对应的 StackProduits 对象。
  • @Adamouization 你的结果是什么?你得到正确的回应了吗?
  • 那么你需要做的就是替换提取点击数据的行,你应该准备好了。如果有帮助,请不要忘记投票并选择此已接受的答案。顺便说一句,您的 getView() 方法可以使用一些重构来提高性能;查看 ViewHolder 模式,第一个链接应该对您有所帮助。
【解决方案2】:

发生这种情况通常是因为您的 ListView 中的项目处于焦点位置。尝试添加

android:descendantFocusability="blocksDescendants"

在您的自定义 ListView 行中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="13dp"
android:layout_marginTop="55dp"
android:descendantFocusability="blocksDescendants">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-24
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多