2016-12-17 6 views
0

ListViewを使用していますが、ExtendedListViewを使用していません。私はrelative layoutをsub_listとして表示するために撮影しました。デフォルトでは、このsub_listの相対レイアウトの表示をGONEと設定しています。そして、listItemのクリックで、relative layout(デフォルトでは「なし」と設定されています)がそれぞれ画面に表示されます。しかし、いずれかの行をクリックすると、各行に同時にsub_listが表示されます。私が望むのは、一度に1つの行だけに表示する必要があるということです。私はすでにこの同様の[質問]を確認しました:How to modify only one row at a time in Listview?と私のコードを変更しましたが、私はまだ私の目標を達成することができません。Customized ListViewを使用して一度に1つの行だけを変更する方法

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(); 


     } 
    }); 

は、ここでは、コードの私のpieaceです。これは私がMainActivityで、アダプタを使用している方法です。このstart_datecaller_numberなどで

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); 

はarraylist.Thisあることは、私は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); 
+0

'notifyItemChanged(位置)' – rookieDeveloper

+0

あなたがこれを達成することができます:あなたのアダプタで関数を作成します

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

を簡単にExtendedListViewで。どうしてそんなに行かないの?特別な理由はありますか? – Nas

+0

私はExtendedListViewで同じことを試しましたが、うまくいきませんので、私はカスタマイズされたリストビューに戻りました。 @Nas –

答えて

0

二つの変数を作成します。

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); 

      } 
     }); 
+0

私はあなたのコードを使用しました。しかし、エラーを得る。 '私は' expand(i、layout)の '私は何ですか; –

+0

大丈夫です!私の場合はその 'pos'です。 –

+0

'i'はポジションです。ポジションを使用してください。 –

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のフラグを更新する必要があります。あなたのGetViewメソッドで

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

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);   

     } 
+0

ご回答ありがとうございます。私はこのコードを試してみます。arrayListDataModelとは何ですか?それはDataModelクラスのオブジェクトですか? –

+0

いいえ、あなたのすべてのデータ内容のarraylistです。あなたはアダプタのデータ個性を渡しましたが、ベストプラクティスはPojo/Modelクラスで作成して渡すのがベストプラクティスです。データを簡単に管理できます。 –

+0

私は前にPojo/modelを使っていません。あなたはそれについていくつかのリンクを提案することができます。それは私の問題を解決するでしょうか? –

0

あなたが拡張可能なリストビューで簡単にこれをacheiveすることができますし、あなたのアダプタクラスが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; 
     } 
    }); 
+0

編集した質問を確認してください。私は私のListViewに関連する完全なコードを共有している。あなたはまだ私はExpandable ListViewを使用することをお勧めしますか? @Nas –

+0

何が得られているかは、行をクリックすると展開する/特定の行の項目/項目を表示することです。別の行をクリックすると、前の行が閉じられ、現在のクリックされた項目のサブリスト/項目が展開/表示されます。そうですか? – Nas

+0

Expandable Listを使用せずに現在のコードが正常に動作しています。私が求めているものを達成するためにコード全体を変更する必要がありますか? –

関連する問題