2012-03-09 2 views
8

텍스트 항목 목록을 화면에 표시하고 클릭 할 수있게해야합니다. 그래서 웹 애플리케이션의 링크 목록과 같은 것입니다.Android 앱 - 항목 목록을 표시하고 클릭 할 수있는 방법

Android 활동 화면에서 어떻게 할 수 있습니까?

DB에서 가져와 링크로 모두 표시해야하는 항목의 임의의 숫자가 될 것입니다.

어떻게 할 수 있습니까?

답변

3

ListView을 사용해야합니다. 매우 간단합니다. ListActivity을 작성하고 Adapter에 상품을 넣은 다음 AdapterListActivity으로 설정하십시오.

당신은 ListFragment라는 새로운 패러다임도 있습니다의 ListView here

1

에 대한 자세한 내용을보실 수 있습니다.

전 ListViews를 사용 했었지만 이제 조각 접근 방식을 선호합니다. 항목을 선택할 때 화면의 다른 영역과의 상호 작용이 매우 유연하고 아주 적은 코드 만 필요하기 때문에 태블릿에서 매우 직선적이며 매우 유연한 esp입니다.

그냥 하나의 예 : 여기

public class Select_FoodCategories_Fragment extends android.app.ListFragment { 
    private static final boolean DEBUG = true; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    if (DEBUG) 
     Log.i(this.getClass().getSimpleName(), " ->" 
      + Thread.currentThread().getStackTrace()[2].getMethodName()); 
    super.onCreate(savedInstanceState); 

    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    if (DEBUG) 
     Log.i(this.getClass().getSimpleName(), " ->" 
      + Thread.currentThread().getStackTrace()[2].getMethodName()); 
    HoldingActivity a = (HoldingActivity) getActivity(); 
    //accessing a variable of the activity is easy 
    a.visibleListViewInFragment = getListView(); 

    List<XYZ> listTodisplay = a.getListToDisplay(); 

    MyAdapter adapter = new MyAdapter(
     getActivity(), 0, listTodisplay); 
    setListAdapter(adapter); 

    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
    if (DEBUG) 
     Log.i(this.getClass().getSimpleName(), " ->" 
      + Thread.currentThread().getStackTrace()[2].getMethodName()); 
     XYZ item = (XYZ) getListAdapter() 
     .getItem(position); 

    } 

} 

더 많은 정보 : 그런데 http://developer.android.com/reference/android/app/ListFragment.html

, 나는 그것이 정말 가치가 새로운 조각 개념에 익숙해 찾을는 - 그것은 단지 더 쉽게 살고 있습니다 - 태블릿에 관해서!

PS 나는 일부러에서 디버그 문을 왼쪽 - 그것은 알토 내 경험

8

예 당신이 그것을 할 수에서 훨씬 빠르게 전체 개념을 이해하는 데 도움이 때문이다. Db에서 가져올 DataExchange 클래스를 만듭니다. 문자열을 배열에 저장합니다.

데이터베이스에서 가져온 문자열 배열을 표시하는 ArrayAdapter를 만듭니다.

public class AndroidListViewActivity extends ListActivity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // storing string resources into Array 
    String[] numbers = {"one","two","three","four"} 
    // here you store the array of string you got from the database 

    // Binding Array to ListAdapter 
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label,  numbers)); 
    // refer the ArrayAdapter Document in developer.android.com 
    ListView lv = getListView(); 

    // listening to single list item on click 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 

      // selected item 
      String num = ((TextView) view).getText().toString(); 

      // Launching new Activity on selecting single List Item 
      Intent i = new Intent(getApplicationContext(), SingleListItem.class); 
      // sending data to new activity 
      i.putExtra("number", num); 
      startActivity(i); 

     } 
    }); 
} 
} 

에 대한

당신이 따라 다양한 레이아웃 파일을 생성해야

public class SingleListItem extends Activity{ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.single_list_item_view); 

    TextView txtProduct = (TextView) findViewById(R.id.product_label); 

    Intent i = getIntent(); 
    // getting attached intent data 
    String product = i.getStringExtra("number"); 
    // displaying selected product name 
    txtProduct.setText(product); 

} 
} 

해야 클릭 한 특정 항목을 표시하는 secondActivity .. 희망이 당신을 도와줍니다 :)

관련 문제