gpt4 book ai didi

java - Android:ListView 项目点击无法正常工作

转载 作者:行者123 更新时间:2023-11-30 01:11:51 24 4
gpt4 key购买 nike

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

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

我确实注意到了一些可能会有所帮助的事情:通过在我的列表中添加一些 Collection 夹,比如 3,我注意到了一种模式:

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

我目前通过使用 Gson 转换它并使用 SharedPrefenreces 传递它,将最喜欢的产品作为类对象传递给 Collection 夹类,然后类 Favoris.java 取回该产品并将其转换回类对象。

这是我所有我最喜欢的产品的 ListView ,您可以看到有 4 个产品添加为 Collection :

favorites' listview

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

wrong product detail

这是产品详情的代码:

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();
}
}
});
}
}

这是我的类处理 Collection 夹的代码:

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

最佳答案

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

在这里,您正在从 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”的实际项目。

关于java - Android:ListView 项目点击无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38349390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com