我想为列表视图中的每个列表项创建视图 [英] i want to create view for each item of the list in the list view

查看:72
本文介绍了我想为列表视图中的每个列表项创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android studio中完成了以下代码。

现在我想在点击时在单独的视图中查看listview的每个元素



MAINACTIVITY.JAVA

  package  com.example.ramneekkashyap.app2; 

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
Button _loginBtn;
@覆盖
受保护 void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_loginBtn =(Button)findViewById(R.id.btn_login);

_loginBtn.setOnClickListener( new View.OnClickListener(){
@ Override
public void onClick(查看v){
// 这是一条对您的代码无益的评论。请在复制粘贴后随意将其删除。
// 单击该按钮时,控件将采用此方法。
// 为了证明这一点,让我们尝试将Button的标签从登录更改为我被点击

意图意图= 意图(MainActivity。, ORGANISATIONListActivity。);
startActivity(intent);
}

}); // 检查当前活动是否需要跳过{
}


@覆盖
public boolean onCreateOptionsMenu(菜单菜单){
// < span class =code-comment>给菜单充气;如果项目存在,则会将项目添加到操作栏。

getMenuInflater()。inflate(R.menu.menu_main,menu);
返回 true;
}

@覆盖
public boolean onOptionsItemSelected(MenuItem item){
// Handle操作栏项目点击此处。操作栏将
// 自动处理Home / Up按钮上的点击,
// 在AndroidManifest.xml中指定父活动。
int id = item.getItemId();

// noinspection SimplifiableIfStatement
if (id == R.id.action_settings){
return true;
}

return super .onOptionsItemSelected(item);
}
}









ACTIVITY_MAIN .XML

< relativelayout xmlns:android =   http://schemas.android.com/apk/res/android\"> 
xmlns:tools = http://schemas.android.com/tools android :layout_width = match_parent
android:layout_height = match_parent android:paddingLeft = @ dimen / activity_horizo​​ntal_margin
android:paddingRight = @ dimen / activity_horizo​​ntal_margin
android:paddingTop = @ dimen / activity_vertical_margin
android :paddingBottom = @ dimen / activity_vertical_margin工具:context = 。MainActivity
android:removed = #fffffff>

< textview android:text = @ string / hello_ORGANISATION android :layout_width = match_parent>
android:layout_height = wrap_content
android:removed = #ff7bffff
android:textColor = #ff000000
android:textSize = 26dp
android:id = @ + id / textView />

< linearlayout>
android:orientation = horizo​​ntal
android:layout_width = fill_parent
android:layout_height = wrap_content
android:layout_centerHorizo​​ntal = true
android:layout_below = @ + id / textView
android:id = @ + id / linearLayout>

< textview>
android:layout_width = wrap_content
android:layout_height = wrap_content
android:textAppearance = ?android:attr / textAppearanceMedium
android:text = 用户名
android:id = @ + id / textView2
android:textColor = #ff000000 />

< edittext>
android:layout_width = wrap_content
android:layout_height = wrap_content
android:inputType = textEmailAddress
android:ems = 10
android:id = @ + id / id_of_the_username_field
android:layout_weight = 1 />
< / edittext>< / textview>< / linearlayout>

< linearlayout>
android:orientation = horizo​​ntal
android:layout_width = fill_parent
android:layout_height = wrap_content
android:layout_alignParentLeft = true
android:layout_alignParentStart = true
android :layout_below = @ + id / linearLayout
android:id = @ + id / linearLayout2>

< textview>
android:layout_width = wrap_content
android:layout_height = wrap_content
android:textAppearance = ?android:attr / textAppearanceMedium
android:text = 密码:
android:id = @ + id / textView3
android:textColor = #ff000000 />

< edittext>
android:layout_width = wrap_content
android:layout_height = wrap_content
android:inputType = textPassword
android:ems = 10
android:id = @ + id / editText2
android:layout_weight = 1 />
< / edittext>< / textview>< / linearlayout>

<按钮
android:layout_width = fill_parent
android:layout_height = wrap_content
android:text = LOGIN
android:id = @ + id / btn_login
android:layout_below = @ + id / linearLayout2
android:layout_centerHorizo​​ntal = true
android:textSize = 25dp />

< / textview>< / relativelayout>





