我希望通过长按列表项将歌曲设置为列表视图中的铃声或闹钟铃声或留言音等。我想显示一个执行这些操作的对话框。如何通过长按列表项并在对话框中设置操作来显示对话框?请帮帮我......
com.example.ring;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Naat extends Activity implements
OnItemClickListener {
private ListView listView;
public static MediaPlayer mp;
Button bt;
PopAdapter adapter;
ArrayList<String> dataItems = new ArrayList<String>();
public static int[] rings = {R.raw.arabicmusic, R.raw.arabicmusic1, R.raw.arabicmusic12,
R.raw.arabicmusic13, R.raw.arabicmusic14, R.raw.arabicmusic15,
R.raw.arabicmusic18, R.raw.arabicmusic16, R.raw.arabicmusic17};
public static int imgadrss = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_naat);
String[] dataArray = getResources().getStringArray(R.array.listdata);
List<String> dataTemp = Arrays.asList(dataArray);
dataItems.addAll(dataTemp);
listView = (ListView) findViewById(R.id.listnaat);
// bt = (Button) findViewById(R.id.button1);
adapter = new PopAdapter(Naat.this, dataItems);
// adapter.setCustomButtonListner(MainActivity.this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
// @Override
// public void onButtonClickListner(int position, String value) {
// mp.create(this, rings[position]);
// mp.start();
//
// Toast.makeText(MainActivity.this, "Button click " + value,
// Toast.LENGTH_SHORT).show();
//
// }
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "" + position, 2000).show();
imgadrss = rings[position];
}
}
适配器类
com.example.ring;
import java.util.ArrayList;
import android.content.Context;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class NaatAdapter extends ArrayAdapter<String> {
customButtonListener customListner;
public interface customButtonListener {
public void onButtonClickListner(int position, String value);
}
public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
}
private Context context;
private ArrayList<String> data = new ArrayList<String>();
public NaatAdapter(Context context, ArrayList<String> dataItem) {
super(context, R.layout.singlerow, dataItem);
this.data = dataItem;
this.context = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.singlerow, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView
.findViewById(R.id.textView1);
viewHolder.button = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
viewHolder.text.setText(temp);
viewHolder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position, temp);
}
// MainActivity.mp.create(context, MainActivity.imgadrss);
// MainActivity.mp.start();
MediaPlayer mp = MediaPlayer.create(context, Naat.rings[position]);
if (mp.isPlaying()) {
if (mp != null) {
mp.pause();
}
} else {
if (mp != null) {
mp.start();
}
}
// mp.stop();
Toast.makeText(context, "" + position, 2000).show();
}
});
return convertView;
}
public class ViewHolder {
TextView text;
Button button;
}
}
答案 0 :(得分:1)
您可以使用此
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
// TODO Auto-generated method stub
Log.d("in onLongClick");
//Open alert dialog here
return true;
}
});