📌  相关文章
📜  如何在 android 中将 textview 添加到 listview - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:37.693000             🧑  作者: Mango

代码示例1
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView tv;
    ListView lv;
      String s="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         tv=(TextView) findViewById(R.id.textView1);
         lv=(ListView) findViewById(R.id.listView1);

        String value[]={"asda","Ansar","Nisam"};
        ArrayAdapteradapter=new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_1,android.R.id.text1,value);
        lv.setAdapter(adapter);
//list item click
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView arg0, View arg1, int arg2,
                        long arg3) {
                     s=s+("\n")+((String) lv.getItemAtPosition(arg2));
                    tv.setText(s);
                }

            });

    }