如何在使用Customized ListView时一次只修改一行

时间:2016-12-17 06:08:03

标签: android listview baseadapter

我使用了自定义ListView而不是ExtendedListView。我已经relative layout将其显示为sub_list。默认情况下,我已将此sub_list相对布局的可见性设置为GONE。点击任意listItemrelative layout(默认设置为已消失)将分别显示在屏幕上。但是,当我单击任何行时,sub_list同时出现在每一行中。我想要的是它应该一次只显示一行。我已经检查了这个类似的[问题]:How to modify only one row at a time in Listview?并修改了我的代码,但仍然无法实现我的目标。

这是我的代码:

public class CustomAdapter extends BaseAdapter {

Activity context;
ArrayList<String> s_date,c_number,d_ration,s_time,download_path,a_number,a_name,dt_number;
int flag=0,temp=1,position;
String mUrl;
int posview;
private int selectedIndex;
CustomAdapter(Activity context, ArrayList<String> start_date, ArrayList<String> caller_number, ArrayList<String> duration,ArrayList<String> start_time, ArrayList<String> download_path, ArrayList<String> agent_number, ArrayList<String> agent_name,ArrayList<String> dt_num)
{

    this.context=context;
    this.s_date=start_date;
    this.c_number=caller_number;
    this.d_ration=duration;
    this.s_time=start_time;
    this.download_path=download_path;
    this.a_number=agent_number;
    this.a_name=agent_name;
    this.dt_number=dt_num;
    selectedIndex = -1;
}
public void setSelectedIndex(int ind)
{
    selectedIndex = ind;
    notifyDataSetChanged();
}

@Override
public int getCount() {

    return s_date.size();
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int position) {

    return position;
}

public View getView(final int pos, final View v, ViewGroup g)
{
    //LayoutInflater l=context.getLayoutInflater();
    //View rowView=l.inflate(R.layout.log_layout,null,true);

    posview=pos;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View row = inflater.inflate(R.layout.list_item, g, false);


    TextView start_date = (TextView) row.findViewById(R.id.start_date);
    start_date.setText(s_date.get(pos));
    TextView caller_number = (TextView) row.findViewById(R.id.caller_number);
    caller_number.setText(c_number.get(pos));
    TextView duration = (TextView) row.findViewById(R.id.duration);
    duration.setText(d_ration.get(pos));
    TextView start_time = (TextView) row.findViewById(R.id.start_time);
    start_time.setText(s_time.get(pos));
    TextView agent_name = (TextView) row.findViewById(R.id.agent_name);
    agent_name.setText(a_name.get(pos));
    TextView agent_number = (TextView) row.findViewById(R.id.agent_number);
    agent_number.setText(a_number.get(pos));
    TextView dt_numb=(TextView)row.findViewById(R.id.dt_number);
    dt_numb.setText("MHL No. -"+dt_number.get(pos));

    RelativeLayout r = (RelativeLayout)row.findViewById(R.id.sub_layout);
    r.setVisibility(position == posview ? View.VISIBLE : View.GONE);

    row.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            posview=pos;
            notifyDataSetChanged();

        }
    });

    return row;

}

}

以前我的代码是:

          row.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {

            //setSelectedIndex(view.getId());
             RelativeLayout sub_list = (RelativeLayout) row.findViewById(R.id.sub_layout);
            sub_list.setVisibility(View.VISIBLE);
            //notifyDataSetChanged();


        }
    });

我动态获取所有数据。这就是我在MainActivity中使用适配器的方式:

    CustomAdapter a=new CustomAdapter(MainScreen.this,start_date,caller_number,duration,start_time,download_path,agent_number,agent_name,dt_number);
    data_list.setAdapter(a);

