如何在不使用rowID或OnClickEvent的情况下读取ListView中特定textview的内容?

问题描述 投票:0回答:1

我的应用程序正确填充ListView。当用户点击该行时,我需要找出特定TextView的内容。我不需要RowID,由于应用程序的工作原理,我无法使用OnClickEvent。

如何在点击的行上获取TextView的当前显示内容?

编辑:以下是我如何实施使用onItemClick的建议。我的布局使用FrameLayout来允许点击行的右侧或左侧。根据侧面轻敲,它会触发不同的活动。这两个活动都需要TextView的内容名为bonusListCode,这是我正在尝试获取的内容。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e(TAG,"Entered onItemClickListener");
            Intent nextActivity = new Intent(bonusListing.this, bonusListing.class);
            startActivity(nextActivity);
        }

当我点击我的单元格时,它会进入相应的活动,但是我没有看到上面代码应该触发的日志条目,因此它没有被触发。我在onItemClickListener做错了什么?

编辑2:我看到一些较旧的帖子表明从我的光标中拉出这个可能更容易,因为我使用的是SimpleCursorAdapter。以下是我的:

// Populate the ListView w/ all rows
        Cursor data = appDBHelper.getAppDataAllBonuses();
        listView.setAdapter(new SimpleCursorAdapter(this, R.layout.bonus_list_row_item, data,
                new String[] {"sCode", "sName", "sCategory", "sCity", "sState"},
                new int[] {R.id.bonusListCode, R.id.bonusListName, R.id.bonusListCategory, R.id.bonusListCity, R.id.bonusListState}, 0));

编辑3:每个请求的附加代码。这是我的bonusListing.java文件,这就是我填充ListView的方式。

public class bonusListing extends AppCompatActivity {

    private static String TAG = "bonusListing"; // Tag just for the LogCat window
    SharedPreferences sharedpreferences;

    UserDataDBHelper userDBHelper;
    AppDataDBHelper appDBHelper;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bonus_listing);
        sharedpreferences = getSharedPreferences(tohPreferences,
                Context.MODE_PRIVATE);
        Toolbar myToolbar = findViewById(R.id.toolbar);
        setSupportActionBar(myToolbar);
        ListView listView = findViewById(R.id.lvBonusData);

        // Handle the appData DB.
        appDBHelper = new AppDataDBHelper(this);

        try {
            Log.e(TAG,"Calling createDatabase");
            appDBHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to CREATE DATABASE");
        }

        // Populate the ListView w/ all rows
        Cursor data = appDBHelper.getAppDataAllBonuses();
        listView.setAdapter(new SimpleCursorAdapter(this, R.layout.bonus_list_row_item, data,
                new String[] {"sCode", "sName", "sCategory", "sCity", "sState"},
                new int[] {R.id.bonusListCode, R.id.bonusListName, R.id.bonusListCategory, R.id.bonusListCity, R.id.bonusListState}, 0));

        // Print to Log the DB headers
        CommonSQLiteUtilities.logDatabaseInfo(appDBHelper.getWritableDatabase());


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e(TAG,"Entered onItemClickListener");
                Intent nextActivity = new Intent(bonusListing.this, bonusListing.class);
                startActivity(nextActivity);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Log.e(TAG,"action_setting");
                startActivity(new Intent(this, appSettings.class));
                return true;

            case R.id.action_trophyMode:
                Log.e(TAG,"action_trophyMode");
                startActivity(new Intent(this, trophyMode.class));
                return true;

            case R.id.action_filter:
                Log.e(TAG,"action_filter");
                // Populate the ListView w/ filtered data
                ListView listView = findViewById(R.id.lvBonusData);
                Cursor data = appDBHelper.getAppDataFilteredBonuses();
                listView.setAdapter(new SimpleCursorAdapter(this, R.layout.bonus_list_row_item, data,
                        new String[] {"sCode", "sName", "sCategory", "sCity", "sState"},
                        new int[] {R.id.bonusListCode, R.id.bonusListName, R.id.bonusListCategory, R.id.bonusListCity, R.id.bonusListState}, 0));
                return true;

            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    public int getIdFromClassName(String className){
        String query = "SELECT rowid" +
                " FROM " + CLASSES_TABLE_NAME +
                " WHERE " + CLASSES_COLUMN_NAME + " = ?;";
        SQLiteDatabase db = this.getReadableDatabase();
        return DatabaseUtils.longForQuery(db, query, new String[]{ className });
    }

    public void goToAppSettings (View View) {
        Log.e(TAG,"goToAppSettings");
        Intent goToAppSettings = new Intent(this,appSettings.class);
        startActivity(goToAppSettings);
    }

    public void goToTrophyMode (View View) {
        Log.e(TAG,"goToTrophyMode");
        Intent goToTrophyMode = new Intent(this,trophyMode.class);
        startActivity(goToTrophyMode);
    }

    public void goToCaptureBonus (View View) {
        String tappedBonus = ((TextView) findViewById(R.id.bonusListCode)).getText().toString();
        Log.e(TAG,"goToCaptureBonus, tapped bonus = " + tappedBonus);
        Intent goToCaptureBonus = new Intent(this,captureBonus.class);
        goToCaptureBonus.putExtra("codeTapped",tappedBonus);
        startActivity(goToCaptureBonus);
    }

    public void goToBonusDetail (View View) {
        String tappedBonus = ((TextView) findViewById(R.id.bonusListCode)).getText().toString();

        Log.e(TAG,"goToBonusDetail, tapped bonus = " + tappedBonus);
        Intent goToBonusDetail = new Intent(this,bonusDetail.class);
        goToBonusDetail.putExtra("codeTapped",tappedBonus);
        startActivity(goToBonusDetail);
    }
}

编辑4:布局文件。这是我的bonus_list_row_item.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <ImageView
            android:id="@+id/bonusListImage"
            android:contentDescription="@string/mainImageDescription"
            android:layout_width="64dp"
            android:layout_height="48dp"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="8dp"
            android:src="@drawable/no_image_taken" />

        <TextView
            android:id="@+id/bonusListName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@id/bonusListImage"
            android:text="@string/valueBonusName" />

        <TextView
            android:id="@+id/bonusListCategory"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/bonusListName"
            android:layout_toEndOf="@id/bonusListImage"
            android:text="@string/valueCategory" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:orientation="horizontal"
            android:layout_below="@+id/bonusListCategory"
            android:layout_toEndOf="@id/bonusListImage">

            <TextView
                android:id="@+id/bonusListCode"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/valueBonusCode" />

            <TextView
                android:id="@+id/bonusListCity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="textEnd"
                android:text="@string/valueBonusCity"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/bonusListState"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="textEnd"
                android:layout_marginStart="4dp"
                android:text="@string/valueBonusState"
                android:textStyle="bold" />

        </LinearLayout>
    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:baselineAligned="false">

        <FrameLayout
            android:id="@+id/leftSideRowTapper"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.0"
            android:onClick="goToBonusDetail"
            android:layout_weight="1"/>

        <FrameLayout
            android:id="@+id/rightSideRowTapper"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.0"
            android:onClick="goToCaptureBonus"
            android:layout_weight="1"/>

    </LinearLayout>
</FrameLayout>

如果它有用,这里也是activity_bonus_listing.xml。

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:layout_alignParentTop="true"/>

        <ListView
            android:id="@+id/lvBonusData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/toolbar">
        </ListView>
    </RelativeLayout>
android
1个回答
0
投票

您不必实现OnClick事件,而是实现OnItemClickListener,如下所示:

lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
© www.soinside.com 2019 - 2024. All rights reserved.