如何使用android获取id onclick listview

时间:2017-05-05 09:14:37

标签: android mysql listview onclick

我正在研究listview示例,我想从mysql数据库中选择onclick选择listview。如何做到这一点请帮助我,我正在工作很长时间,但无法找到任何解决方案。

ListTask活动

  $ npm install
  $ rimraf dist && ./node_modules/.bin/webpack --config  
   webpack.config.vendor.js && ./node_modules/.bin/webpack --config
   webpack.config.js
  $ dot net run

ListAdapter类

public class ListTask extends Activity {

Task task_;
ListView listCollege;
ProgressBar proCollageList;

TextView GetTaskName;

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

    listCollege = (ListView)findViewById(R.id.tasklist);
    proCollageList = (ProgressBar)findViewById(R.id.proCollageList);
    GetTaskName = (TextView) findViewById(R.id.taskname);

    new GetHttpResponse(this).execute();
}
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
    private Context context;
    String result;
    List<Task> Task_;
    public GetHttpResponse(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0)
    {
        HttpService httpService = new HttpService("http://192.168.0.103/GroupBuilder/GetListTask.php");
        try
        {
            httpService.ExecutePostRequest();

            if(httpService.getResponseCode() == 200)
            {
                result = httpService.getResponse();

                System.out.println("Result       . . . "+result);

                Log.d("Result", result);
                if(result != null)
                {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(result);

                        JSONObject object;
                        JSONArray array;
                        // cources college;
                        Task_ = new ArrayList<Task>();
                        for(int i=0; i<jsonArray.length(); i++)
                        {
                            task_ = new Task();
                            object = jsonArray.getJSONObject(i);

                            task_.taskname = object.getString("taskname");
                            task_.tid = object.getString("tid");

                            System.out.println("Taask Tid  "+task_.tid);
                            Task_.add(task_);
                        }
                    }
                    catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            else
            {
                Toast.makeText(context, httpService.getErrorMessage(), Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

activity_list_task.xml代码

public class ListTaskAdapter extends BaseAdapter {

Context context;
List<Task> taskList;
Task task_;

public ListTaskAdapter(List<Task> listValue, Context context)
{
    this.context = context;
    this.taskList = listValue;
}

@Override
public int getCount() {
    return this.taskList.size();
}

@Override
public Object getItem(int position) {
    return this.taskList.get(position);
}

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

@Override
public View getView(final int position, View convertView, final ViewGroup viewGroup) {

    ViewTaskName viewtaskname = null;
    if(convertView == null)
    {
        viewtaskname = new ViewTaskName();
        LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        //LayoutInflater layoutInfiater = LayoutInflater.from(context);
        convertView = layoutInfiater.inflate(R.layout.listtaskadapter, null);

        viewtaskname.task_name = (TextView)convertView.findViewById(R.id.taskname);

        final String str = viewtaskname.task_name.getText().toString();
        convertView.setTag(viewtaskname);
    }
    else
    {
        viewtaskname = (ViewTaskName) convertView.getTag();
    }

    viewtaskname.task_name.setText(taskList.get(position).taskname);
    return convertView;
}
    }
class ViewTaskName
    {
TextView task_name;
    }


class Task{
public String taskname;
public String tid;
}

listadaper代码

<ListView
    android:id="@+id/tasklist"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    android:background="#38a2ed"
    android:layout_marginTop="35dp" >
</ListView>

我没有得到任何解决方案,请帮助我并更新我的代码,谢谢

4 个答案:

答案 0 :(得分:1)

您应该使用 setOnItemClickListener

  

注册此AdapterView中的项目时要调用的回调   点击了。

创建一个包含名称等ID的TextView。

<TextView
android:id="@+id/taskID"
android:layout_width="0dp"
android:layout_height="0dp"

/>

然后

 listCollege.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
  {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
    {
         TextView taskIDOBJ=(TextView)view.findViewById(R.id.taskID);
        String item = tasknameOBJ.getText().toString();
        Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();                
    }
});

简单方法

 String str_ID = Task_.get(position).tid;

<强>代码

 listCollege.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
  {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
    {
        String str_ID = Task_.get(position).tid;
        Toast.makeText(this,"You selected : " + str_ID ,Toast.LENGTH_SHORT).show();                
    }
});

答案 1 :(得分:0)

试试这个

//declare global use this array list in AsyncTask response to add
List<Tast> taskList = new ArrayList<>();


    listCollege.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String tid = taskList.get(position).tid;
                }
            });

答案 2 :(得分:0)

使用setOnItemClickListener方法并从sqllite获取id,此方法根据您从bean类获取id的位置为您提供一个位置。点击此链接。

Get Id from database in a listview onclick

答案 3 :(得分:-1)

view.setTag(id);
view.getTag();