我使用以下方法在片段类中填充listview。
private void showEmployee(){
JSONObject jsonObject = null;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String cia = jo.getString(Config.TAG_CIA);
String fn = jo.getString(Config.TAG_FN);
String ln = jo.getString(Config.TAG_LN);
String ad1 = jo.getString(Config.TAG_AD1);
String ad2 = jo.getString(Config.TAG_AD2);
String type = jo.getString(Config.TAG_TYPE);
String city = jo.getString(Config.TAG_CITY);
String state = jo.getString(Config.TAG_STATE);
String zip = jo.getString(Config.TAG_ZIP);
String phone = jo.getString(Config.TAG_PHONE);
String ext = jo.getString(Config.TAG_EXT);
String fromto = jo.getString(Config.TAG_FROMTO);
Log.d("HOLA ADDRESSES", "FROM O TO: " + fromto);
String user = jo.getString(Config.TAG_USER);
String id_address = jo.getString(Config.TAG_ID_ADDRESS);
HashMap<String,String> employees = new HashMap<>();
employees.put(Config.TAG_CIA,cia);
employees.put(Config.TAG_LN,fn);
employees.put(Config.TAG_FN,ln);
employees.put(Config.TAG_AD1,ad1);
employees.put(Config.TAG_AD2,ad2);
employees.put(Config.TAG_TYPE,type);
employees.put(Config.TAG_CITY,city);
employees.put(Config.TAG_STATE,state);
employees.put(Config.TAG_ZIP,zip);
employees.put(Config.TAG_PHONE,phone);
employees.put(Config.TAG_EXT,ext);
employees.put(Config.TAG_FROMTO,fromto);
employees.put(Config.TAG_USER,user);
employees.put(Config.TAG_ID_ADDRESS,id_address);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
getActivity(), list, R.layout.addresses_list_item,
new String[]{Config.TAG_CIA,
Config.TAG_FN,
Config.TAG_LN,
Config.TAG_AD1,
Config.TAG_AD2,
Config.TAG_TYPE,
Config.TAG_CITY,
Config.TAG_STATE,
Config.TAG_ZIP,
Config.TAG_PHONE,
Config.TAG_EXT,
Config.TAG_FROMTO,
Config.TAG_USER,
Config.TAG_ID_ADDRESS},
new int[]{R.id.cia,
R.id.fn,
R.id.ln,
R.id.ad1,
R.id.ad2,
R.id.type,
R.id.city,
R.id.state,
R.id.zip,
R.id.phone,
R.id.ext,
R.id.fromto,
R.id.user,
R.id.id_address});
listView.setAdapter(adapter);
}
现在我想在列表的每一行都包含一个按钮,并且能够根据其中一个项目的值更改每一行的按钮文本。
我已将该按钮包含在listview项目布局文件中。
我不知道在这个方法上放置按钮引用的位置。
谢谢。
答案 0 :(得分:1)
首先为您的数据制作model
public class DataModel {
String title;
String description;
String addedby;
public DataModel(String title, String description, String addedby) {
this.title=title;
this.description=description;
this.addedby=addedby;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getAddedby() {
return addedby;
}
}
然后为列表定义自定义适配器
public class CustomAdapter extends ArrayAdapter实现了View.OnClickListener {
private ArrayList<DataModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView title;
TextView description;
TextView addedBy;
Button yourButton;
}
public CustomAdapter(ArrayList<DataModel> data, Context context) {
super(context, R.layout.list_view_items, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
DataModel dataModel=(DataModel)object;
}
private int lastPosition = -1;
@Override
public DataModel getItem(int position) {
return super.getItem(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
final DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_view_items, parent, false);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.description = (TextView) convertView.findViewById(R.id.description);
viewHolder.addedBy = (TextView) convertView.findViewById(R.id.addedBy);
viewholder.yourButton = (Button) convertView.findViewById(R.id.btn);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
lastPosition = position;
viewHolder.title.setText(dataModel.getTitle());
viewHolder.description.setText(dataModel.getDescription());
viewHolder.addedBy.setText(dataModel.getAddedby());
viewholder.yourButton.setOnClickListener(this);
// Return the completed view to render on screen
return convertView;
}
}
现在列表项的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#000000"
android:id="@+id/title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#212121"
android:fadingEdge="horizontal"
android:maxLines="3"
android:ellipsize="end"
android:id="@+id/description"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addedBy"
android:textSize="13sp"
android:layout_gravity="end"
android:textColor="#212121"/>
</LinearLayout>
现在只需使用这些类
ArrayList<DataModel> dataModels;
private static CustomAdapter adapter;
JSONObject jo = result.getJSONObject(i);
String cia = jo.getString(Config.TAG_CIA);
String fn = jo.getString(Config.TAG_FN);
String ln = jo.getString(Config.TAG_LN);
dataModels.add(new DataModel(cia , fn , ln ));
adapter= new CustomAdapter(dataModels,getApplicationContext());
list.setAdapter(adapter);
答案 1 :(得分:0)
您应该创建自定义适配器。 请尝试以下链接: -
答案 2 :(得分:0)
唯一的解决方案是必须使用自定义adpter。喜欢这个
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.RecyclerViewViewHolder> {
private ContactAdapter.RecyclerViewViewHolder viewHolder;
private View view;
Context ctx;
List<Contact> usersList;
public ContactAdapter(Context ctx, List<Contact> usersList) {
this.ctx = ctx;
this.usersList = usersList;
}
@Override
public ContactAdapter.RecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_contact_list, parent, false);
viewHolder = new RecyclerViewViewHolder(view);
viewHolder.setIsRecyclable(false);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewViewHolder holder, int position) {
viewHolder.name.setText(usersList.get(position).getName());
viewHolder.avatar.setImageBitmap(AppUtil.getCircleBitmap(usersList.get(position).getAvatar()));
}
@Override
public int getItemCount() {
return usersList.size();
}
public class RecyclerViewViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView name;
Button avatar;
public RecyclerViewViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
avatar = (Button) itemView.findViewById(R.id.avatar);
avatar.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getLayoutPosition();
Intent intent = new Intent(ctx,DetailScreen.class);
AppUtil.hideKeyBoard(ctx);
intent.putExtra("id",usersList.get(position).getId());
ctx.startActivity(intent);
}
}
}
答案 3 :(得分:0)
您应该创建一个自定义适配器类,并且在getView方法中,您应该引用该按钮并在该按钮上设置单击侦听器。
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
}
});
请参阅此链接以获取更多详细信息 ListView with Add and Delete Buttons in each Row in android
答案 4 :(得分:0)
使用Base Adapter和getView代码,如下所示:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView2);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.clickableButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Code goes here
}
});
//set text and url
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView);
return convertView;
}