2012-04-18 7 views
0

これで、リストアイテムから値を取得するためにOnItemClickListenerを取得しようとしていました。現時点では、クリックは登録されていますが、値は通過していません。私のコードの関連部分をHere're:OnItemClickListenerを使用してリストアイテムからnull値を取得する

public class DiarySchedule extends ListActivity implements OnClickListener 
{ 
    private DiaryDataSource datasource; 
    private static final String TAG = "MAD Diary Schedule"; 
    private String delTitle; 
    private String delDate; 
    private String delTime; 
    private String editTitle; 
    private String editDate; 
    private String editTime; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.diary_schedule); 

    datasource = new DiaryDataSource(this); 
    datasource.open(); 

    List<DiaryEntry> values = datasource.getAllDiaryEntries(); 

    DiaryScheduleAdapter adapter = new DiaryScheduleAdapter(this,values); 
    setListAdapter(adapter); 

    registerForContextMenu(getListView()); 


} 


public class DiaryScheduleAdapter extends ArrayAdapter<DiaryEntry> 
{ 
    private LayoutInflater li; 

    public DiaryScheduleAdapter(Context context, List<DiaryEntry> values) 
    { 
     super(context, 0, values); 
     li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     DiaryEntry diaryEntry = getItem(position); 

     View v = convertView; 
     if (v == null) 
     { 
      v = li.inflate(R.layout.diary_schedule, null); 
     } 

     TextView date = (TextView)v.findViewById(R.id.scheduleListDate); 
     String initialDate = diaryEntry.getDate(); 
     String formattedDate = ConvertToDate(initialDate); 
     date.setText(formattedDate); 

     TextView link = (TextView)v.findViewById(R.id.scheduleListLink); 
     link.setText(" at "); 

     TextView time = (TextView)v.findViewById(R.id.scheduleListTime); 
     time.setText(diaryEntry.getTime()); 

     TextView title = (TextView)v.findViewById(R.id.scheduleListTitle); 
     title.setText(diaryEntry.getTitle()); 

     v.setOnClickListener(new OnItemClickListener(position)); 

     return v; 
    } 

} 


@Override 
public boolean onCreateOptionsMenu (Menu menu) 
{ 
    new MenuInflater(getApplication()).inflate(R.menu.diary_menu, menu); 
    return (super.onCreateOptionsMenu(menu)); 
} 

@Override 
public boolean onOptionsItemSelected (MenuItem item) 
{ 
    switch (item.getItemId()) 
    { 
     case R.id.add: 
      Intent intent = new Intent(DiarySchedule.this, DiaryAddEntry.class); 
      startActivity(intent); 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 



@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) 
{ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.diary_context_menu, menu); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) 
{ 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 

    switch (item.getItemId()) 
    { 
     case R.id.edit: 
      Intent editIntent = new Intent(DiarySchedule.this, DiaryEditEntry.class); 

      editTitle = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListTitle)).getText(); 
      editDate = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListDate)).getText(); 
      editDate = GetInfoConvertToDate(editDate); 
      editTime = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListTime)).getText(); 

      editIntent.putExtra("title", editTitle); 
      editIntent.putExtra("date", editDate); 
      editIntent.putExtra("time", editTime); 

      startActivity(editIntent); 
      break; 

     case R.id.delete: 
      delTitle = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListTitle)).getText(); 
      delDate = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListDate)).getText(); 
      delDate = GetInfoConvertToDate(delDate); 
      delTime = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListTime)).getText(); 

      Log.v(TAG, "Hopefully title is: " + delTitle + " with date of " + delDate + " and time of " + delTime); 

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) 
       .setNegativeButton("No", dialogClickListener).show(); 

      break; 
    } 
    return super.onContextItemSelected(item); 
} 



DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() 
{ 
    @Override 
    public void onClick(DialogInterface dialog, int which) 
    { 
     switch (which) 
     { 
      case DialogInterface.BUTTON_POSITIVE: 
      datasource.deleteDiaryEntry(delTitle, delDate, delTime); 

      // IN CASE NEED TO DELETE ALL DB ENTRIES UNCOMMENT THIS 
      // (AND COMMENT THE ABOVE METHOD) 
      //datasource.deleteAll(); 

      Intent intent = new Intent(DiarySchedule.this, DiarySchedule.class); 
      startActivity(intent); 
      break; 

      case DialogInterface.BUTTON_NEGATIVE: 
      // No action taken 
      break; 
     } 
    } 
}; 



@Override 
public void onClick(View v) 
{ 
    // TODO Auto-generated method stub 

} 

