我想让它可以点击ListView中的某个项目,然后打开一个活动来描述该用户点击的事情。我是Android开发的新手,所以很感激建设性的批评(Aka我知道这段代码很难看。)
package com.example.something.myapplication;
import android.content.Intent;
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.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import static android.widget.AdapterView.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[]Planets = {"Mercury", "Venus", "Earth", "Mars" , "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Planets)
ListView PlanetList = (ListView) findViewById(R.id.PlanetList);
PlanetList.setAdapter(adapter);
PlanetList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg) {
//What do I change to make the thing in the list open a new activity about that item on the list?
String planetPicked = "You selected " + String.valueOf(adapterView.getItemAtPosition(position));
Toast.makeText(MainActivity.this, planetPicked, Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:0)
您需要一个意图:
Intent itemIntent = new Intent(MainActivity.this, your_seond_activity.class);
startActivity(itemIntent);
您可能希望将一些数据传递给新活动,为此您可以使用InentExtra,因此代码将为:
Intent itemIntent = new Intent(MainActivity.this, your_seond_activity.class);
itemIntent.putExtra("some_key", some_data);
startActivity(itemIntent);
在这里阅读:
http://developer.android.com/reference/android/content/Intent.html
在这里
答案 1 :(得分:0)
我首先要说的是,在java中,通常变量以小写字母开头,类以大写字母开头。
getIntent().getStringExtra(getString("SOME_KEY_VALUE"));
访问您加载到意图使用方法
的附加内容{{1}}
此示例用于获取您放入额外内容的字符串值。您可以在Android开发人员指南中找到其他意图和附加方法。
答案 2 :(得分:0)
使用以下代码。 供参考click here to see the full code
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
// Array of strings storing country names
String[] countries = new String[] {
"India",
"Pakistan",
"Sri Lanka",
"China",
"Bangladesh",
"Nepal",
"Afghanistan",
"North Korea",
"South Korea",
"Japan"
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.india,
R.drawable.pakistan,
R.drawable.srilanka,
R.drawable.china,
R.drawable.bangladesh,
R.drawable.nepal,
R.drawable.afghanistan,
R.drawable.nkorea,
R.drawable.skorea,
R.drawable.japan
};
// Array of strings to store currencies
String[] currency = new String[]{
"Indian Rupee",
"Pakistani Rupee",
"Sri Lankan Rupee",
"Renminbi",
"Bangladeshi Taka",
"Nepalese Rupee",
"Afghani",
"North Korean Won",
"South Korean Won",
"Japanese Yen"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Country : " + countries[i]);
hm.put("cur","Currency : " + currency[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
// Item Click Listener for the listview
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
// Getting the Container Layout of the ListView
LinearLayout linearLayoutParent = (LinearLayout) container;
// Getting the inner Linear Layout
LinearLayout linearLayoutChild = (LinearLayout ) linearLayoutParent.getChildAt(1);
// Getting the Country TextView
TextView tvCountry = (TextView) linearLayoutChild.getChildAt(0);
Toast.makeText(getBaseContext(), tvCountry.getText().toString(), Toast.LENGTH_SHORT).show();
}
};
// Setting the item click listener for the listview
listView.setOnItemClickListener(itemClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}