开发者

My ListView consist an ImageView and a TextView i need to get the Text from the TextView

开发者 https://www.devze.com 2023-02-17 09:17 出处:网络
My ListView consist an ImageView and a TextView I need to get the Text from the TextView. int the position of my list where I press (onItemClick).

My ListView consist an ImageView and a TextView I need to get the Text from the TextView. int the position of my list where I press (onItemClick).

How can I do that?

The 1 class have a Button then when you press I moving to the next activity (CountryView) and expect to get back from the next activity with a text (name of the selected Country)

The 2 classes have a ListView (ImageView and TextView) the data is getting from a database and showing on the ListView.

My problem is to get back to the 1 class the selected name of the country.

Thanks so much for helping!!!

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.home);

   // final int recquestCode = 0;
    final Button btnCountry = (Button) findViewById(R.id.fromButton);
    btnCountry.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            Intent pingIntent = new Intent("CountryView");
            pingIntent.putExtra("btnText", " ");
            pingIntent.setClass(Travel.this, CountryView.class);
            startActivityForResult(pingIntent, RECEIVE_MESSAGE);
        }
    }); 

    /*  Button btnSearch = (Button) findViewById(R.id.searchButton);
    btnSearch.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            Intent intent = new Intent(v.getContext(), ResultView.class);
            startActivityForResult(intent, 0);
        }
    });*/
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{

    super.onActivityResult(requestCode, resultCode, data);
    if (data.hasExtra("response")){
        Button b = (Button)findViewById(R.id.fromButton);
        CharSequence seq = data.getCharSequenceExtra("response");
        b.setText(seq);
    }
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.country);

    mListUsers = getCountry();
    lvUsers = (ListView) findViewById(R.id.countrylistView);
    lvUsers.setAdapter(new ListAdapter(this, R.id.countrylistView, mListUsers)); 

   // lvUsers.setTextFilterEnabled(true);
   // String extraMsg1 = getIntent().getExtras().getString("extra1");

    lvUsers.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id)
        {
            // When clicked, show a toast with the TextView text
            //textItem=view;
            //         Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
            //       Toast.LENGTH_SHORT).show();

            Intent pongIntent = new Intent();
            // lvUsers.getItemAtPosition(position);

            t = (TextView) view;
            Log.v("fffvd"+t, null);
            t.setText(getIntent().getStringExtra("btnText"));
            String strText = t.getText().toString();

            //((TextView) view).getText().toString()

            pongIntent.putExtra("response",strText);
            setResult(Activity.RESULT_OK,pongIntent);
            finish();

            // startActivity(new Intent(CountryView.this,TravelPharmacy.class)); 
        }
     });
}

public ArrayList<Country> getCountry(){             
    DBHelper dbAdapter=DBHelper.getDBAdapterInstance(this);
    try {
        dbAdapter.createDataBase();
    } catch (IOException e) {
        Log.i("*** select ",e.getMessage());
    }
    dbAdapter.openDataBase();       
    String query="SELECT * FROM Pays;";
    ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
    dbAdapter.close();

    ArrayList<Country> countryList = new ArrayList<Country>();
    for (int i = 0; i < stringList.size(); i++) {
        ArrayList<String> list = stringList.get(i);
        Country country = new Country();
        try {
            //country.id = Integer.parseInt(list.get(0));
            country.pays = list.get(1);
        //  country.age = Long.parseLong(list.get(2));
        } catch (Exception e) {
            Log.i("***" + TravelPharmacy.class.toString(), e.getMessage());
        }
        countryList.add(country);
    }
    return countryList;
}

@Override
public void onDestroy()
{
    //     adapter.imageLoader.stopThread();
    lv.setAdapter(null);
    super.onDestroy();
}

public OnClickListener listener=new OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
        //       adapter.imageLoader.clearCache();
        ((BaseAdapter) adapter).notifyDataSetChanged();
    }
};

CountryAdapter Class

public class CountryAdapter extends BaseAdapter {
    private Activity activity;
    private String[] data;
    private  LayoutInflater inflater=null;
    // public ImageLoader imageLoader; 

    public CountryAdapter(Activity a, String[] d)
    {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public  class ViewHolder
    {
        public TextView text;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null)
        {
            vi = inflater.inflate(R.layout.singlecountry, null);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.text);;
            holder.image=(ImageView)vi.findViewById(R.id.image);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();

        holder.text.setText("item "+data[position]);
        holder.image.setTag(data[position]);
        return vi;
    }
}

ListAdapter Class

private class ListAdapter extends ArrayAdapter<Country> {  // --CloneChangeRequired
    private ArrayList<Country> mList;  // --CloneChangeRequired
    private Context mContext;

    public ListAdapter(Context context, int textViewResourceId,ArrayList<Country> list) { // --CloneChangeRequired
        super(context, textViewResourceId, list);
        this.mList = list;
        this.mContext = context;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        try{
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.singlecountry, null);    // --CloneChangeRequired(list_item)
        }
        final C开发者_运维百科ountry listItem = mList.get(position);   // --CloneChangeRequired                
        if (listItem != null) {
            // setting singleCountry views                      
        //  ( (TextView) view.findViewById(R.id.tv_id) ).setText( listItem.getId()+"");
            ( (TextView) view.findViewById(R.id.text) ).setText( listItem.getPays() );
            //((ImageView)view.findViewById(R.id.image)).setImageDrawable(drawable.world);
            //( (TextView) view.findViewById(R.id.tv_age) ).setText( listItem.getAge()+"" );

        }}catch(Exception e){
            Log.i(CountryView.ListAdapter.class.toString(), e.getMessage());                
        }
        return view;
    }
}


When you add things to the list, you can add hashmaps to the arraylist which the adapter looks at. Then you can grab the values which are name value pairs.


I think you want to get the position of the list you clicked, Its simple you can use OnItemClickListener as follows

YourList.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
                           int position=arg2;
                          // where position is the clicked position 

    }   }

If you stored your data in String array pass this position say array[position] to string array you can get the Text..

0

精彩评论

暂无评论...
验证码 换一张
取 消