private class OnItemClickListener implements OnClickListener 
{   
    private int mPosition; 

    OnItemClickListener(int position) 
    { 
      mPosition = position; 
    } 

    @Override 
    public void onClick(View v) 
    { 
     Log.v(TAG, "onItemClick at position" + mPosition); 
     final String title = (String) ((TextView) findViewById(R.id.scheduleListTitle)).getText(); 
     System.out.println("Title is: " + title); 
     String date = (String) ((TextView) findViewById(R.id.scheduleListDate)).getText(); 
     date = GetInfoConvertToDate(date); 
     System.out.println("Date is: " + date); 
     final String time = (String) ((TextView) findViewById(R.id.scheduleListTime)).getText(); 
     System.out.println("Time is: " + time); 

     Intent descIntent = new Intent(DiarySchedule.this, DiaryDetailed.class); 

     descIntent.putExtra("title", title); 
     descIntent.putExtra("date", date); 
     descIntent.putExtra("time", time); 

     startActivity(descIntent); 
    }    
} 

} 

それをデバッグOnItemClickListenerのOnClickメソッドに到達し、適切にそれを通過するが、ちょうど空の出てくる値を、ピックアップしていません。何か案は?おかげ

+0

問題が解決しました - 提案を投稿したすべての人に感謝します! –

答えて

1

同じID各行に1つずつ)。取得すると値は、取得しようとしている他の値のため

final String title = (String) ((TextView) v.findViewById(R.id.scheduleListTitle)).getText(); 

同じことで

final String title = (String) ((TextView) findViewById(R.id.scheduleListTitle)).getText(); 

を交換してみてください。

+0

ありがとうazertiti - これは私がそれを働かせるために欠けていたものでした! –

1

あなたが過剰に複雑にあなたがOnItemClickListenerを実装する必要があり、あなたのメインのDiarySchedule活性のもの

大変です。 リスナーのonItemClickメソッドをオーバーライドし、そこでアクションを実行します。 getListView()を呼び出してリスナーを設定します。setOnItemClickListener(this);あなたのonCreateメソッドで..

Ex。

public class DiarySchedule extends ListActivity implements OnItemClickListener 
{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.diary_schedule); 

    datasource = new DiaryDataSource(this); 
    datasource.open(); 

    List<DiaryEntry> values = datasource.getAllDiaryEntries(); 

    DiaryScheduleAdapter adapter = new DiaryScheduleAdapter(this,values); 
    setListAdapter(adapter); 

    registerForContextMenu(getListView()); 

    getListView().setOnItemClickListener(this); //this sets the listener 


    } 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) 
    { 

     String title = ((TextView)v.findViewById(R.id.scheduleListTitle)).getText().toString(); 
     String date = ((TextView)v.findViewById(R.id.scheduleListDate)).getText().toString(); 
     date = GetInfoConvertToDate(date); 
     String time = ((TextView)v.findViewById(R.id.scheduleListTime)).getText().toString(); 


     Intent descIntent = new Intent(DiarySchedule.this, DiaryDetailed.class); 
     descIntent.putExtra("title", title); 
     descIntent.putExtra("date", date); 
     descIntent.putExtra("time", time); 

     startActivity(descIntent); 

    } 

} 

次に、v.setOnClickListener(new OnItemClickListener(position))の呼び出しを削除できます。アダプターと、クリックを処理するために作成したクラスに含まれています。

onItemClickメソッドで「findViewById」を使用しようとすると、「v.findViewById(id);」を使用する必要があります。あなたがビュー "v"内で検索しているからです。

これはあなたの問題です。 (そしてあなたはそれを修正することができます)しかし、リストビューの各項目に個別のハンドラを持たせる代わりに、上記のように "適切な"方法をお勧めします。

+0

ありがとうdymmeh!私は間違いなくこのようなヒントを持ってすべてのコードを磨くように戻ってくるだろうが、私は厳しい制限を受けているので、今は仕事をしているだけだ。乾杯! –

+0

さて、これで2分かかります。私が上に持っているものをコピーして、 "v.findViewById"で修正したコードをonclickから取り出し、上のonItemClickに貼り付けます。位置をmPositionに変更します。 :)より少ない+クリーンなコード、少ないクラス、それぞれのビューの個々のクリックリスナーを持つことに関連するオーバーヘッドが少なくなりました。変更するのにかかる時間のほんの少しの価値があります – dymmeh

+0

私は、あなたがすべき変更を含めるようにソースを更新しました。今あなたはそれを実装していないために0の言い訳を持っています;) – dymmeh

関連する問題