2012-01-05 2 views
1

Im은 ListView와보기의 항목을 클릭 할 때 호출되는 다른 여러 가지 활동으로 구성된이 앱을 만들려고합니다. 그러나 주 활동에 onclicklistener 작업을 구현할 수 없습니다. 아래 코드를 사용하여 활동을 열 수 없습니다.ListView는 항목을 클릭 할 때 활동을 엽니 다?

을 heres 코드 :

public void setOnItemClickListener(AdapterView<?> parent, View view, 
     int position, long id) { 
    switch(position) 
    { 
     case 0: Intent newActivity0 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity0); 
       break; 
     case 1: Intent newActivity1 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity1); 
       break; 
     case 2: Intent newActivity2 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity2); 
       break; 
     case 3: Intent newActivity3 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity3); 
       break; 
     case 4: Intent newActivity4 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity4); 
       break; 
    } 
} 

어떤 도움을 주시면 감사하겠습니다 :) U는 전체 코드를 볼 필요가 있다면이 게시물에 업데이트됩니다, 코멘트. 감사!

답장을 보내 주셔서 감사합니다. Heres는 전체 코드 :

public class MainActivity extends ListActivity { 
private LayoutInflater mInflater; 
private Vector<RowData> data; 
RowData rd; 

static final String[] title = new String[] { 
    "Christmas Crepes", "Dorayaki", "Corned Beef", "Tempura" 
    }; 

static final String[] release = new String[] { 
"New!", "New!", "4 months ago", "New!" 
}; 

static final String[] description = new String[] { 
    "Blehh..","Pam pam pam","new era","Steve Jobs is dead" 
    }; 

static final String[] difficulty = new String[] { 
"Hard","Easy","Medium","Hard" 
}; 

static final String[] serves = new String[] { 
"4 persons","2 persons","2 cups","1 person" 
}; 

static final String[] time = new String[] { 
"40 mins","20 mins","50 mins","120 mins" 
}; 

private Integer[] imgid = { 
    R.drawable.thumbone,R.drawable.thumbtwo,R.drawable.thumbthree, 
    R.drawable.thumbfour 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE); 
data = new Vector<RowData>(); 
for(int i=0;i<title.length;i++){ 

try { 
rd = new  RowData(i,title[i],release[i],description[i],difficulty[i],serves[i],time[i]); 
    } catch (ParseException e) { 
    e.printStackTrace(); 
    } 
    data.add(rd); 
    } 
    CustomAdapter adapter = new CustomAdapter(this, R.layout.list, android.R.id.list, data); 
    setListAdapter(adapter); 
    getListView().setTextFilterEnabled(true); 
    } 
    public void setOnItemClickListener(AdapterView<?> parent, View view, 
     int position, long id) { 
    switch(position) 
    { 
     case 0: Intent newActivity0 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity0); 
       break; 
     case 1: Intent newActivity1 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity1); 
       break; 
     case 2: Intent newActivity2 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity2); 
       break; 
     case 3: Intent newActivity3 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity3); 
       break; 
     case 4: Intent newActivity4 = new Intent(this, TempuraActivity.class);  
       startActivity(newActivity4); 
       break; 
    } 
} 
    private class RowData { 
    protected int mId; 
    protected String mTitle; 
    protected String mRelease; 
    protected String mDescription; 
    protected String mDifficulty; 
    protected String mServes; 
    protected String mTime; 
    RowData(int id, String title, String release, String description, String difficulty, String serves, String time){ 
    mId=id; 
    mTitle = title; 
    mRelease = release; 
    mDescription = description; 
    mDifficulty = difficulty; 
    mServes = serves; 
    mTime = time; 
    } 
    @Override 
    public String toString() { 
      return mId+" "+mTitle+" "+mRelease+" "+mDescription+" "+mDifficulty+" "+mServes+" "+mTime; 
    } 
} 
private class CustomAdapter extends ArrayAdapter<RowData> { 

public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {    

super(context, resource, textViewResourceId, objects); 
} 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder = null; 
    TextView title = null; 
    TextView release = null; 
    TextView description = null; 
    TextView difficulty = null; 
    TextView serves = null; 
    TextView time = null; 
    ImageView thumbnail=null; 
    RowData rowData= getItem(position); 
    if(null == convertView){ 
     convertView = mInflater.inflate(R.layout.list, null); 
     holder = new ViewHolder(convertView); 
     convertView.setTag(holder); 
} 
     holder = (ViewHolder) convertView.getTag(); 
     title = holder.gettitle(); 
     title.setText(rowData.mTitle); 
     release = holder.getdescription(); 
     release.setText(rowData.mRelease); 
     description = holder.getrelease(); 
     description.setText(rowData.mDescription); 
     difficulty = holder.getdifficulty(); 
     difficulty.setText(rowData.mDifficulty); 
     serves = holder.getserves(); 
     serves.setText(rowData.mServes); 
     time = holder.gettime(); 
     time.setText(rowData.mTime); 

