你能帮我吗?
这是我的代码:
holder.relativeLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
String click = finalHolder.txtTitle.getText().toString();
SharedPreferences s = context.getSharedPreferences("Favs", 0);
String last = s.getString("fav", "");
if (!last.contains(";")) {
SharedPreferences so = context.getSharedPreferences("Favs", 0);
String lastpl = so.getString("fav", "");
lastpl = lastpl.replace(lastpl, "");
SharedPreferences sp8 = context.getSharedPreferences("Favs", 0);
SharedPreferences.Editor ed = sp8.edit();
ed.putString("fav", lastpl);
ed.apply();
lv = lastpl;
sprüche = new String[]{"Keine Favoriten vorhanden"};
finalHolder.btn.setBackground(context.getResources().getDrawable(transparent));
}
else {
String[] hui;
hui = last.split(";");
if (!hui[0].equals(click)) {
SharedPreferences so = context.getSharedPreferences("Favs", 0);
String lastpl = so.getString("fav", "");
lastpl = lastpl.replace(";" + click, "");
SharedPreferences sp8 = context.getSharedPreferences("Favs", 0);
SharedPreferences.Editor ed = sp8.edit();
ed.putString("fav", lastpl);
ed.apply();
lv = lastpl;
sprüche = lv.split(";");
} else {
SharedPreferences so = context.getSharedPreferences("Favs", 0);
String lastpl = so.getString("fav", "");
lastpl = lastpl.replace(click+";", "");
SharedPreferences sp8 = context.getSharedPreferences("Favs", 0);
SharedPreferences.Editor ed = sp8.edit();
ed.putString("fav", lastpl);
ed.apply();
lv = lastpl;
sprüche = lv.split(";");
}
}
rowItems = new ArrayList<Liste3>();
for (int ii = 0; ii < sprüche.length; ii++) {
Liste3 item = new Liste3(sprüche[ii]);
rowItems.add(item);
}
adapter.notifyDataSetChanged();
if (click.equals(lastplay)) {
ui();
favorite = false;
}
return false;
}
});
“last”是listview中保存在一个字符串中的所有项目。 “rowitems”是我自己的名单。
Liste3:
public class Liste3 {
private String title;
public Liste3( String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title;
}
}
logcat的:
致命的例外:主要 流程:de.kruemelopment.org.lustigewitze,PID:9288 java.lang.IndexOutOfBoundsException:索引:1,大小:1 at java.util.ArrayList.get(ArrayList.java:411) at de.kruemelopment.org.lustigewitze.k.getItem(Unknown Source) at de.kruemelopment.org.lustigewitze.k.getItemId(Unknown Source) 在android.widget.AbsListView.showContextMenuForChildInternal(AbsListView.java:4303) 在android.widget.AbsListView.showContextMenuForChild(AbsListView.java:4293) 在android.view.View.showContextMenu(View.java:6423) 在android.view.View.performLongClickInternal(View.java:6342) 在android.view.View.performLongClick(View.java:6296) 在android.view.View.performLongClick(View.java:6314) 在android.view.View $ CheckForLongPress.run(View.java:23721) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1518) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
这是完整的适配器:
public class startadapter extends BaseAdapter {
Context context;
List<Liste3> rowItems;
public startadapter(Context context, List<Liste3> items) {
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
Button btn;
RelativeLayout relativeLayout;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.liste, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.textView9);
holder.btn=(Button) convertView.findViewById(R.id.button3);
holder.relativeLayout=(RelativeLayout) convertView.findViewById(R.id.relativ);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Liste3 rowItem = (Liste3) getItem(position);
holder.txtTitle.setText(rowItem.getTitle());
if (!rowItem.getTitle().equals("Keine Favoriten vorhanden")) {
holder.btn.setBackground(context.getResources().getDrawable(ic_menu_share));
}
final ViewHolder finalHolder1 = holder;
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!rowItem.getTitle().equals("Keine Favoriten vorhanden")) {
int position = 0;
String he = finalHolder1.txtTitle.getText().toString();
for (int in = 0; in < spr.length; in++) {
if (spr[in].contains(he)) {
position = in;
}
}
File k = new File(context.getExternalFilesDir(null), he+".mp3");
if (k.exists()) {
k.delete();
try {
k.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream in = context.getResources().openRawResource(sp[position]);
FileOutputStream out = null;
try {
out = new FileOutputStream(k);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri uri=Uri.fromFile(k);
Intent share=new Intent(Intent.ACTION_SEND);
share.setType("audio/mp3");
share.putExtra(Intent.EXTRA_STREAM,uri);
context.startActivity(Intent.createChooser(share,"Witz teilen über..."));
}
}
});
final ViewHolder finalHolder = holder;
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String he = finalHolder.txtTitle.getText().toString();
lastplay=he;
favorite=true;
u();
SharedPreferences s = context.getSharedPreferences("Favs", 0);
String load = s.getString("fav", "");
if (!load.isEmpty()) {
int position = 0;
for (int in = 0; in < spr.length; in++) {
if (spr[in].contains(he)) {
position = in;
}
}
if (i==1){
witz.stop();
}
witz = MediaPlayer.create(context, sp[position]);
witz.start();
i=1;
witz.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
i=0;
}
});
}
}
});
holder.relativeLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
String click = finalHolder.txtTitle.getText().toString();
SharedPreferences s = context.getSharedPreferences("Favs", 0);
String last = s.getString("fav", "");
if (!last.contains(";")) {
SharedPreferences so = context.getSharedPreferences("Favs", 0);
String lastpl = so.getString("fav", "");
lastpl = lastpl.replace(lastpl, "");
SharedPreferences sp8 = context.getSharedPreferences("Favs", 0);
SharedPreferences.Editor ed = sp8.edit();
ed.putString("fav", lastpl);
ed.apply();
lv = lastpl;
sprüche = new String[]{"Keine Favoriten vorhanden"};
finalHolder.btn.setBackground(context.getResources().getDrawable(transparent));
}
else {
String[] hui;
hui = last.split(";");
if (!hui[0].equals(click)) {
SharedPreferences so = context.getSharedPreferences("Favs", 0);
String lastpl = so.getString("fav", "");
lastpl = lastpl.replace(";" + click, "");
SharedPreferences sp8 = context.getSharedPreferences("Favs", 0);
SharedPreferences.Editor ed = sp8.edit();
ed.putString("fav", lastpl);
ed.apply();
lv = lastpl;
sprüche = lv.split(";");
} else {
SharedPreferences so = context.getSharedPreferences("Favs", 0);
String lastpl = so.getString("fav", "");
lastpl = lastpl.replace(click + ";", "");
SharedPreferences sp8 = context.getSharedPreferences("Favs", 0);
SharedPreferences.Editor ed = sp8.edit();
ed.putString("fav", lastpl);
ed.apply();
lv = lastpl;
sprüche = lv.split(";");
}
rowItems = new ArrayList<Liste3>();
for (int ii = 0; ii < sprüche.length; ii++) {
Liste3 item = new Liste3(sprüche[ii]);
rowItems.add(item);
}
}
adapter.notifyDataSetChanged();
if (click.equals(lastplay)) {
ui();
favorite = false;
}
return false;
}
});
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
答案 0 :(得分:0)
你可以分享logcat吗?我相信它将是索引出界的问题。但我需要看日志cat.if yes然后替换你的这段代码
for (int ii = 0; ii < sprüche.length; ii++) {
Liste3 item = new Liste3(sprüche[ii]);
rowItems.add(item);
}
用
for (int ii = 0; ii <= sprüche.length-1; ii++) {
Liste3 item = new Liste3(sprüche[ii]);
rowItems.add(item);
}