从动态插入的ListView中获取项目

时间:2014-03-05 13:08:26

标签: android sqlite listview

我正在尝试从列表视图中获取项目。我正在创建许多动态布局(取决于数据库的输出),当我点击视图时,我想在Textviews中的另一个Activity中显示数据。我怎么安排这个?

这是我的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显示:

公共类DetailOutputActivity扩展了Activity {

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!

1 个答案:

答案 0 :(得分: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);

    }
});