通过 onClickListener 查找选定的解析数据以切换播放和暂停按钮
Finding selected parsed data via onClickListener for switching Play&Pause Button
我正在开发一个存储铃声的应用程序..我从 mysql 数据库更新新铃声的内容..
我正在尝试创建一个播放和暂停切换按钮..
现在我的问题是我找不到确切的所选项目..
this is a PICTURE off what i am doing here
正如我用箭头向您展示的那样,当我点击每个播放按钮时,第一个播放按钮开始..
到目前为止,这是代码:
public class ListActivity extends AppCompatActivity implements AsyncResponse
,AdapterView.OnItemClickListener{
private boolean paused = true;
URL url;
Bitmap bmImg = null;
public static final int NOTIFICATION_ID = 1;
final String LOG = "ListActivity";
private ArrayList<Product> productList;
private ListView lvProduct;
private FunDapter<Product> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.d_activity_list_d);
ImageLoader.getInstance().init(UILConfig.config(ListActivity.this));
PostResponseAsyncTask taskRead = new PostResponseAsyncTask(ListActivity.this, this);
taskRead.execute("http://symphonyrecords.6te.net/product.php");
lvProduct = (ListView)findViewById(R.id.lvProduct);
registerForContextMenu(lvProduct);
}//onCreate
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final Product selectedProduct = adapter.getItem(info.position);
Log.d(LOG, selectedProduct.name);
if(item.getItemId() == R.id.menuUpdate){
Intent in = new Intent(ListActivity.this, DetailActivity.class);
in.putExtra("product", selectedProduct);
startActivity(in);
}
return super.onContextItemSelected(item);
}
@Override
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
BindDictionary<Product> dict = new BindDictionary<Product>();
dict.addStringField(R.id.tvName, new StringExtractor<Product>() {
@Override
public String getStringValue(Product product, int position) {
return product.name;
}
});
adapter = new FunDapter<>(
ListActivity.this, productList, R.layout.d_layout_list_d, dict);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Product selectedProduct = productList.get(position);
}
public void playAndPause(View view1){
Toast.makeText(ListActivity.this, "Network Error", Toast.LENGTH_LONG).show();
ImageButton button = (ImageButton ) findViewById(R.id.ivShare);
int icon;
if (paused) {
paused = false;
icon = R.drawable.ic_pause;
}
else {
paused = true;
icon = R.drawable.ic_play;
}
button.setImageDrawable(
ContextCompat.getDrawable(getApplicationContext(), icon));
}
}
(考虑此代码,如果您有任何更好的方法来执行此操作,或者有获得所选项目的想法,我将不胜感激).. 谢谢。
在您的 Adapter 中的 getView 方法中:
Product m =(Product) mDataItems.get(position);
holder.playPause.setImageResource(m.getPlayPauseId());
holder.playPause.setTag(position);
holder.playPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Product m =(Product) mDataItems.get(pos);
if (paused) {
m.setPlayPauseId(R.drawable.ic_pause);
paused=false;
}else {
m.setPlayPauseId(R.drawable.ic_play);
paused = true;
}
FunDapter.this.notifyDataSetChanged();
}
});
我正在开发一个存储铃声的应用程序..我从 mysql 数据库更新新铃声的内容.. 我正在尝试创建一个播放和暂停切换按钮.. 现在我的问题是我找不到确切的所选项目.. this is a PICTURE off what i am doing here
正如我用箭头向您展示的那样,当我点击每个播放按钮时,第一个播放按钮开始..
到目前为止,这是代码:
public class ListActivity extends AppCompatActivity implements AsyncResponse
,AdapterView.OnItemClickListener{
private boolean paused = true;
URL url;
Bitmap bmImg = null;
public static final int NOTIFICATION_ID = 1;
final String LOG = "ListActivity";
private ArrayList<Product> productList;
private ListView lvProduct;
private FunDapter<Product> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.d_activity_list_d);
ImageLoader.getInstance().init(UILConfig.config(ListActivity.this));
PostResponseAsyncTask taskRead = new PostResponseAsyncTask(ListActivity.this, this);
taskRead.execute("http://symphonyrecords.6te.net/product.php");
lvProduct = (ListView)findViewById(R.id.lvProduct);
registerForContextMenu(lvProduct);
}//onCreate
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final Product selectedProduct = adapter.getItem(info.position);
Log.d(LOG, selectedProduct.name);
if(item.getItemId() == R.id.menuUpdate){
Intent in = new Intent(ListActivity.this, DetailActivity.class);
in.putExtra("product", selectedProduct);
startActivity(in);
}
return super.onContextItemSelected(item);
}
@Override
public void processFinish(String s) {
productList = new JsonConverter<Product>().toArrayList(s, Product.class);
BindDictionary<Product> dict = new BindDictionary<Product>();
dict.addStringField(R.id.tvName, new StringExtractor<Product>() {
@Override
public String getStringValue(Product product, int position) {
return product.name;
}
});
adapter = new FunDapter<>(
ListActivity.this, productList, R.layout.d_layout_list_d, dict);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Product selectedProduct = productList.get(position);
}
public void playAndPause(View view1){
Toast.makeText(ListActivity.this, "Network Error", Toast.LENGTH_LONG).show();
ImageButton button = (ImageButton ) findViewById(R.id.ivShare);
int icon;
if (paused) {
paused = false;
icon = R.drawable.ic_pause;
}
else {
paused = true;
icon = R.drawable.ic_play;
}
button.setImageDrawable(
ContextCompat.getDrawable(getApplicationContext(), icon));
}
}
(考虑此代码,如果您有任何更好的方法来执行此操作,或者有获得所选项目的想法,我将不胜感激).. 谢谢。
在您的 Adapter 中的 getView 方法中:
Product m =(Product) mDataItems.get(position);
holder.playPause.setImageResource(m.getPlayPauseId());
holder.playPause.setTag(position);
holder.playPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Product m =(Product) mDataItems.get(pos);
if (paused) {
m.setPlayPauseId(R.drawable.ic_pause);
paused=false;
}else {
m.setPlayPauseId(R.drawable.ic_play);
paused = true;
}
FunDapter.this.notifyDataSetChanged();
}
});