我在一个单独的文件中创建了一个类适配器,但在我的主文件中,我调用remove方法来删除项目或我选择的项目,但是当我删除它时,它不会在我的应用程序上更新。
CODE:
public static void populateChatListView(final Context context){
adapter = new myAdapter(context, R.layout.source_contact);
listv.setAdapter(adapter);
for (Contacts friend: friends){
adapter.add(friend);
}
listv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
//supposed to remove it
adapter.remove(adapter.getItem(0));
//supposed to update it(notify)
adapter.notifyDataSetChanged();
return true;
}
});
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listv.setSelection(adapter.getCount() - 1);
}
});
}
它没有给我任何错误,也没有删除它。我将不胜感激任何帮助。
CLASS ADAPTER:
public class myAdapter extends ArrayAdapter<Contacts>{
private List<Contacts> Listc = new ArrayList<>();
TextView NameWindow, LastN;
ImageView contactProfile;
Context context;
public myAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
}
@Override
public void add(Contacts object) {
Listc.add(object);
super.add(object);
}
@Override
public Contacts getItem(int position) {
return Listc.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.source_contact, parent, false);
}
NameWindow = (TextView) convertView.findViewById(R.id.contactN);
LastN = (TextView) convertView.findViewById(R.id.contactL);
contactProfile = (ImageView) convertView.findViewById(R.id.contactProfile);
String name, lastn;
Contacts provider = getItem(position);
name = provider.getName(); // name goes down
lastn = provider.getLastn(); // goes upper
NameWindow.setText(name);
LastN.setText(lastMessage);
return convertView;
}
...
编辑。
答案 0 :(得分:1)
将此添加到您的适配器类
public void myRemove(int position){
Listc.remove(position);
myAdapter.super.notifyDataSetChanged();
}
并使用它来查看它是否会产生影响
答案 1 :(得分:0)
我认为在你的适配器中你应该有一个像这样的数组变量。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
enum States
{
FIND_OUTPUT,
GET_SEPERATOR,
GET_TABLE_HEADER,
GET_DATA_TABLE,
END
}
static void Main(string[] args)
{
States state = States.FIND_OUTPUT;
StreamReader reader = new StreamReader(FILENAME);
string inputLine = "";
string connStr = "Enter your connection string here";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
string[] headers = null;
while ((inputLine = reader.ReadLine()) != null)
{
inputLine = inputLine.Trim();
if (inputLine.Length > 0)
{
switch (state)
{
case States.FIND_OUTPUT:
if (inputLine.StartsWith("OUTPUT (SQL Server Table)"))
state = States.GET_SEPERATOR;
break;
case States.GET_SEPERATOR:
state = States.GET_TABLE_HEADER;
break;
case States.GET_TABLE_HEADER:
headers = inputLine.Split(new char[] { '|'}, StringSplitOptions.RemoveEmptyEntries);
headers = headers.Select(x => x.Trim()).ToArray();
state = States.GET_DATA_TABLE;
break;
case States.GET_DATA_TABLE:
string[] dataArray = inputLine.Split(new char[] { '|'}, StringSplitOptions.RemoveEmptyEntries);
dataArray = dataArray.Select(x => x.Trim()).ToArray();
string commandText = string.Format("Insert into table1 ({0}) values ({1})", string.Join(",", headers), "'" + string.Join("','", dataArray) + "'");
cmd.CommandText = commandText;
cmd.ExecuteNonQuery();
break;
}
}
else
{
if (state == States.GET_DATA_TABLE)
break; //exit while loop if blank row at end of data table
}
}
reader.Close();
conn.Close();
}
}
}
您需要做的就是更新此列表,然后调用
class yourAdapter extends BaseAdapter {
private ArrayList<Object> your_list_variable;
....
}
试试吧。
//在这里更新一个例子,你可以查看我的代码。
adapter.notifyDataSetChanged();
希望这对你有所帮助。