gpt4 book ai didi

android - 在结束 ListView android中添加项目

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:04 24 4
gpt4 key购买 nike

这是一个展示动态 ListView 从列表 1 中获取值的示例。现在我想在结束位置(最后)添加项目。意味着在我们插入时不覆盖项目。这将是项目的分页。请有人帮助我吗?

package com.list.viewer;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/** http://www.android-codes-examples.blogspot.com/ */
public class ListViewExample extends Activity {

private String[] list1 = { "Icon", "Icon Creator", "Image", "Image Creator" };

private ListView lister1;

Button btn_add;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lister1 = (ListView) findViewById(R.id.ListView1);
btn_add =(Button)findViewById(R.id.btn_add);
String[] list2 = { "nikunj", "patel", "hi", "this" };
ItemsAdapter itemsAdapter = new ItemsAdapter(ListViewExample.this,
R.layout.list, list2);
lister1.setAdapter(itemsAdapter);
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String[] list2 = { "nikunj", "patel", "hi", "this" };
ItemsAdapter itemsAdapter = new ItemsAdapter(ListViewExample.this,
R.layout.list, list2);
lister1.setAdapter(itemsAdapter);

}
});

/*lister1.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int positon, long arg3) {
String[] lister = null;
switch (positon) {
case 0:
lister = list21;
break;
case 1:
lister = list22;
break;
case 2:
lister = list23;
break;
case 3:
lister = list24;
break;
default:
lister = list21;
}
lister2.setAdapter(new ArrayAdapter<String>(
ListViewExample.this,
android.R.layout.simple_list_item_1, lister));
}
});*/
}

private class ItemsAdapter extends BaseAdapter {
String[] items;

public ItemsAdapter(Context context, int textViewResourceId,
String[] items) {
this.items = items;
}

@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
System.out.println("position is "+position);
TextView mDescription;
View view = convertView;
//ImageView image;
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list, null);
}
if(position == 3)
{
mDescription = (TextView) view.findViewById(R.id.desc);
mDescription.setText(items[position]);
}else
{
mDescription = (TextView) view.findViewById(R.id.desc);
mDescription.setText(items[position]);

}
//image = (ImageView) view.findViewById(R.id.image);

//image.setBackgroundResource(R.id.image);
return view;
}

public int getCount() {
return items.length;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}
}
}

最佳答案

您可以添加一个始终显示在列表底部的“页脚 View ”:

View footerView = getLayoutInflater().inflate(R.layout.footer_layout, lister1, false);
lister1.addFooterView(footerView);

关于android - 在结束 ListView android中添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5785539/

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