Android、Java、Onclick和;需要Arraylist帮助(我想是吧?)
嘿,伙计们强> 公正的警告:我对编程非常陌生,更不用说java和android了,所以坦率地说,我甚至不知道如何以正确的方式提出正确的问题。因此,我非常感谢你们中任何一位愿意花时间来研究这一问题并尽可能提供帮助的人!此外,如果你能找到一种方法,把你所有的解决方案都说得哑巴,并像我三岁那样解释它,我也会非常感激。我浏览了developer.android.com,所有的技术术语都让我的困惑变得更糟 所以到目前为止我所做的是强> 对于一个应用程序,我有以下代码(java和xml),它一旦打开,就会在activity_main.xml文件上显示我的应用程序的三个“类别”(即activities.java文件)。因为这三个都是相同的,所以我在下面发布了MidlandsActivity.java的代码 单击后,它将打开您单击的任何活动类别,并在list_items.xml文件中显示信息的arraylist(如MidlandsActivity.java文件所示) 所以现在。。。我希望能够做到的是: 我希望您能够单击列表中的任何项目(您当前可以单击,但目前它不执行任何操作),然后显示您在新活动中单击的任何列表项目(我想是吧?为了便于交谈,我们将其命名为ListDetails.java,但由于我迷路了,我还没有这样做。或者应该将其编码到MidlandsActivity.java和每个附加的类别活动中吗?)。然后将该活动显示到列表_details.xml文件中,以便全面查看所选类别arraylist中的数据 目前,我确实为此设置了xml文件(list_details.xml),但我不知道下一步需要做什么,也不知道该放在哪里 有什么帮助或建议吗Android、Java、Onclick和;需要Arraylist帮助(我想是吧?),java,android,xml,arraylist,onclick,Java,Android,Xml,Arraylist,Onclick,嘿,伙计们 公正的警告:我对编程非常陌生,更不用说java和android了,所以坦率地说,我甚至不知道如何以正确的方式提出正确的问题。因此,我非常感谢你们中任何一位愿意花时间来研究这一问题并尽可能提供帮助的人!此外,如果你能找到一种方法,把你所有的解决方案都说得哑巴,并像我三岁那样解释它,我也会非常感激。我浏览了developer.android.com,所有的技术术语都让我的困惑变得更糟 所以到目前为止我所做的是 对于一个应用程序,我有以下代码(java和xml),它一旦打开,就会在acti
Java文件
MidlandsActivity.java
java
java
package com.example.android.hourtedsc;
导入android.content.Context;
导入android.support.v4.content.ContextCompat;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类WordAdapter扩展了ArrayAdapter{
//此单词列表背景颜色的资源ID
私有int mColorResourceId;
//这里,我们为上下文和列表初始化ArrayAdapter的内部存储。
//第二个参数在ArrayAdapter填充单个TextView时使用。
//因为这是两个TextView和一个ImageView的自定义适配器,所以适配器不是
//我们将使用第二个参数,所以它可以是任何值。
publicWordAdapter(上下文上下文、ArrayList单词、int-colorResourceId){
超级(上下文,0,单词);
mColorResourceId=colorResourceId;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//检查现有视图是否正在重新使用,否则会膨胀视图
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
R.layout.list_项,父项,false);
}
//获取位于列表中此位置的{@link AndroidFlavor}对象
Word currentWord=getItem(位置);
//在list_item.xml布局中查找ID版本名称为的TextView
TextView titleTextView=(TextView)listItemView.findViewById(R.id.description\u text\u view);
//从当前AndroidFlavor对象获取版本名,然后
//在名称TextView上设置此文本
titleTextView.setText(currentWord.gettitle());
//在list_item.xml布局中查找ID版本号为的TextView
TextView地址TextView=(TextView)listItemView.findViewById(R.id.address\u text\u view);
//从当前AndroidFlavor对象获取版本号,然后
//在数字文本视图上设置此文本
addressTextView.setText(currentWord.getaddress());
//在list_item.xml布局中查找ID版本号为的TextView
TextView descriptionTextView=(TextView)listItemView.findViewById(R.id.title\u text\u视图);
//从当前AndroidFlavor对象获取版本号,然后
//在数字文本视图上设置此文本
descriptionTextView.setText(currentWord.getdescription());
//在list_item.xml布局中找到具有ID图像的ImageView。
ImageView ImageView=(ImageView)listItemView.findViewById(R.id.image\u视图);
//检查是否为此单词提供了图像
if(currentWord.hasImage()){
//如果图像可用,则根据资源ID显示提供的图像
setImageResource(currentWord.getImageResourceId());
//确保视图可见
设置可见性(View.VISIBLE);
}否则{
//否则隐藏ImageView(将可见性设置为GONE)
设置可见性(View.GONE);
}
//设置列表项的主题颜色
View textContainer=listItemView.findViewById(R.id.text\u容器);
//找到资源ID映射的颜色
int color=ContextCompat.getColor(getContext(),mColorResourceId);
//设置文本容器视图的背景色
textContainer.setBackgroundColor(颜色);
//设置列表项的主题颜色
View imageContainer=listItemView.findViewById(R.id.list\u item\u布局);
//找到资源ID映射的颜色
int icolor=ContextCompat.getColor(getContext(),mColorResourceId);
//设置文本容器视图的背景色
imageContainer.setBackgroundColor(icolor);
//返回整个列表项布局(包含两个TextView和一个ImageView)
//以便可以在ListView中显示它
返回listItemView;
}
}
package com.example.android.hauntedsc;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class MidlandsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
//Create an array of words
final ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
// Create an {@link ArrayAdapter}, whose data source is a list of Strings. The
// adapter knows how to create layouts for each item in the list, using the
// simple_list_item_1.xml layout resource defined in the Android framework.
// This list item layout contains a single {@link TextView}, which the adapter will set to
// display a single word.
WordAdapter adapter =
new WordAdapter(this, words, R.color.category_midlands);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link ArrayAdapter} we created above, so that the
// {@link ListView} will display list items for each word in the list of words.
// Do this by calling the setAdapter method on the {@link ListView} object and pass in
// 1 argument, which is the {@link ArrayAdapter} with the variable name itemsAdapter.
listView.setAdapter(adapter);
// Set a click listener to play the audio when the list item is clicked on
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Get the {@link Word} object at the given position the user clicked on
Word word = words.get(position);
}
});
}
}
package com.example.android.hauntedsc;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
//Find the View that shows the lowcountry catagory
TextView lowcountry = (TextView)findViewById(R.id.lowcountry);
//set a clicklistener on that View
lowcountry.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers category is clicked on.
@Override
public void onClick(View view) {
//Create a new intent to open the (@Link LowcountryActivity)
Intent lowcountryIntent = new Intent(MainActivity.this,LowcountryActivity.class);
//Start New Activity
startActivity(lowcountryIntent);
}
});
//Find the View that shows the upstate catagory
TextView upstate = (TextView)findViewById(R.id.upstate);
//set a clicklistener on that View
upstate.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers category is clicked on.
@Override
public void onClick(View view) {
//Create a new intent to open the (@Link UpstateActivity)
Intent upstateIntent = new Intent(MainActivity.this,UpstateActivity.class);
//Start New Activity
startActivity(upstateIntent);
}
});
//Find the View that shows the midlands catagory
TextView midlands = (TextView)findViewById(R.id.midlands);
//set a clicklistener on that View
midlands.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers category is clicked on.
@Override
public void onClick(View view) {
//Create a new intent to open the (@Link midlandsActivity)
Intent midlandsIntent = new Intent(MainActivity.this,MidlandsActivity.class);
//Start New Activity
startActivity(midlandsIntent);
}
});
}
}
package com.example.android.hauntedsc;
public class Word {
//
private String mDescription;
//
private String mAddress;
//
private String mTitle;
/** Image resource ID for the word */
private int mImageResourceId = NO_IMAGE_PROVIDED;
/** Constant value that represents no image was provided for this word */
private static final int NO_IMAGE_PROVIDED = -1;
public Word(String description, String address, String title) {
mDescription = description;
mAddress = address;
mTitle = title;
}
public Word(String description, String address, String title, int imageResourceId) {
mDescription = description;
mAddress = address;
mTitle = title;
mImageResourceId = imageResourceId;
}
//Get the description of the list item.
public String getdescription() { return mDescription;}
//Get the address of the list item.
public String getaddress() { return mAddress; }
//Get the title of the list item.
public String gettitle() { return mTitle; }
//Return the image resource ID of the list item.
public int getImageResourceId() { return mImageResourceId; }
//Returns whether or not there is an image for this list item.
public boolean hasImage() { return mImageResourceId != NO_IMAGE_PROVIDED; }
}
package com.example.android.hauntedsc;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class WordAdapter extends ArrayAdapter<Word> {
//Resource ID for background color of this list of words
private int mColorResourceId;
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
public WordAdapter(Context context, ArrayList<Word> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link AndroidFlavor} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView titleTextView = (TextView) listItemView.findViewById(R.id.description_text_view);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
titleTextView.setText(currentWord.gettitle());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView addressTextView = (TextView) listItemView.findViewById(R.id.address_text_view);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
addressTextView.setText(currentWord.getaddress());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView descriptionTextView = (TextView) listItemView.findViewById(R.id.title_text_view);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
descriptionTextView.setText(currentWord.getdescription());
// Find the ImageView in the list_item.xml layout with the ID image.
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image_view);
// Check if an image is provided for this word or not
if (currentWord.hasImage()) {
// If an image is available, display the provided image based on the resource ID
imageView.setImageResource(currentWord.getImageResourceId());
// Make sure the view is visible
imageView.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
imageView.setVisibility(View.GONE);
}
//Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
//find the color that the resource ID maps too
int color = ContextCompat.getColor(getContext(), mColorResourceId);
//Set the background color of the text container View
textContainer.setBackgroundColor(color);
//Set the theme color for the list item
View imageContainer = listItemView.findViewById(R.id.list_item_layout);
//find the color that the resource ID maps too
int icolor = ContextCompat.getColor(getContext(), mColorResourceId);
//Set the background color of the text container View
imageContainer.setBackgroundColor(icolor);
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="vertical"
tools:context="com.example.android.hauntedsc.MainActivity">
<!-- Lowcountry category ((formerly numbers))-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/category_lowcountry">
<TextView
android:id="@+id/lowcountry"
style="@style/CategoryStyle"
android:background="?android:attr/selectableItemBackground"
android:text="@string/category_lowcountry" />
</FrameLayout>
<!-- Midlands category ((formerly family))-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/category_midlands">
<TextView
android:id="@+id/midlands"
style="@style/CategoryStyle"
android:background="?android:attr/selectableItemBackground"
android:text="@string/category_midlands" />
</FrameLayout>
<!-- Upstate category ((formerly colors))-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/category_upstate">
<TextView
android:id="@+id/upstate"
style="@style/CategoryStyle"
android:background="?android:attr/selectableItemBackground"
android:text="@string/category_upstate" />
</FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list_item_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height"
android:background="@color/background"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height"/>
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/image_view"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="@+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:layout_weight="1"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"
tools:text=""/>
<TextView
android:id="@+id/address_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center_vertical"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"
tools:text="" />
<TextView
android:id="@+id/description_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0"
android:gravity="top"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"
tools:text="" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/detail_title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"/>
<TextView
android:id="@+id/detail_address_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"/>
<TextView
android:id="@+id/detail_description_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Get the {@link Word} object at the given position the user clicked on
//Word word = words.get(position);
Intent intent = new Intent(Midlands.this, ListDetails.class);
startActivity(intent);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Word word = words.get(position);
Intent intent = new Intent(MidlandsActivity.this, ListDetails.class);
intent.putExtra("myWord", word);
startActivity(intent);
}
});
Bundle data = getIntent().getExtras();
Word word = (Word) data.getParcelable("myWord");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Get the {@link Word} object at the given position the user clicked on
//Word word = words.get(position);
Intent intent = new Intent(Midlands.this, ListDetails.class);
Word word = word.get(position);
intent.putExtra("wordToBeDisplayed",word);
startActivity(intent);
}
Bundle b = getIntent().getExtras();
Word word = (Word) b.get("wordToBeDisplayed");
// The rest depends on your preferences about what to display