gpt4 book ai didi

android - 保存 TouchListView 并在下次应用打开时打开它

转载 作者:行者123 更新时间:2023-11-30 04:20:33 25 4
gpt4 key购买 nike

我需要帮助如何在拖放后保存 ListView 的订单。我的列表有项目(谷歌和雅虎),当用户按下它们时,它会打开 www.google.com 或 www.yahoo.com。

所以现在每次我重新排序我的 ListView 并关闭应用程序时,它都会忘记我之前制作的订单并打开默认订单。

我的代码:

    private static String[] items={
"Google",
"Yahoo"};

private static String[] links={
"http://google.com",
"http://yahoo.com};

private IconicAdapter adapter=null;

private IconicAdapter1 adapter2=null;

ArrayList<String> array= new ArrayList<String>(Arrays.asList(items));

private ArrayList<String> array2=new ArrayList<String>(Arrays.asList(links));


/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);



TouchListView tlv=(TouchListView)getListView();
adapter=new IconicAdapter();
adapter2=new IconicAdapter1();
setListAdapter(adapter);
setListAdapter(adapter2);

tlv.setOnItemClickListener(itemclick);
tlv.setDropListener(onDrop);

}



private TouchListView.DropListener onDrop=new TouchListView.DropListener() {
@Override
public void drop(int from, int to) {
String item=(String) adapter.getItem(from);

adapter.remove(item);
adapter.insert(item, to);

String link=(String) adapter2.getItem(from);
adapter2.remove(link);
adapter2.insert(link, to);


}
};

private TouchListView.OnItemClickListener itemclick= new TouchListView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int from, long id) {

String item = adapter.getItem(from);

Toast.makeText(getBaseContext(), "you pick: "+item, Toast.LENGTH_SHORT).show();

String link = adapter2.getItem(from);
Intent showContent = new Intent(getApplicationContext(),
Web.class);
showContent.setData(Uri.parse(link));
startActivity(showContent);
}
};

class IconicAdapter extends ArrayAdapter<String> {
IconicAdapter() {
super(MainScreen.this, R.layout.row, array);
}

public View getView(int position, View convertView,
ViewGroup parent) {
View row=convertView;

if (row==null) {
LayoutInflater inflater=getLayoutInflater();

row=inflater.inflate(R.layout.row, parent, false);
}

TextView label=(TextView)row.findViewById(R.id.label);

label.setText(array.get(position));

return(row);

}
};

class IconicAdapter1 extends ArrayAdapter<String> {
IconicAdapter1() {
super(MainScreen.this, R.layout.row, array2);
}

public View getView(int position, View convertView,
ViewGroup parent) {
View row=convertView;

if (row==null) {
LayoutInflater inflater=getLayoutInflater();

row=inflater.inflate(R.layout.row, parent, false);
}

TextView label=(TextView)row.findViewById(R.id.label);

label.setText(array.get(position));

return(row);

}
}

最佳答案

既然您关心的是将项目的状态存储在列表中,那么如何将您的数据存储在 SQLite database 中呢?并使用 CursorAdapter ?您可以将订单存储为数据库中的一列。

不过一般来说,您需要注意明确保存您的应用程序状态。在这里查看更多详细信息:http://developer.android.com/guide/topics/data/data-storage.html

关于android - 保存 TouchListView 并在下次应用打开时打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9316454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com