可扩展的列表视图是正确的,但在再次调用时它不会刷新.旧的项目再次出现在可扩展列表视图中.这是我的适配器类.如何在expab = ndable listview中刷新项目?
MyAdapter.class
public class MyAdapter extends BaseExpandableListAdapter {
private Context _context;
private static List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private static HashMap<String, List<String>> _listDataChild;
public MyAdapter(Context context, List<String> listDataHeader,HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int …Run Code Online (Sandbox Code Playgroud) 我有一个自定义baseadapter,它会对一些图像进行一些延迟加载,然后对布局进行充气,这样我最终会得到一个listview,其中我将图像和文本放在一行中.
当用户按下列表视图中的一个项目时,比如说项目0(顶部项目),我想显示一个带有一些特定内容的对话框.此含量取决于项目数 - 因此对于项0示出的内容是不一样的,作为第1项,依此类推.
以下是getView自定义适配器的方法:
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
if(convertView==null)
{
vi = inflater.inflate(R.layout.facebook_item, null);
vi.setClickable(true);
vi.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String s = "test";
Toast.makeText(myContext, s, Toast.LENGTH_SHORT).show();
}
});
}
TextView text=(TextView)vi.findViewById(R.id.text);
ImageView image=(ImageView)vi.findViewById(R.id.image);
text.setText(msg[position]);
text.getLineCount();
imageLoader.DisplayImage(data[position], image);
return vi;
}
Run Code Online (Sandbox Code Playgroud)
这里重要的是该onClick方法正在发生什么.我想有一个item参数,但这对于这个OnClickListener是不可能的.我知道,正常的列表视图是可能的.
那么 - 如何确定点击了哪个项目?
PS:我曾尝试过使用某种方式vi.setTag(<<number>>);,但是我没有看到如果没有为listview的所有项目设置相同的标签,这怎么办.
我想将无限的适配器功能添加到我的自定义适配器.我该如何使用这种组合?谢谢.
我已经制作了一个自定义ListView但它没有调用我已注册的任何onclick监听器或上下文菜单.
这是我的自定义适配器:
class AdapterContactsActivity extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> rowdata;
private static LayoutInflater inflater = null;
public ArrayList<String> checkedContacts = new ArrayList<String>();
public AdapterContactsActivity(Activity a,
ArrayList<HashMap<String, String>> d) {
activity = a;
rowdata = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return rowdata.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) { …Run Code Online (Sandbox Code Playgroud) 我正在做一个项目,我想在ListView中显示联系人名称列表.从本地sqli db中检索名称.到目前为止,我已设法检索名称并使用标准ArrayAdapter类显示它们.
但是为了更多控制,我正在尝试创建自己的适配器,以允许我在每一行上显示控制按钮.我对这段代码感到困惑:
private void fillData() {
Cursor mContactsCursor = mDbAdapter.getAllContacts();
startManagingCursor(mContactsCursor);
String [] from = new String[] {ContactsDbAdapter.COLUMN_FNAME, ContactsDbAdapter.COLUMN_LNAME};
int [] to = new int [] { R.id.fname, R.id.lname};
//What variables should constructor take?
adapter = new ContactsListAdapter(this, from, to);
data.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
基本上我不知道如何将这些值传递给构造函数或者我是否应该这样做?
String [] from = new String[] {ContactsDbAdapter.COLUMN_FNAME, ContactsDbAdapter.COLUMN_LNAME};
int [] to = new int [] { R.id.fname, R.id.lname};
Run Code Online (Sandbox Code Playgroud)
这是我的ContactsListAdapter类:
public class ContactsListAdapter extends ArrayAdapter<Contact> {
private List<Contact> contacts;
private Button deleteBtn;
private Button editBtn;
private TextView …Run Code Online (Sandbox Code Playgroud) 以下是我目前用于listView的代码:
private class Asyncprojectlist extends AsyncTask<Void, Void, Void>{
JSONArray JSar;
JSONObject JSob;
String project_title, project_sector;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(Allprojects.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
UserFunctions user = new UserFunctions();
JSob = user.allprojects();
try {
JSar = JSob.getJSONArray("data");
} catch (JSONException e1) {
e1.printStackTrace();
}
for(int i=0; i<JSar.length(); i++){
try {
JSONObject newobj = JSar.getJSONObject(i);
project_title = newobj.getString("title");
project_sector = newobj.getString("sector");
HashMap<String, String> single = new HashMap<String, String>();
single.put("title", …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的活动实现自定义适配器,该适配器将从两个ArrayList填充listview。但是,我对下一步该怎么办感到困惑?这是我的代码:
public class Offers extends Activity
{
ListView latesttrans;
Boolean flagValue = false;
ArrayList<String> listItems = new ArrayList<String>();
ArrayList<String> listAddress = new ArrayList<String>();
String[] addr;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offers);
latesttrans = (ListView) findViewById(R.id.listView1);
Uri latesttransuri = Uri.parse("content://sms/inbox");
Cursor latesttranscursor = getContentResolver().query(latesttransuri, new String[]{"_id","address","date","body"}, null, null, null);
if(flagValue == false)
{
latesttranscursor.moveToFirst();
flagValue = true;
}
String address = null,body = null;
int i=0;
while(latesttranscursor.moveToNext())
{
address=latesttranscursor.getString(1);
long date = latesttranscursor.getLong(2);
String sdate = millisToDate(date);
body = …Run Code Online (Sandbox Code Playgroud) 我正在尝试列出具有从Shop类型填充的ArrayList中的列的行,这些列是从SQLite数据库中提取的.因此,我有一个自定义适配器.
但是,不会显示任何数据.没有错误消息或异常.
我使用过调试器(我使用的是Android Studio).我知道没有调用重写的getView方法(此方法中的断点未被触发).触发了构造函数中的断点,并且预期的数据位于ArrayList中.
ListView,已经用于其他方法(我已经从一个简单的列表中获得了一个字符串数组中的数据,它确认了现有数据的其他测试,例如对话框显示数据,使用ShopList的数据列表到一个简单的列表type,列出元素而不是数据).
rowview XML可能存在问题.
我非常感谢有人让我知道上述问题的代码(如下所示)有什么问题.即如果时间允许,为什么没有显示任何内容以及如何更正代码.
我想确认一下我看了很多关于这个的帖子并尝试了各种选项,你可以在代码中看到一些.
这是我认为相关的MainShopActivity的摘录(完整的代码是最后的,因为它很难看并且有很多代码被注释掉了).
ShopListArrayAdapter adapter = new ShopListArrayAdapter(this,shoplist);
//listview.setAdapter(adapter);
setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
这是ShopListArrayAdapter.
package mjt.shopper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Mike092015 on 1/02/2016.
*/
public class ShopListArrayAdapter extends ArrayAdapter<ArrayList<Shop>> {
private final Context context;
private final ArrayList<Shop> shoplist;
public ShopListArrayAdapter(Context context, ArrayList<Shop> shoplist) {
super(context, R.layout.activity_shop_list_entry);
this.context = context;
this.shoplist = shoplist;
}
@Override
public View getView(int position, View convertview, …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Android应用程序,并且正在尝试使该项目在具有自定义适配器的listView上单击以正常工作,但无法运行。我在客户适配器内部实现了OnItemClickListener。
你知道我会做错什么吗?ListView正确加载了内容,唯一的一点是它没有单击。
这是我的listView定义和适配器设置:
public void updateUserAssetBookingsListView(final ArrayList<AssetBooking> userAssetBookings) {
System.out.println("Bookings updated, new total is: " + userAssetBookings.size());
BookingAdapter bookingAdapter = new BookingAdapter(getContext(), 0, userAssetBookings);
userAssetBookingsListView.setAdapter(bookingAdapter);
userAssetBookingsListView.setOnItemClickListener(bookingAdapter);
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义适配器:
package it.bitrack.adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.Toast;
import org.w3c.dom.Text;
import it.bitrack.fabio.bitrack.R;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import it.bitrack.support.Epoch;
import it.bitrack.support.AssetBooking;
/**
* Created by fabio on 25/04/2017.
*/
public class …Run Code Online (Sandbox Code Playgroud) 我有listview的自定义适配器.程序无需错误,但列表视图为空.
我的适配器看起来像:
public class CustomAdapter extends BaseAdapter implements Filterable {
private ArrayList<OItem> _data;
Context _c;
public CustomAdapter(ArrayList<OItem> data, Context c) {
_data = data;
_c = c;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.item, null);
}
OItem oItem = _data.get(position);
TextView tvId = (TextView)v.findViewById(R.id.id);
TextView tvName = (TextView)v.findViewById(R.id.name);
TextView tvBatch = (TextView)v.findViewById(R.id.batch);
tvId.setText(oItem.getId());
tvName.setText(oItem.getName());
tvBatch.setText(oItem.getBatch());
return v;
}
}
Run Code Online (Sandbox Code Playgroud)
在活动中:
ArrayList<OItem> …Run Code Online (Sandbox Code Playgroud) android ×10
custom-adapter ×10
listview ×5
arraylist ×2
baseadapter ×1
cwac-endless ×1
hashmap ×1
sqlite ×1