Android自定义listview布局到另一个布局

时间:2012-03-10 20:00:07

标签: java android android-layout

我有一个从网站上获得的自定义ListView布局。它允许我从列表中单击某个项目,并根据您单击的项目使用新的不同项目列表重新填充相同的布局。布局工作正常,我得到它来填充从数据库中提取的信息。

我遇到的问题是,在点击时,不是让它用新列表重新填充布局,我希望它转到另一个布局。我尝试了几件事,但没有运气。这是我的代码。感谢您的帮助:

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;
    }

}

private class CustomAdapter extends ArrayAdapter<RowData> {

    public CustomAdapter(Context context, int resource,
                    int textViewResourceId, List<RowData> objects) {
            super(context, resource, textViewResourceId, objects);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;

            TextView title = null;
            TextView detail = null;
            ImageView i11=null;

            RowData rowData= getItem(position);

            if(null == convertView){
                    convertView = mInflater.inflate(R.layout.second_list, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);                   
            }
            holder = (ViewHolder) convertView.getTag();    

            title = holder.gettitle();     
            title.setText(rowData.mTitle);



            i11=holder.getImage();
            i11.setImageResource(imgid[0]);

            return convertView;
    }




    private class ViewHolder {      
        private View mRow;
        private TextView title = null;
        private TextView detail = null;
        private ImageView i11=null;            

            public ViewHolder(View row) {
            mRow = row;
            }

            public TextView gettitle() {
                    if(null == title){
                            title = (TextView) mRow.findViewById(R.id.title);
                    }
                    return title;
            }


            public ImageView getImage() {
                if(null == i11){

                    i11 = (ImageView) mRow.findViewById(R.id.img); 
            }

            return i11;
    }

}
}

}

1 个答案:

答案 0 :(得分:0)

听起来您正在寻找的是一种转换到新Activity并展示新ListView的方法。要转到新的活动,您可以执行以下操作:

startActivity(new Intent(this, SecondActivity.class));

这行代码正在创建一个新的Intent对象,告诉Android下一个Activity要打开的对象。然后,您将Intent传递给当前startActivity的{​​{1}}方法。更详细的解释如下:http://developer.android.com/reference/android/app/Activity.html#StartingActivities

将某些信息传递给下一个Activity也可能很有用,就像所选状态的名称一样。您可以通过将此信息放入Intent对象来实现:

Activity

您可以通过执行以下操作从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 不要忘记将它添加到您的manifest.xml ,否则在尝试启动时会出现异常。