我正在开发应用程序,其中我想在列表视图中显示一些引号和语句,并且还希望在用户单击列表项时显示新布局文件中的每个引号。这是我的主要活动代码与简单列表视图: -
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Created by Tauseef on 11/11/2015.
*/
public class MainActivity extends AppCompatActivity {
private String[] loveArray = {
"Jo Pyar Hamesha Sath Rahe Wo Sacha Hai Ji,\n" +
"Jo Musibaton Mein Kaam Aaye Wo Acha Hai Ji,\n" +
"Kabhi Kabhi Humse Bhi Ho Jati Hai Nadaniya,\n" +
"Kyonki Dil To Baccha Hai Ji.", "Chor to sakta hu,\n" +
"Magar Chor nahipate use,\n" +
"Wo shaks meri bigri huyi,\n" +
"Aadat ki trha hai.",
"Ek shaam aati hai tumhari yaad lekar,\n" +
"Ek shaam jaati hai tumhari yaad dekar,\n" +
"Par hume intezaar hai us shaam ka,\n" +
"Jo aaye sirf tumhe saath lekar"
};
private ListView loveListView;
private ArrayAdapter arrayAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_love);
Intent intent = getIntent();
loveListView = (ListView) findViewById(R.id.loveList);
// this-The current activity context.
// Second param is the resource Id for list layout row item
// Third param is input array
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, loveArray);
loveListView.setAdapter(arrayAdapter);
loveListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newIntent = new Intent(LoveActivity.this, MainContent.class);
newIntent.putExtra("loveArray", position);
startActivity(newIntent);
}
});
}
}
以下是仅包含textview的新活动,我想在其中将所点击的列表项目显示为textView: -
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
/**
* Created by Tauseef on 11/11/2015.
*/
public class MainContent extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView =(TextView)findViewById(R.id.mainContentTextView);
Intent intent = getIntent();
String data = "";
if(intent!= null){
Bundle extras = intent.getExtras();
if(extras != null){
data = extras.getString("loveArray");
}
}
}
}
每当我运行这个应用程序并点击列表项时,它会显示空指针和其他一些东西。我没有得到这个代码中实际出现的错误。
答案 0 :(得分:2)
你的爱情阵应该有多个项目。因此,当您通过单击listview启动活动时,它应该采取位置数组,以便更改
newIntent.putExtra("loveArray", position);
到
newIntent.putExtra("loveArray", loveArray[position]);
所以它需要位置数据的数据
答案 1 :(得分:1)
Bundle extras = intent.getExtras();
if(extras != null){
data = extras.getInt("loveArray");
}
并使用getInt("loveArray")
代替getString()
Bçoz你传递的位置,即int
Intent newIntent = new Intent(LoveActivity.this, MainContent.class);
newIntent.putExtra("loveArray", position);//Int not string
修改 如果您传递引用消息而不是位置,请在下方使用。
Intent newIntent = new Intent(LoveActivity.this, MainContent.class);
newIntent.putExtra("loveArray", loveArray [position]);// Pass the quote as string
然后
data = extras.getString("loveArray");
将有效
答案 2 :(得分:1)
问题来自您传递整数(位置)值的MainActivity活动,以及您收到字符串值的MainContent活动。你需要写
data = extras.getInt("loveArray");
墨水瓶架
data = extras.getString("loveArray");
答案 3 :(得分:1)
按照以下步骤
不要仅传递位置,只需通过执行
传递StringIntent intent = new Intent(LoveActivity.this, MainContent.class);
intent.putExtra("loveArray", loveArray[position]);// this will pass the string of this position`
然后在MainContent Activity中,执行
data= extras.getString("loveArray");
它必须工作`