我无法使用ListView的每一行中的删除按钮删除项目。我不知道如何获取Listview项目按钮单击的当前位置。你能帮我解决一下这个问题。 我想要做的就是当我点击删除按钮时,特定行被删除。
以下是主要活动代码和xml代码
我的可展开列表视图的Xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:slide="http://schemas.android.com/apk/res/com.example.test_expendable"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".Create_Team_Form" >
<RelativeLayout
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello World"
android:textSize="20dip" />
<Button
android:id="@+id/expandable_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/Team_More" />
</RelativeLayout>
<LinearLayout
android:id="@+id/expandable"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_Del"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:onClick="Team_Delete"
android:text="@string/Team_Del"
android:textSize="12dip" />
<Button
android:id="@+id/btn_Details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:onClick="Team_Details"
android:text="@string/Team_Detail"
android:textSize="12dip" />
</LinearLayout>
</LinearLayout>
我的主要活动Xml代码,我有ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="@+id/Team_Create_MainTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_Create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Create_Team"
android:text="@string/Team_Create_New" />
<Button
android:id="@+id/btn_ADD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Team_Create_Add" />
<Button
android:id="@+id/btn_Edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Team_Create_Edit" />
<Button
android:id="@+id/btn_Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Team_Create_Back" />
</TableRow>
<com.example.fyp_api_8_team.SlideExpandableListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="465dp" >
</com.example.fyp_api_8_team.SlideExpandableListView>
</TableLayout>
</LinearLayout>
我的主要活动代码
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Create_Team_Form extends Activity {
TextView txt;
private String[] lv_arr = {};
ArrayList<String> teamNames = new ArrayList<String>();
ListView list;
ListAdapter adapter;
static String[] COUNTRIES;
//ListView lv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create__team__form);
list = (ListView) this.findViewById(R.id.list);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_create__team__form, menu);
return true;
}
public void Create_Team(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("CreateTeam");
alert.setMessage("Enter Team Name");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// OK
String ans = input.getText().toString();
if (ans == null || ans.length() == 0 || ans.equals("")) {
} else {
String value = ans;
Add_TeamName_List(value);
Set_List();
}
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
public void Add_TeamName_List(String Name) {
teamNames.add(Name);
Put_data_Array();
}
public void Set_List() {
adapter = new ArrayAdapter<String>(this,
R.layout.expandable_list_item, R.id.text, lv_arr);
list.setAdapter(new SlideExpandableListAdapter(adapter,
R.id.expandable_toggle_button, R.id.expandable));
/*
lv1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, lv_arr));
*/
}
public void Put_data_Array() {
lv_arr = new String[teamNames.size()];
for (int i = 0; i < teamNames.size(); i++) {
lv_arr[i] = teamNames.get(i);
}
Arrays.sort(lv_arr);
put_Data_again_Arraylist();
}
public void put_Data_again_Arraylist()
{
teamNames.clear();
for (int i = 0; i < lv_arr.length; i++) {
teamNames.add(lv_arr[i]);
}
}
public void Team_Delete(View v)
{
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
teamNames.remove(position);
((BaseAdapter) adapter).notifyDataSetChanged();
}
});
}
public void Team_Details(View v)
{
}
下面我附上了我的UI的截图
当我点击“更多”按钮时,我会向用户显示“删除”和“详细信息”按钮。如果用户按“删除”按钮,则应删除该行项。
等待回复我正试图解决这个问题很长一段时间
答案 0 :(得分:0)
- 始终使用ArrayList
而不是String[]
来保存ArrayAdapter
将使用的值Collection
更加灵活比Arrays
- 点击删除按钮后,您只需删除 ArrayList
条目,然后执行notifyDataSetChanged()
方法。
<强>例如强>
ArrayList<String> s = new ArrayList<String>();
s.setList(); // A custom method to fill the list
// assign your ArrayList to the ArrayAdpater here
// set ArrayAdpater to ListView
arrayadapter.notifyDataSetChanged(); // arrayadapter is of the ArrayAdapter which is
// set with your ArrayList