android - 如何在不使用 rowID 或 OnClickEvent 的情况下读取 ListView 中特定文本视图的内容?
问题描述
我的应用程序正在正确填充 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>
解决方案
您不必像这样实现OnClickEvent而是impelement 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();
}
});
推荐阅读
- sql - 如何将 varchar2 列更新为来自另一列的字符串 + 数值?
- python - 无法抑制在导入调用期间执行的警告
- python - Python Pandas 对数据框列进行部分切片和排序
- python - NSRANGE 异常
- r - 如何避免 rbind 将列从数字更改为字符?
- conv-neural-network - CNN中的几种架构需要什么
- flutter - 如何在颤振中使用 SfCartesianChart 自定义图例
- angular - RxJs 获得评论和作者
- mysql - Mysql有没有办法将同一id下的所有数据放在同一行中?
- powerbi - 尝试为来自同一数据表的相似数据创建包含两条线的折线图