我有一个包含listview的应用。单击列表视图项时,它会打开一个包含textview和Burton的新活动。
Here是我的列表视图的图片。
现在,如果我按下listview项目1的活动内的按钮,我希望它打开活动1,如果我按下listview项目2的活动内的按钮,我希望它打开活动2,依此类推&#34 ; N"按钮点击我想要" n"活动开放。这可能吗?
main activity.java
public class MainActivity extends Activity {
// Declare Variables
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Country");
query.orderByDescending("_created_at");
try {
ob = query.find();
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into an ArrayAdapter
adapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.listview_item);
// Retrieve object "name" from Parse.com database
for (ParseObject country : ob) {
adapter.add((String) country.get("name"));
}
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Capture button clicks on ListView items
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(MainActivity.this,
SingleItemView.class);
// Pass data "name" followed by the position
i.putExtra("name", ob.get(position).getString("name")
.toString());
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
}
}
singleitemview。爪哇
public class SingleItemView extends Activity {
// Declare Variables
TextView txtname;
String name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.singleitemview);
// Retrieve data from MainActivity on item click event
Intent i = getIntent();
// Get the name
name = i.getStringExtra("name");
// Locate the TextView in singleitemview.xml
txtname = (TextView) findViewById(R.id.name);
// Load the text into the TextView
txtname.setText(name);
}
}
答案 0 :(得分:0)
根据listView的设置,你要求的是什么,并且你想要在每个项目点击上打开不同的活动,因为你在第一个项目上表示Activiy1,在第二个项目上点击2,依此类推。
你可以用listView.setOnItemClickListener。
来做ListView list = (ListView) findViewById(R.id.listViewXML_ID);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
//Toast.makeText(getApplicationContext(), (String)parent.getItemAtPosition(position), Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this,Activity1.class);
startActivity(i);
break;
case 1:
Intent i1 = new Intent(MainActivity.this,Activity2.class);
startActivity(i1);
break;
}
}
});
忘记在清单文件中声明这些活动。
<activity android:name="packageName.Class1"></activity>
<activity android:name="packageName.Class2"></activity>
活动1的代码:
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity1extends Activity{
Button btnActivity1;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnActivity1 = (Button) findViewById(R.id.btnID_in_XML);//make a button in activity_main and assign it an id like i did here.
btnActivity1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Activity1.this,Class2.class);
startActivity(i);
}
});
}
通过这种方式,您可以从单击List ...
打开的活动中打开活动