这个start_date caller_number等是arraylist。这就是我在MainActivity中解析数据的方式。

    JSONObject responseObject = new JSONObject(response);
                        JSONObject callLog = responseObject.getJSONObject("call_log");

                        Iterator<String> phoneNumbers = callLog.keys();


                        while (phoneNumbers.hasNext()) {
                            String number = phoneNumbers.next();
                           // Log.v("string number",number);
                            JSONObject numberLog = callLog.getJSONObject(number);
                            Iterator<String> callEntries = numberLog.keys();

                            while (callEntries.hasNext()) {
                                String entry = callEntries.next();

                                //Log.v("unique keys are",entry);
                                JSONObject entryObject = numberLog.getJSONObject(entry);
                                jsonArray.put(entryObject.getString("start_date"));
                                jsonArray.put(entryObject.getString("start_time"));
                                jsonArray.put(entryObject.getString("end_date"));
                                jsonArray.put(entryObject.getString("end_time"));
                                jsonArray.put(entryObject.getString("recording_path"));



                                String startDate = entryObject.getString("start_date");
                                start_date.add(startDate);
                                String startTime = entryObject.getString("start_time");
                                start_time.add(startTime);
                                String endDate = entryObject.getString("end_date");
                                String endTime = entryObject.getString("end_time");
                                String call_sdate = entryObject.getString("call_sdate");
                                String call_edate = entryObject.getString("call_edate");
                                String call_type = entryObject.getString("call_type");
                                String caller = entryObject.getString("caller");
                                caller_number.add(caller);
                                String duartion = entryObject.getString("duartion");
                                String call_duartion = entryObject.getString("call_duartion");
                                duration.add(call_duartion);
                                String dtmf = entryObject.getString("dtmf");
                                String dt_num = entryObject.getString("dt_number");
                                dt_number.add((dt_num));
                                String recording_path = entryObject.getString("recording_path");
                                download_path.add(recording_path);
                                String agent_mobile = entryObject.getString("agent_mobile");
                                agent_number.add(agent_mobile);
                                String a_name = entryObject.getString("agent_name");
                                agent_name.add(a_name);

3 个答案:

答案 0 :(得分:0)

当您点击该项目时,您必须使用ArrayList作为您的数据。

public class DataModel{
 private String s_date,c_number,d_ration,s_time,download_path,a_number,a_name,dt_number;
private boolean isChildVisible;            
..... create it's setter and getter methods.
}

当调用itemclick时,您应该更新该位置的isChildVisible标志。

public void setSelectedIndex(int ind)
{
   arrayListDataModel.get(ind).setIsChildVisible(true);
    notifyDataSetChanged();
}

在你的getview方法中:

   public View getView(final int pos, final View v, ViewGroup g)
    {
        ----your initialization code.    
        RelativeLayout r = (RelativeLayout)row.findViewById(R.id.sub_layout);
        r.setVisibility(arrayListDataModel.get(pos).isChildVisible() ? View.VISIBLE : View.GONE);

        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    setSelectedIndex(pos);

            }
        });
    return row;
}

使用以下代码解析数据:

final ArrayList<DataModel> dataModelList = new ArrayList<>();
        while (callEntries.hasNext()) {
            DataModel  dataModel  = new DataModel();
            String entry = callEntries.next();

            //Log.v("unique keys are",entry);
            JSONObject entryObject = numberLog.getJSONObject(entry);
            jsonArray.put(entryObject.getString("start_date"));
            jsonArray.put(entryObject.getString("start_time"));
            jsonArray.put(entryObject.getString("end_date"));
            jsonArray.put(entryObject.getString("end_time"));
            jsonArray.put(entryObject.getString("recording_path"));


            String startDate = entryObject.getString("start_date");
            dataModel.setStartDate(startDate);
            String startTime = entryObject.getString("start_time");
            dataModel.setStartTime(startTime);
            ...... same as above for others fields

            dataModelList.add(dataModel);        

        }

答案 1 :(得分:0)

您可以使用可扩展的listview轻松实现此功能,并且您的适配器类应该扩展BaseExpandableListAdapter

活动

private int lastExpandedPosition = -1;

expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                final int groupPosition, long id) {
            // TODO Auto-generated method stub

            if (parent.isGroupExpanded(groupPosition)) {

                expListView
                        .collapseGroup(lastExpandedPosition);
            } else {
                expListView.expandGroup(groupPosition);
            }

            return true;
        }
    });

    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            // TODO Auto-generated method stub
            if (lastExpandedPosition != -1
                    && groupPosition != lastExpandedPosition) {
                expListView
                        .collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
        }
    });

答案 2 :(得分:0)

创建两个变量:

 Map<Integer, View> lastExpanded = new HashMap<>();
    int lastExpandedPosition =-1;

在适配器中创建一个函数:

private void expand(int i, View layout){
        if(!lastExpanded.containsKey(i)){
            layout.setVisibility(View.VISIBLE);
            lastExpanded.put(i, layout);

            if(lastExpanded.containsKey(lastExpandedPosition)){
                expand(lastExpandedPosition, lastExpanded.get(lastExpandedPosition));
            }
            lastExpanded.remove(lastExpandedPosition);
            lastExpandedPosition = i;
            lastExpanded.put(lastExpandedPosition, layout);
        }else{
            layout.setVisibility(View.GONE);
            lastExpandedPosition = -1;
            lastExpanded.remove(i);
        }
    }

现在,

final RelativeLayout layout = (RelativeLayout) row.findViewById(R.id.b);
        layout.setVisibility(View.GONE);
        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                expand(i, layout);

            }
        });