     thumbnail=holder.getImage(); 
     thumbnail.setImageResource(imgid[rowData.mId]); 
     return convertView; 
} 
     private class ViewHolder { 
     private View mRow; 
     private TextView title = null; 
     private TextView release = null; 
     private TextView description = null; 
     private TextView difficulty = null; 
     private TextView serves = null; 
     private TextView time = null; 
     private ImageView thumbnail=null; 

     public ViewHolder(View row) { 
     mRow = row; 
} 
    public TextView gettitle() { 
     if(null == title){ 
      title = (TextView) mRow.findViewById(R.id.title); 
      } 
     return title; 
    }  

    public TextView getrelease() { 
     if(null == release){ 
      release = (TextView) mRow.findViewById(R.id.release); 
      } 
     return release; 
    } 

    public TextView getdescription() { 
     if(null == description){ 
       description = (TextView) mRow.findViewById(R.id.description); 
       } 
     return description; 
    } 
    public TextView getdifficulty() { 
     if(null == difficulty){ 
      difficulty = (TextView) mRow.findViewById(R.id.difficulty); 
      } 
     return difficulty; 
    } 
    public TextView getserves() { 
     if(null == serves){ 
      serves = (TextView) mRow.findViewById(R.id.serves); 
      } 
     return serves; 
    } 
    public TextView gettime() { 
     if(null == time){ 
      time = (TextView) mRow.findViewById(R.id.title); 
      } 
     return time; 
    } 

    public ImageView getImage() { 
     if(null == thumbnail){ 
       thumbnail = (ImageView) mRow.findViewById(R.id.thumbnail); 
            } 
      return thumbnail; 
     } 
    }    
    } 
} 

내 애플, 게시하시기 바랍니다해야하는지 달성하기 위해 더 나은 방법이 있다면! 한 번만 끝내야 할 필사적 인 필자!

+2

에 응답하지 않는 경우 때를 어떻게됩니까 딸깍 하는 소리? 스위치에 들어 갑니까? 그것은 추락합니까? –

+0

전체 코드 게시, 구현에 문제가있는 것 같습니다 (예 : 코드가 setOnItemClickListener가 아닌 onItemClick에 있어야 함) –

+0

아무 것도 클릭하지 않을 때 – borislemke

답변

4

UPDATE

변경

public void setOnItemClickListener(AdapterView<?> parent, View view, 
    int position, long id) { 

protected void onListItemClick(ListView l, View v, int position, long id) { 

하고 그것을 작동합니다.

ORIGINAL

귀하의 코드는 setOnItemClickListener이있는, 조금 혼란하지만 실제로는 방법에게 onItemClickListener 객체 (또는 익명 클래스)를 통과해야합니다.

아마도 ListActivity에서 이와 같은 것을 찾고있을 것입니다.

final ListView listView = getListView(); 
listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, 
      long paramLong) { 
     switch(position) 
{ 
    case 0: Intent newActivity0 = new Intent(YourActivity.this, TempuraActivity.class);  
      startActivity(newActivity0); 
      break; 
    case 1: Intent newActivity1 = new Intent(YourActivity.this, TempuraActivity.class);  
      startActivity(newActivity1); 
      break; 
    case 2: Intent newActivity2 = new Intent(YourActivity.this, TempuraActivity.class);  
      startActivity(newActivity2); 
      break; 
    case 3: Intent newActivity3 = new Intent(YourActivity.this, TempuraActivity.class);  
      startActivity(newActivity3); 
      break; 
    case 4: Intent newActivity4 = new Intent(YourActivity.this, TempuraActivity.class);  
      startActivity(newActivity4); 
      break; 
}    
    } 
}); 

은 여기에서 onItemClickListeneronItemClick 방법 내부 스위치 문을 구현할 수 있으며 작동합니다.

참고 : 내부 클래스 내부에 있으므로 외부 활동 클래스를 참조해야하므로 this을 YourActivity로 변경해야합니다.

또는 당신 onListItemClick 무시할 수 :

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // ... put switch statement here 
} 

을 목록 활동에서 getListView()

에 추가 호출을 제거하기 위해이 질문하시기 바랍니다 전체 코드

+0

UR이 변경된 내용을 변경했으나 클릭하면 강제로 닫는다. 그러나 ur 메서드가 조금 도움이 된 것 같습니다. 클릭 할 때 아무 일도 일어나지 않았으므로 강제 종료됩니다. 그래서 기본적으로 그 라인 내에서 잡기.:( – borislemke

+0

나는 초기 질문에 대답했다. (비록 내가 틀렸을지라도) 의심된다. ​​새로운 문제가 청취자의 원래 문제와 관련 있다고 생각한다면, 오류 로그를 게시한다. 그렇지 않으면 새로운 질문을 만든다. 새로운 활동으로 인해 앱이 깨지기 만하면됩니다. – zode64

+0

오 마이 갓! 나쁘지 않아! 매니 페스트에서 활동을 선언하지 않았어! 네, 당신의 대답은 도움이되었습니다! 많이 감사합니다! – borislemke

관련 문제