2014-03-05 86 views
0

我想從列表視圖中獲取項目。我創建了許多動態佈局(取決於數據庫的輸出),當我點擊視圖時,我想在Textviews中的另一個活動中顯示數據。我該如何安排?從動態插入ListView獲取項目

這裏是我的活動使用ListView:

public void getRoute() { 

     mdbH = new DatabaseHelperActivity(this); 
     cursor = mdbH.fetchallRoutes(intent.getStringExtra("StartHaltestelle"),intent.getStringExtra("ZielHaltestelle"), intent.getStringExtra("Zeit")); 
     ArrayList<DefineRouteActivity> route = new ArrayList<DefineRouteActivity>(); 
     int i = 0; 
     while(cursor.moveToNext()) { 
      i++; 
      route.add(new DefineRouteActivity(cursor.getString(0),cursor.getString(2),cursor.getString(4))); 
     } 
     if(i == 0){ 
      AlertDialog alertDialog = new AlertDialog.Builder(
        PlanOutputActivity.this).create(); 

      // Setting Dialog Title 
      alertDialog.setTitle(getString(R.string.pDInfo)); 

      // Setting Dialog Message 
      alertDialog.setMessage(getString(R.string.pDErrorMessage)); 

      // Setting OK Button 
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // Write your code here to execute after dialog closed 
        Intent intent = new Intent(getApplicationContext(),PlanActivity.class); 
        startActivity(intent); 
       } 
      }); 

      // Showing Alert Message 
      alertDialog.show(); 
     } 

     ListView lvList = (ListView)findViewById(R.id.ListOutput); 
     ArrayAdapter<DefineRouteActivity> adapter = new RouteAdapterActivity(this, route); 
     lvList.setAdapter(adapter); 

     lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       Intent detail = new Intent(getApplicationContext(),DetailOutputActivity.class); 


       startActivity(detail); 
      } 
     }); 

    } 

這裏是我的活動,其中的字符串,應該與以下四個TextViews被dispalyed:

公共類DetailOutputActivity延伸活動{

Intent detail; 

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

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent detail) { 
    super.onActivityResult(requestCode, resultCode, detail); 

    detail = getIntent(); 
    if (resultCode == Activity.RESULT_OK) { 
     TextView txStart = (TextView)findViewById(R.id.txDetailOutputStart); 
     TextView txEnd = (TextView)findViewById(R.id.txDetailOutputEnd); 
     TextView txTime = (TextView)findViewById(R.id.txDetailOutputTime); 
     TextView txRoute = (TextView)findViewById(R.id.txDetailOutputRoute); 

     txStart.setText(detail.getStringExtra("start")); 
     /*txEnd.setText(detail.getStringExtra("EndStop")); 
     txTime.setText(detail.getStringExtra("Time")); 
     txRoute.setText(detail.getStringExtra("Route"));*/ 
    } 
} 

這是我的佈局,它被插入:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/outputlayout" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:background="@color/ligthgrey"> 

     <TextView 
      android:id="@+id/txOutputDeparture" 
      android:layout_width="130dp" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Abfahrt " 
      android:textColor="@color/black" 
      android:layout_alignParentLeft="true" 
      android:maxLength="@android:integer/config_shortAnimTime" 
      android:layout_marginTop="5dp" /> 

     <TextView 
      android:id="@+id/txOutputDuration" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/txOutputDeparture" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Dauer" 
      android:layout_marginTop="5dp" /> 

     <TextView 
      android:id="@+id/txOutputTransition" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/txOutputDuration" 
      android:text="Umstieg" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:layout_marginTop="5dp" /> 

    </RelativeLayout> 
</LinearLayout> 

當然我的Listview ID是ListOutput!

回答

0

在onItemClick()中使用被點擊項目的索引,並從您的數據源中獲取它,並使用它作爲適配器。 (例如傳遞給適配器的ArrayList對象)。然後使用Bundle/Extra和getIntent in Detail活動將它放入Intent中。

要獲得視圖中選擇列表項目,其數據(文本)做象下面這樣: -

public void onItemClick(AdapterView <? > parent, View view, 
     int position, long id) { 
     TextView txt = (TextView) view.findViewById(R.id.mylistviewtextview); 
     //here view is your selected list Item view 
     String keyword = txt.getText().toString(); 
     Log.v("value ", "result is " + keyword); 

    } 
}); 
+0

但我怎麼能得到這個TextView的......我的插入佈局包括4 TextView的......我應該得到他們通過findViewById? – schocka94

+0

你是在談論你的DetailOutputActivity的TextViews或你的列表項的Textviews。 – Ankit

+0

我在談論List元素......我如何從textvies中獲取這些字符串? – schocka94