Java Android自定义listview布局到另一个布局
我有一个自定义的Java Android自定义listview布局到另一个布局,java,android,android-layout,Java,Android,Android Layout,我有一个自定义的列表视图布局,是从网站上获得的。它允许我单击列表中的一个项目,并根据您单击的项目使用不同项目的新列表重新填充相同的布局。布局很好,我用从数据库中提取的信息填充它 我遇到的问题是,我希望它转到另一个布局,而不是在单击时用新列表重新填充布局。我尝试了几件事,但没有成功。这是我的密码。提前感谢您的帮助: public class firstactivity extends ListActivity { private LayoutInflater mInflater; private
列表视图
布局,是从网站上获得的。它允许我单击列表中的一个项目,并根据您单击的项目使用不同项目的新列表重新填充相同的布局。布局很好,我用从数据库中提取的信息填充它
我遇到的问题是,我希望它转到另一个布局,而不是在单击时用新列表重新填充布局。我尝试了几件事,但没有成功。这是我的密码。提前感谢您的帮助:
public class firstactivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
CustomAdapter adapter;
int pos=1;
public int picpos = 0;
LinkedList<String> region = new LinkedList<String>();
String name = null;
String something = null;
private Integer[] imgid = {
R.drawable.icon
};
RelativeLayout layr1;
Animation ar3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
String finaline = "";
//Get info from database
InputStream is = null;
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http connection
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:8888/PhpProject1/index.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag"," names: "+json_data.getString("name")
);
//Get an output to the screen
finaline = "\n\t" + jArray.getJSONObject(i);
something = finaline.substring(finaline.indexOf(',',10));
name = something.substring(9,finaline.indexOf('\"', 9));
region.add(something);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
//set list with information from database
for(int i=0;i<region.size();i++){
try {
rd = new RowData(i,region.get(i));
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
adapter = new CustomAdapter(this, R.layout.second_list,R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
//getListView().setOnItemClickListener(this);
}
/*private OnClickListener SaveListener = new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.newlayout);
}
};*/
/*protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
setContentView(R.layout.newlayout);
//final Intent intent = new Intent();
//startActivityForResult(intent, position);
}*/
public void onListItemClick(ListView parent, View v, int position, long id) {
adapter = (CustomAdapter) parent.getAdapter();
data.removeAllElements();
//String insert_list;
////
//if(position==0) {
/*setContentView(R.layout.newlayout);
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText((String)region.get(0));*/
//setListAdapter(adapter);
//getListView().setTextFilterEnabled(true);
if(position == 0)
setContentView(R.layout.newlayout);
}
private class RowData {
protected int mId;
protected String mTitle;
RowData(int id,String title){
mId=id;
mTitle = title;
}
@Override
public String toString() {
return mId+" "+mTitle;
}
公共类firstactivity扩展了ListActivity{
私人停车场;
专用矢量数据;
罗维达路;;
自定义适配器;
int pos=1;
公共int picpos=0;
LinkedList region=新建LinkedList();
字符串名称=null;
字符串something=null;
私有整数[]imgid={
R.drawable.icon
};
相对年轻人1;
动画ar3;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.main);
mInflater=(LayoutInflater)getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
数据=新向量();
字符串finaline=“”;
//从数据库获取信息
InputStream=null;
字符串结果=”;
ArrayList nameValuePairs=新的ArrayList();
//http连接
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2:8888/PhpProject1/index.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);
对于(int i=0;i,听起来您正在寻找的是一种进入新的活动
并显示新的列表视图
的方法。要进入新活动,您可以执行以下操作:
startActivity(new Intent(this, SecondActivity.class));
这行代码正在创建一个新的Intent
对象,它告诉Android下一步打开哪个Activity
。然后您将Intent
传递给当前Activity
的startActivity
方法。更详细的解释如下:
将一些信息传递给下一个活动
,例如所选状态的名称,对您来说可能也很有用。您可以通过将这些信息放入Intent对象中来实现:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("state", "New York");
startActivity(intent);
您可以通过执行以下操作从SecondActivity
中获取此数据:
Intent incomingIntent = getIntent();
String state = incomingIntent.getExtras().getString("state");
SecondActivity
指的是一个新类,它扩展了您需要在代码中定义的Activity
。不要忘了将它也添加到manifest.xml中,否则在尝试启动它时会出现异常。很难理解您的应用程序应该做什么,以及问题是什么。您可能想尝试编写一个更简洁的解释/问题。让我再试一次,这段代码从数据库提取字符串并创建一个列表,使用xml布局上名为second_list的自定义listview列表。列表上的每个项目都是可单击的。我从网站获得的原始代码使您在单击某个项目时清除了第二个_列表中的数据并添加了一个新列表,例如:打开应用程序时,我们有一个列表:“伊利诺伊州”、“印第安纳州”、“爱荷华州”、“堪萨斯州”、“肯塔基州”、“路易斯安那州”、“缅因州”。单击伊利诺伊州时,它将创建一个新列表,其中包含伊利诺伊州内的区域,在本例中为:“芝加哥大都会区”、“香槟-乌尔巴纳大都会区”,“伊利诺斯州中部”、“伊利诺斯州南部”、“肖尼山”、“福克斯山谷”、“大都会东部”、“美国底部”“。我想做的不是在同一版面上用新列表重新创建列表,而是希望它转到另一个版面。例如,在本例中,如果我单击伊利诺伊州,它会将我发送到另一个屏幕,该屏幕上有伊利诺伊州的图片和一些背景信息。我使用setContentView(R.layout.newlayout);将其发送到另一个屏幕就足够了,但它不起作用,希望这有助于理解我遇到的问题再次感谢!感谢您的帮助,我对android还是新手,这对我有很大帮助!我尝试按照您告诉我的做,但仍然出现错误,我没有以正确的方式在manifest.xml中添加活动我现在是这样的:我很难通过看你粘贴的内容来判断。如果你有第二个错误,最好在上面贴一个新问题。
Intent incomingIntent = getIntent();
String state = incomingIntent.getExtras().getString("state");