单击列表项上的Android工具,通过自定义列表适配器将数据传递给新的Activity

时间:2015-11-29 22:04:40

标签: java android

我是Android开发新手,我尝试通过检查其他示例代码,我设法将数据从我的网站拉到自定义列表视图,我的主要活动显示: Main Activity Snapshot 目前,我在描述中显示的文字很少,我想如果我点击列表项,jobid将被带到我的案例中的新活动是Main2Activity。我的问题是:我不知道在列表项单击事件上实现的位置以及如何编写它以便它可以将jobid传递给Main2Activity。我的完整MainActivity类源代码如下。

package com.helloworld.kazitz.mnewonluine;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.helloworld.kazitz.mnewonluine.models.KaziModel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView tvData;
    private ListView lvKazi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvKazi = (ListView)findViewById(R.id.lvKazi);

        new JSONTask().execute("http://johnapps.ga/kazitz/kazitz.php");
    }


    public class JSONTask extends AsyncTask<String, String, List<KaziModel>> {

        @Override
        protected List<KaziModel> doInBackground(String... params) {

            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line = "";
                while((line = reader.readLine()) != null)
                {
                    buffer.append(line);
                }
                String finalJson = buffer.toString();

                JSONObject parentObject = new JSONObject(finalJson);
                JSONArray parentArray = parentObject.getJSONArray("kazitz");

                List<KaziModel> kaziModelList = new ArrayList<>();
                for(int i=0; i<parentArray.length(); i++)
                {
                    JSONObject finalObject = parentArray.getJSONObject(i);
                    KaziModel kaziModel = new KaziModel();
                    kaziModel.setId(finalObject.getInt("id"));
                    kaziModel.setTime(finalObject.getString("time"));
                    kaziModel.setDomain(finalObject.getString("domain"));
                    kaziModel.setTitle(finalObject.getString("title"));
                    kaziModel.setSdesc(finalObject.getString("sdesc"));
                    kaziModelList.add(kaziModel);
                }
                return kaziModelList;


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally{
                if(connection != null) {
                    connection.disconnect();
                }
                try {
                    if(reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<KaziModel> result) {
            super.onPostExecute(result);

            KaziAdapter adapter = new KaziAdapter(getApplicationContext(), R.layout.row, result);
            lvKazi.setAdapter(adapter);
            // TODO need to set data to the list
        }
    }



    public class KaziAdapter extends ArrayAdapter{

        private List<KaziModel> kaziModelList;
        private int resource;
        private LayoutInflater inflater;

        public KaziAdapter(Context context, int resource, List<KaziModel> objects) {
            super(context, resource, objects);
            kaziModelList = objects;
            this.resource = resource;

            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final int jobid = kaziModelList.get(position).getId();

            if(convertView == null){
                convertView = inflater.inflate(resource, null);
            }

            TextView tvSource;
            TextView tvDate;
            TextView tvTitle;
            TextView tvExcerpt;

            tvSource = (TextView)convertView.findViewById(R.id.tvSource);
            tvDate = (TextView)convertView.findViewById(R.id.tvDate);
            tvTitle = (TextView)convertView.findViewById(R.id.tvTitle);
            tvExcerpt = (TextView)convertView.findViewById(R.id.tvExcerpt);

            tvSource.setText(kaziModelList.get(position).getDomain());
            tvDate.setText(kaziModelList.get(position).getTime());
            tvTitle.setText(kaziModelList.get(position).getTitle());
            tvExcerpt.setText(kaziModelList.get(position).getSdesc());

            return convertView;
        }
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_refresh) {
            new JSONTask().execute("http://johnapps.ga/kazitz/kazitz.php");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

lvKazi=...行:

之后立即将此添加到您的onCreate
lvKazi.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            //do what you are trying to do here
        }
    });