我的主要活动列表视图从数据库中获取数据。
我遇到的问题是根据用户在列表中选择的内容将文本传递给下一个活动。
我正在获取列表视图以打开下一个活动并将一些奇怪的字符串发送到下一个活动,但这不是正确的信息发送。这是我的代码:
主要活动代码:
package com.example.chris.gymtracker;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Days extends AppCompatActivity {
dbAddDays dbAddDays;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_days);
dbAddDays = new dbAddDays(this);
final ListView daysListView = (ListView) findViewById(R.id.daysListView);
String[] fromFieldNames = new String[]{dbAddDays.ADDDAYS_KEY_DAYS};
Button addDaysButton = (Button) findViewById(R.id.addDaysButton);
Button refreshButton = (Button)findViewById(R.id.refreshButton);
addDaysButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent startAddDaysActivity = new Intent(Days.this, addDays.class);
startActivity(startAddDaysActivity);
}
});
refreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
populateDaysListView();
}
});
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, fromFieldNames);
daysListView.setAdapter(adapter);
daysListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id){
Intent myIntent = new Intent(view.getContext(),selectWorkout.class);
String s = daysListView.getItemAtPosition(position).toString();
myIntent.putExtra("itemSelected",s);
startActivityForResult(myIntent,position);
}
});
}
public void populateDaysListView() {
Cursor cursor = dbAddDays.getDaysData();
String[] fromFieldNames = new String[]{dbAddDays.ADDDAYS_KEY_DAYS};
int[] toViewIDs = new int[]{R.id.addDaysDaysTextView};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.activity_add_day_text_view, cursor, fromFieldNames, toViewIDs, 0);
ListView myList = (ListView) findViewById(R.id.daysListView);
myList.setAdapter(myCursorAdapter);
}
}
第二个活动代码:
package com.example.chris.tracker;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class selectWork extends AppCompatActivity {
dbAddDays dbAddDays;
Days Days;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_work);
Intent intentExtras = getIntent();
//Bundle bun = intentExtras.getExtras();
String s = intentExtras.getStringExtra("itemSelected");
TextView textView = (TextView)findViewById(R.id.textView3);
textView.setText(s);
dbAddDays = new dbAddDays(this);
populateWorkoutGridView();
}
public void populateWorkGridView() {
Cursor cursor = dbAddDays.getWorkoutData();
String[] fromFieldNames = new String[]{dbAddDays.ADDDAYS_KEY_WORKOUT};
int[] toViewIDs = new int[]{R.id.workoutWorkoutTextView};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.activity_workout_text_view, cursor, fromFieldNames, toViewIDs, 0);
GridView myGrid = (GridView) findViewById(R.id.workoutGridView);
myGrid.setAdapter(myCursorAdapter);
}
}
问题:
But that bold Android. ... text view should show the text of the item clicked.
答案 0 :(得分:1)
由于您使用的是CursorAdapter,因此getItemAtPosition调用通过调用该对象上的toString()返回游标对象。
尝试以下代码。它尝试检索游标对象,然后通过fromFieldNames数组中的列名获取数据。
Cursor cursor = (Cursor)daysListView.getItemAtPosition(position);
cursor.getString(cursor.getColumnIndex(fromFieldNames[column]))
答案 1 :(得分:-1)
尝试使用:
Intent myIntent = new Intent(view.getContext(),selectWorkout.class);
myIntent.putExtra("itemSelected", fromFieldNames[position]);
startActivityForResult(myIntent,position);