ORGANISATIONLISTACTIVITY.JAVA



  package  com.example.ramneekkashyap.app2; 

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class ORGANISATIONListActivity extends ListActivity {

private ArrayAdapter ORGANISATIONItemArrayAdapter;
@覆盖
受保护 void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_ORGANISATION_list);
ORGANISATIONItemArrayAdapter = new ORGANISATIONAdapter( this new 字符串 [ 10 ]);
setListAdapter(ORGANISATIONItemArrayAdapter);
}
@覆盖
protected void onListItemClick(ListView l,View v, int 位置, long id ){
TextView t =(TextView)v.findViewById(R.id.ORGANISATIONTitle);
t.setText( ORGANIZATION点击);
}


}





ORGANISATIONADAPTER.JAVA

  package  com.example.ramneekkashyap.app2; 

import android.view.ViewGroup;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;





public class ORGANISATIONAdapter extends ArrayAdapter {
private LayoutInflater inflater;

public ORGANISATIONAdapter(活动活动,字符串 []项目){
super (活动,R.layout.row_ORGANISATION,items);
inflater = activity.getWindow()。getLayoutInflater();
}

@覆盖
public 查看getView ( int position,View convertView,ViewGroup parent){
return inflater.inflate(R。 layout.row_ORGANISATION,parent,false);
}

}





ACTIVITY_ORGANISATION_ADAPTER.XML

< relativelayout xmlns:android =   http://schemas.android.com/apk/ RES /机器人> 
xmlns:tools = http://schemas.android.com/tools android :layout_width = match_parent
android:layout_height = match_parent android:paddingLeft = @ dimen / activity_horizo​​ntal_margin
android:paddingRight = @ dimen / activity_horizo​​ntal_margin
android:paddingTop = @ dimen / activity_vertical_margin
android :paddingBottom = @ dimen / activity_vertical_margin
tools:context = com.example.ramneekkashyap.app2.ORGANISATIONAdapter>

< textview android:text = @ string / hello_world android :layout_width = wrap_content>
android:layout_height = wrap_content />

< / textview>< / relativelayout>





ROW_ORGANISATION.XML



< linearlayout xmlns:android =   http://schemas.android.com/apk/res/android\"> 
android:layout_width = match_parent
android:layout_height = match_parent
android:orientation = horizo​​ntal>

< ImageView
android:id = @ + id / imageView1
android:layout_width = 100dp
android:layout_height = 100dp
android:src = @ drawable / user_profile
android:layout_marginLeft = 5dp
android:layout_marginTop = 5dp />


< linearlayout>
android:layout_width = wrap_content
android:layout_height = match_parent
android:orientation = vertical
android:layout_marginLeft = 10dp
android:gravity = left>


< textview>
android:id = @ + id / ORGANISATIONTitle
android:layout_width = match_parent
android:layout_height = wrap_removed
android:text = 标题文本
android:textSize = 19sp
android:textColor = #222222
android:textStyle = bold />


< textview>
android:id = @ + id / textView2
android:layout_width = match_parent
android:layout_height = wrap_content
android:text = ORGANIZATION正文文本
android:layout_marginTop = 5dp
android:ellipsize = end
android:lines = < span class =code-string> 3
android:textColor = #666666
android:textSize = 14sp /> ;


< textview>
android:id = @ + id / textView3
android:layout_width = match_parent
android:layout_height = wrap_content
android:text = 2013年11月20日
android:layout_marginTop = 5dp
android:textColor = #999999
android:textSize = < span class =code-string> 12sp />


< / textview>< / textview>< / textview>< / linearlayout>


< / linearlayout>









ACTIVITY_ORGANISATION_LIST.XML

< relativelayout xmlns:android =   http://schemas.android.com/apk/res/android> 
xmlns:tools = http://schemas.android.com/tools
android:layout_width = match_parent
android:layout_height = match_parent
android:paddingBottom = @ dimen / activity_vertical_margin
android:paddingLeft = @ dimen / activity_horizo​​ntal_margin
android:paddingRight = @ dimen / activity_horizo​​ntal_margin
android:paddingTop = @ dimen / activity_vertical_margin
工具:context = com.example.ramneekkashyap.app2.ORGANISATIONListAc tivity $ PlaceholderFragment>

< listview>
android:id = @ + id / list
android:layout_width = match_parent
android:layout_height = match_parent>
< / listview>

< / relativelayout>

解决方案

PlaceholderFragment>

