我在这里提交了三个我的项目文件,点击删除按钮我要删除该特定行。
list_mobile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/logo"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:src="@drawable/windowsmobile_logo" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30px" >
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete" />
</LinearLayout>
listmobileactivity.java
package com.mkyong.android;
import com.mkyong.android.adaptor.MobileArrayAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
public class ListMobileActivity extends ListActivity {
static final String[] MOBILE_OS = new String[] { "Android", "iOS",
"WindowsMobile", "Blackberry"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setListAdapter(new ArrayAdapter<String>(this, R.layout.list_mobile,
// R.id.label, MOBILE_OS));
setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}
mobilearrayadapter.java
package com.mkyong.android.adaptor;
import com.mkyong.android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("WindowsMobile")) {
imageView.setImageResource(R.drawable.windowsmobile_logo);
} else if (s.equals("iOS")) {
imageView.setImageResource(R.drawable.ios_logo);
} else if (s.equals("Blackberry")) {
imageView.setImageResource(R.drawable.blackberry_logo);
} else {
imageView.setImageResource(R.drawable.android_logo);
}
return rowView;
}
}
答案 0 :(得分:0)
您需要从适配器中删除该项并刷新列表。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
MobileArrayAdapter la = (MobileArrayAdapter) getListAdapter();
la.remove(la.getItem(position));
la.notifyDataSetChanged();
}
答案 1 :(得分:0)
做这样的事情
public class ListMobileActivity extends ListActivity {
String[] MOBILE_OS = new String[] { "Android", "iOS",
"WindowsMobile", "Blackberry"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setListAdapter(new ArrayAdapter<String>(this, R.layout.list_mobile,
// R.id.label, MOBILE_OS));
}
@Override
protected void onResume() {
super.onResume();
setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String[] MOBILE_OS = new String[] { "Android", "iOS",
"WindowsMobile", "Blackberry"};
String[] newArray = new String[MOBILE_OS.length()-1];
[tmp] =0;
for(int i=0;i<MOBILE_OS.length();i++){
if(i!=position){
newArray[tmp] = MOBILE_OS[i];
tmp++;
}
}
MOBILE_OS = newArray;
onResume();
}
答案 2 :(得分:0)
我重写您的mobilearrayadapter.java
以包含点击监听器按钮。我建议使用ArrayList<String>
代替String[]
来轻松添加和删除行
import ...
.
.
.
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private LayoutInflater inflater = null;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(convertView == null)
rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
Button delBtn = (Button) rowView.findViewById(R.id.button1);
delBtn..setOnClickListener(mOnClickListener);
delBtn.setTag(position);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("WindowsMobile")) {
imageView.setImageResource(R.drawable.windowsmobile_logo);
} else if (s.equals("iOS")) {
imageView.setImageResource(R.drawable.ios_logo);
} else if (s.equals("Blackberry")) {
imageView.setImageResource(R.drawable.blackberry_logo);
} else {
imageView.setImageResource(R.drawable.android_logo);
}
return rowView;
}
private OnClickListener mOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
Toast.makeText(MainLocalListAdapter.this.context, ""+position, Toast.LENGTH_SHORT).show();
// After remove row from list call this
notifyDataSetChanged();
}
};
}