如何通过单击在新布局中显示 Android ListView 项目
How to show Android ListView item in new layout with click
我正在开发应用程序,我想在列表视图中显示一些引语和谚语,并且还想在用户单击列表时在新布局文件中显示每个引语 item.This 我的主要代码 Activity 使用简单的列表视图:-
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);
}
});
}
}
这里是新的 activity 只有文本视图,我想在其中将单击的列表项显示为文本视图:-
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");
}
}
}
}
每次当我 运行 这个应用程序并点击列表项时,它会显示空指针异常和其他一些东西。我没有得到这段代码中实际出现的错误类型。
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");
会起作用
问题出在您的 MainActivity activity 您传递了一个整数(位置)值,而您从 MainContent activity 收到了一个字符串值。你需要写
data = extras.getInt("loveArray");
的墨水瓶
data = extras.getString("loveArray");
您的 loveArray 应该有不止一项。因此,当您通过单击列表视图启动 activity 时,它应该占据位置数组,因此更改
newIntent.putExtra("loveArray", position);
至
newIntent.putExtra("loveArray", loveArray[position]);
所以它需要特定位置数组的数据
按照以下步骤操作
不是只传递位置,而是通过
传递字符串
Intent 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");
它必须有效`
我正在开发应用程序,我想在列表视图中显示一些引语和谚语,并且还想在用户单击列表时在新布局文件中显示每个引语 item.This 我的主要代码 Activity 使用简单的列表视图:-
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);
}
});
}
}
这里是新的 activity 只有文本视图,我想在其中将单击的列表项显示为文本视图:-
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");
}
}
}
}
每次当我 运行 这个应用程序并点击列表项时,它会显示空指针异常和其他一些东西。我没有得到这段代码中实际出现的错误类型。
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");
会起作用
问题出在您的 MainActivity activity 您传递了一个整数(位置)值,而您从 MainContent activity 收到了一个字符串值。你需要写
data = extras.getInt("loveArray");
的墨水瓶
data = extras.getString("loveArray");
您的 loveArray 应该有不止一项。因此,当您通过单击列表视图启动 activity 时,它应该占据位置数组,因此更改
newIntent.putExtra("loveArray", position);
至
newIntent.putExtra("loveArray", loveArray[position]);
所以它需要特定位置数组的数据
按照以下步骤操作
不是只传递位置,而是通过
传递字符串Intent 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");
它必须有效`