< listview>
android:id = @ + id / list
android:layout_width = match_parent
android:layout_height = match_parent>
< / listview>

< /的RelativeLayout>


i have done the following code in android studio.
now i want to view a each element of the listview in a separate view when it is clicked

MAINACTIVITY.JAVA

package com.example.ramneekkashyap.app2;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
    Button _loginBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _loginBtn = (Button) findViewById(R.id.btn_login);

        _loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //This is a comment which does no good to your code. Feel free to remove it after you copy paste.
                //When the button is clicked, the control will come to this method.
                //To demonstrate this, let us try changing the label of the Button from 'Login' to 'I am clicked'

                Intent intent = new Intent(MainActivity.this, ORGANISATIONListActivity.class);
                startActivity(intent);
            }

        });//check if the current Activity needs to be skipped {
    }


    @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_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}





ACTIVITY_MAIN.XML

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:removed="#ffffffff">

    <textview android:text="@string/hello_ORGANISATION" android:layout_width="match_parent">
        android:layout_height="wrap_content"
        android:removed="#ff7bffff"
        android:textColor="#ff000000"
        android:textSize="26dp"
        android:id="@+id/textView" />

    <linearlayout>
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/textView"
        android:id="@+id/linearLayout">

        <textview>
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="User Name"
            android:id="@+id/textView2"
            android:textColor="#ff000000" />

        <edittext>
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/id_of_the_username_field"
            android:layout_weight="1" />
    </edittext></textview></linearlayout>

    <linearlayout>
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout"
        android:id="@+id/linearLayout2">

        <textview>
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Password: "
            android:id="@+id/textView3"
            android:textColor="#ff000000" />

        <edittext>
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/editText2"
            android:layout_weight="1" />
    </edittext></textview></linearlayout>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="LOGIN"
        android:id="@+id/btn_login"
        android:layout_below="@+id/linearLayout2"
        android:layout_centerHorizontal="true"
        android:textSize="25dp" />

</textview></relativelayout>



ORGANISATIONLISTACTIVITY.JAVA

package com.example.ramneekkashyap.app2;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class ORGANISATIONListActivity extends ListActivity {

    private ArrayAdapter ORGANISATIONItemArrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ORGANISATION_list);
        ORGANISATIONItemArrayAdapter = new ORGANISATIONAdapter(this, new String[10]);
        setListAdapter(ORGANISATIONItemArrayAdapter);
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        TextView t = (TextView) v.findViewById(R.id.ORGANISATIONTitle);
        t.setText("ORGANISATION Clicked");
    }


}



ORGANISATIONADAPTER.JAVA

package com.example.ramneekkashyap.app2;

import android.view.ViewGroup;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;





public class ORGANISATIONAdapter extends ArrayAdapter{
    private LayoutInflater inflater;

    public ORGANISATIONAdapter(Activity activity, String[] items){
        super(activity, R.layout.row_ORGANISATION, items);
        inflater = activity.getWindow().getLayoutInflater();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        return inflater.inflate(R.layout.row_ORGANISATION, parent, false);
    }

}



ACTIVITY_ORGANISATION_ADAPTER.XML

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.ramneekkashyap.app2.ORGANISATIONAdapter">

    <textview android:text="@string/hello_world" android:layout_width="wrap_content">
        android:layout_height="wrap_content" />

</textview></relativelayout>



ROW_ORGANISATION.XML

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/user_profile"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp" />


    <linearlayout>
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:gravity="left" >


        <textview>
            android:id="@+id/ORGANISATIONTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_removed"
            android:text="Header Text"
        android:textSize="19sp"
        android:textColor="#222222"
        android:textStyle="bold" />


        <textview>
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ORGANISATION body text here"
        android:layout_marginTop="5dp"
        android:ellipsize="end"
        android:lines="3"
        android:textColor="#666666"
        android:textSize="14sp" />


        <textview>
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="20 Nov 2013"
        android:layout_marginTop="5dp"
        android:textColor="#999999"
        android:textSize="12sp" />


    </textview></textview></textview></linearlayout>


</linearlayout>





ACTIVITY_ORGANISATION_LIST.XML

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ramneekkashyap.app2.ORGANISATIONListActivity$PlaceholderFragment" >

    <listview>
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </listview>

</relativelayout>

解决方案

PlaceholderFragment" > <listview> android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </listview> </relativelayout>


这篇关于我想为列表视图中的每个列表项创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